作业四
程序员文章站
2022-09-05 13:41:47
<%@ page import="java.sql.Connection" %><%@ page import="java.sql.Driver" %><%@ page import="java.sql.DriverManager" %><%@ page import="javax.swing.plaf.nimbus.State" %><%@ page import="java.sql.Statement" %><%@ page i...
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="javax.swing.plaf.nimbus.State" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.mysql.jdbc.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.name.user"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//user对象实例化
//userBean user= new userBean();
//给对象赋值
//user.setUsername(request.getParameter("username"));
//user.setPassword(request.getParameter("password"));
//数据库操作
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//报错,说明驱动包没有完成
//建立数据库连接 18.3.22.1
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql = "insert into user(username,password,gender,age) values (?,?,?,?)";
PreparedStatement temt = connection.prepareStatement(sql);
temt.setString(1,request.getParameter("username"));
temt.setString(2,request.getParameter("password"));
temt.setString(3,request.getParameter("gender"));
temt.setInt(4,request.getParameter("age"));
ResultSet rs = temt.executeQuery();
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="javax.swing.plaf.nimbus.State" %>
<%@ page import="java.sql.Statement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.name.user">
</jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);
String sql = "insert into user (username,password,gender,age) values ('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//user对象实例化
//userBean user= new userBean();
//给对象赋值
//user.setUsername(request.getParameter("username"));
//user.setPassword(request.getParameter("password"));
//数据库操作
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//报错,说明驱动包没有完成
//建立数据库连接 18.3.22.1
String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);
//3创建Statement对象 执行sql语句 alt+enter 自动引入缺失的类
Statement stmt = connection.createStatement();
//删除
String sql="delete from user where id = 4 ";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("删除失败");
}else{
out.print("删除成功");
}
%>
本文地址:https://blog.csdn.net/weixin_45820586/article/details/108971924