Java写一个JSP页面,Springboot实现Mysql数据库的增删改查
- 一、目标
- 二、环境配置
- 1、在web目录下的Web-INF下创建一个lib目录,并把mysql-connector-java-8.0.21.jar包复制进去
- 2、将jar包加入到项目中
- 三、hello world效果
- 确保在计算机上安装好MySQL数据库
- 四、连接测试是否连接成功数据库
- 五、开始正式进入JSP页面实现数据库的增删改查
- 1、需要创建一个Student.java,并生成该类的set,get方法,该类放在src/domain目录下
- 2、实现全查询功能,Dao.java中加入全查询代码,index.jsp文件中加入查询代码,web下创建selectNameStudent.jsp文件
- 3、按照姓名查询:Dao.java中加入查询代码,web下创建selectNameStudent.jsp文件(上面已经创建)
- 4、插入数据功能:Dao.java中加入查询代码,web下创建insert.jsp和info.jsp文件,info.jsp用来进行提示页的功能
- 5、删除数据功能:Dao.java中加入查询代码,web下创建delete.jsp文件
- 6、修改数据功能:Dao.java中加入查询代码,web下创建update.jsp和info2.jsp文件,info2.jsp用来进行提示页的功能
- 六、全部的Dao.java代码
一、目标
一个JSP页面,Springboot实现Mysql数据库的增删改查
二、环境配置
1.Intellij IDEA
2.Tomcat 8.5.75
在创建项目之前,可以准备下载好,然后配置上,网上资料很多,这里不做赘述
还挺麻烦的,需要配置路径
3.java运行环境 jdk 1.8
安装jdk过程这里省略一下
4.数据库MySQL 8.0.21
需要下载:mysql-connector-java-8.0.21.jar,百度上搜一搜下载即可
5.java数据连接驱动 jdbc 8.0.21.
项目创建过程:
然后选择项目路径,取个名字即可
1、在web目录下的Web-INF下创建一个lib目录,并把mysql-connector-java-8.0.21.jar包复制进去
2、将jar包加入到项目中
现在前期工作就准备好了,
可以测试一下效果,用hello world
三、hello world效果
在index.jsp中写一下,然后右上角运行
自动跳转页面
确保在计算机上安装好MySQL数据库
具体安装步骤,可上网上查询
注意:记住自己建立的数据库名字
四、连接测试是否连接成功数据库
(默认数据库已经创建数据库和表)首先要编写连接数据库的代码,在src目录下创建一个connect目录(持久层)用于存放数据库操作的代码,一个domain目录(用于存放实体类),一个Test目录(用于测试代码)。
1、创建Dao.java
注意:连接数据库的三大要素中URL,“student”为自己建立的数据库名,自行替换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package connect; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Dao { //连接数据库的三大要素 final String URL = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC"; final String USER = "root"; final String PWD = "123456"; Connection connection; public Connection getConnection(){ try{ Class.forName("com.mysql.cj.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); } try{ connection = DriverManager.getConnection(URL, USER, PWD); this.connection = connection; }catch(SQLException e){ e.printStackTrace(); } return connection; } }
2、创建Mytest.java
测试数据库驱动(jar文件)是否加载成功。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package test; import connect.*; import java.sql.Connection; import java.sql.SQLException; public class Mytest { public static void main(String[] args) throws SQLException { Dao dao = new Dao(); Connection connection = dao.getConnection(); if(connection != null){ System.out.println("数据库连接成功"); }else{ System.out.println("数据库连接失败"); } } }
运行Mytest.java文件,出现数据库连接成功,则成功!
五、开始正式进入JSP页面实现数据库的增删改查
1、需要创建一个Student.java,并生成该类的set,get方法,该类放在src/domain目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51public class Student { private int id; //成员变量(字段)、实例变量(表示该Id变量既属于成员变量又属于实例变量) private String name; //成员变量(字段)、实例变量 private String sex; private int age; private String email; // 将返回的结果集封装成对象 public int getId(){ //id的可读属性 return id; } public void setId(int id){//id的可写属性 this.id = id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getSex(){ return sex; } public void setSex(String sex){ this.sex = sex; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public String getEmail(){ return email; } public void setEmail(String email){ this.email = email; } @Override //重写toString public String toString(){ return "student{" + "id=" + id + "name=" + name + "sex=" + sex + "age=" + age + "email=" + email + "}"; } }
2、实现全查询功能,Dao.java中加入全查询代码,index.jsp文件中加入查询代码,web下创建selectNameStudent.jsp文件
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37public List<Student> selectAll(){ //select All()方法用来查询所有数据 String selectAll = "select * from student"; try{ //Connection.createStatement()方法用于创建一个Statement对象 //封装 SQL 语句发送给数据库,通常用来执行不带参数的SQL语句 statement = connection.createStatement(); }catch(SQLException e){ e.printStackTrace(); } try{ //产生一个结果集resultSet resultSet = statement.executeQuery(selectAll); //创建一个list集合将结果集数据遍历出来,放在student对象中 // 将对象放在list集合中,最后将集合通过循环的方法输出 while(resultSet.next()){ Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setSex(resultSet.getString("sex")); student.setAge(resultSet.getInt("age")); student.setEmail(resultSet.getString("email")); list.add(student); } }catch(SQLException e){ e.printStackTrace(); }finally{ try{ resultSet.close(); statement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return list; }
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64<%@ page import="java.sql.*" %><%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/10 Time: 15:29 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.util.List" %> <%@ page import="domain.Student" %> <html> <head> <title>查询学生</title> </head> <body> <form action="selectNameStudent.jsp" method="post"> 输入姓名: <input type="text" name="name"> <input type="submit" value="查询"> </form> <form action="insert.jsp" method="post"> <input type="submit" value="插入数据"> </form> <br> <% Dao dao=new Dao(); Connection connection=dao.getConnection(); if (connection!=null){ //out.println("数据库连接成功"); List<Student> list= dao.selectAll(); out.print("<table border='1' cellspacing='1'><tr>"); out.print("<caption>人员信息表</caption>"); out.print("<tr><th width='60'>序号</th>" + "<th width='100'>id</th>" + "<th width='100'>姓名</th>" + "<th width='100'>性别</th>" + "<th width='100'>年龄</th>" + "<th width='120'>邮箱</th>" + "<th width='100'>操作</th>"); int count = 0; for (Student student:list){ out.print("<tr>"); out.print("<td>" + (++count) + "</td>"); out.print("<td>" + student.getId()+ "</td>"); out.print("<td>" + student.getName() + "</td>"); out.print("<td>" + student.getSex() + "</td>"); out.print("<td>" + student.getAge()+ "</td>"); out.print("<td>" + student.getEmail() + "</td>"); out.print("<td>" + "<a href=delete.jsp?id=" + student.getId() + ">删除" + "</a>" + " / <a href=update.jsp?id=" + student.getId() + ">修改" + "</a>"+ "</td>"); out.print("</tr>"); } out.print("</table>"); connection.close(); connection.close(); }else { out.println("数据库连接失败"); } %> </body> </html>
selectNameStudent.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56<%@ page import="java.sql.*" %><%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/15 Time: 11:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <%@ page import="domain.Student" %> <%@ page import="java.util.List" %> <html> <head> <title>查询结果</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String Name=request.getParameter("name"); Dao dao=new Dao(); Connection connection=dao.getConnection(); if (connection!=null){ out.println("数据库连接成功"); List<Student> list= dao.selectName(Name); out.print("<table border='1' cellspacing='1'><tr>"); out.print("<caption>人员信息表</caption>"); out.print("<tr><th width='60'>序号</th>" + "<th width='100'>id</th>" + "<th width='100'>姓名</th>" + "<th width='100'>性别</th>" + "<th width='100'>年龄</th>" + "<th width='120'>邮箱</th>" + "<th width='100'>操作</th>"); int count = 0; for (Student student:list){ out.print("<tr>"); out.print("<td>" + (++count) + "</td>"); out.print("<td>" + student.getId()+ "</td>"); out.print("<td>" + student.getName() + "</td>"); out.print("<td>" + student.getSex() + "</td>"); out.print("<td>" + student.getAge()+ "</td>"); out.print("<td>" + student.getEmail() + "</td>"); out.print("<td>" + "<a href=deleteuser.jsp?id=" + student.getId() + ">删除" + "</a>" + " / <a href=updateuser.jsp?id=" + student.getId() + ">修改" + "</a>"+ "</td>"); out.print("</tr>"); } out.print("</table>"); }else { out.println("数据库连接失败"); } %> </body> </html>
调用selectAll()方法,实现数据库的查询,这个是我在数据库中已经建立好的表格
3、按照姓名查询:Dao.java中加入查询代码,web下创建selectNameStudent.jsp文件(上面已经创建)
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34public List<Student> selectName(String Name){ //selectName()实现查询功能 String selectstudent = "select * from student where name=?"; try{ //String sql = "select * from student where name=?"; //PreparedStatement是预编译的,也叫JDBC存储过程 //PreparedStatement对象的参数被强制进行类型转换,确保数据与底层的数据库格式匹配 //防止 SQL 注入 preparedStatement = connection.prepareStatement(selectstudent); preparedStatement.setString(1, Name); System.out.println(Name); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setSex(resultSet.getString("sex")); student.setAge(resultSet.getInt("age")); student.setEmail(resultSet.getString("email")); list.add(student); } }catch(SQLException e){ e.printStackTrace(); }finally { try{ resultSet.close(); preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return list; }
4、插入数据功能:Dao.java中加入查询代码,web下创建insert.jsp和info.jsp文件,info.jsp用来进行提示页的功能
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26//insert()方法插入数据 public int insert(int Id, String Name, String Sex, int Age,String Email){ String sql = "insert into student values(?,?,?,?,?)"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,Id); preparedStatement.setString(2,Name); preparedStatement.setString(3,Sex); preparedStatement.setInt(4,Age); preparedStatement.setString(5,Email); i=preparedStatement.executeUpdate(); }catch (SQLException e){ e.printStackTrace(); }finally { try { preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; }
insert.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48<%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/15 Time: 15:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <html> <head> <title>插入数据</title> </head> <body> <form action="info.jsp" method="post"> <h4 align="center">插入数据</h4> <table align="center" border="1" cellspacing="2"> <tr> <td width="80" align="center">id:</td> <td width="120"><input type="text" name="id"></td> </tr> <tr> <td width="80" align="center">姓名:</td> <td width="120"><input type="text" name="name"></td> </tr> <tr> <td width="80" align="center">性别:</td> <td width="120"><input type="text" name="sex"></td> </tr> <tr> <td width="80" align="center">年龄:</td> <td width="120"><input type="text" name="age"></td> </tr> <tr> <td width="80" align="center">邮箱:</td> <td width="120"><input type="text" name="email"></td> </tr> <tr> <th width="120" colspan="2"><input type="submit" value="提交"></th> </tr> </table> </form> </body> </html>
info.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42<%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/15 Time: 15:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <html> <head> <title>提示页</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String id=request.getParameter("id"); String Name=request.getParameter("name"); String Sex=request.getParameter("sex"); String age=request.getParameter("age"); String Email=request.getParameter("email"); int ID=Integer.parseInt(id); int Age=Integer.parseInt(age); Dao dao=new Dao(); Connection connection=dao.getConnection(); if (connection!=null){ int i=dao.insert(ID,Name,Sex,Age,Email); out.print("成功添加"+i+"条数据"); out.print("添加成功"+"<a href=index.jsp?id=>返回首页" + "</a>"); }else { out.print("数据库连接数据失败"); out.print("添加失败"+"<a href=index.jsp?id=>返回首页" + "</a>"); } %> </body> </html>
5、删除数据功能:Dao.java中加入查询代码,web下创建delete.jsp文件
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public int deleteStudent(int Id){ String sql = "delete FROM student where id=?"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,Id); i = preparedStatement.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); }finally{ try{ preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; }
delete.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/16 Time: 10:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <html> <head> <title>删除数据</title> </head> <body> <% String id=request.getParameter("id"); int Id=Integer.parseInt(id); out.print(Id); Dao dao=new Dao(); Connection connection=dao.getConnection(); if(connection!=null){ int i=dao.deleteStudent(Id); out.print("成功删除"+i+"条数据"); out.print("删除成功"+"<a href=index.jsp?id=>返回首页" + "</a>"); }else { out.print("数据库连接失败"); out.print("删除失败"+"<a href=index.jsp?id=>返回首页" + "</a>"); } %> </body> </html>
6、修改数据功能:Dao.java中加入查询代码,web下创建update.jsp和info2.jsp文件,info2.jsp用来进行提示页的功能
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public int updateStudent(String Name, String Sex, int Age, String Email, int Id){ String sql = "update Student set name =?,sex =?,age=?,email=? where id=?"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,Name); preparedStatement.setString(2,Sex); preparedStatement.setInt(3,Age); preparedStatement.setString(4,Email); preparedStatement.setInt(5,Id); i = preparedStatement.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); }finally{ try{ preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; }
update.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/16 Time: 10:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <html> <head> <title>修改数据</title> </head> <body> <% String id; id=request.getParameter("id"); int Id=Integer.parseInt(id); %> <form action="info2.jsp"> 学号:<input type="text" value="<%=Id%>" name="id"> 姓名:<input type="text" name="name"> 性别:<input type="text" name="sex"> 年龄:<input type="text" name="age" > 邮箱:<input type="text" name="email"> <input type="submit" value="修改"> </form> </body> </html>
info2.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38<%-- Created by IntelliJ IDEA. User: genghong Date: 2022/2/16 Time: 10:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="connect.Dao" %> <%@ page import="java.sql.Connection" %> <html> <head> <title>提示页</title> </head> <body> <% String id=request.getParameter("id"); String Name=request.getParameter("name"); String Sex=request.getParameter("sex"); String age=request.getParameter("age"); String Email=request.getParameter("email"); int Id=Integer.parseInt(id); int Age=Integer.parseInt(age); out.print(Id); Dao dao=new Dao(); Connection connection=dao.getConnection(); if (connection!=null){ int i=dao.updateStudent(Name,Sex,Age,Email,Id); out.print("成功修改了"+i+"条数据"); out.print("修改成功"+"<a href=index.jsp?id=>返回首页" + "</a>"); }else{ out.print("数据库连接失败"); out.print("修改失败"+"<a href=index.jsp?id=>返回首页" + "</a>"); } %> </body> </html>
六、全部的Dao.java代码
Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180package connect; import domain.Student; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Dao { //连接数据库的三大要素 student为表名 final String URL = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC"; final String USER = "root"; final String PWD = "123456"; Connection connection; List<Student> list = new ArrayList<>(); Statement statement; ResultSet resultSet; PreparedStatement preparedStatement; public Connection getConnection(){ //连接数据库 try{ //加载数据库驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); } try{ //通过DriverManager的getConnection()方法获取数据库连接 connection = DriverManager.getConnection(URL, USER, PWD); this.connection = connection; }catch(SQLException e){ e.printStackTrace(); } return connection; } public List<Student> selectAll(){ //select All()方法用来查询所有数据 String selectAll = "select * from student"; try{ //Connection.createStatement()方法用于创建一个Statement对象 //封装 SQL 语句发送给数据库,通常用来执行不带参数的SQL语句 statement = connection.createStatement(); }catch(SQLException e){ e.printStackTrace(); } try{ //产生一个结果集resultSet resultSet = statement.executeQuery(selectAll); //创建一个list集合将结果集数据遍历出来,放在student对象中 // 将对象放在list集合中,最后将集合通过循环的方法输出 while(resultSet.next()){ Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setSex(resultSet.getString("sex")); student.setAge(resultSet.getInt("age")); student.setEmail(resultSet.getString("email")); list.add(student); } }catch(SQLException e){ e.printStackTrace(); }finally{ try{ resultSet.close(); statement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return list; } public List<Student> selectName(String Name){ //selectName()实现查询功能 String selectstudent = "select * from student where name=?"; try{ //String sql = "select * from student where name=?"; //PreparedStatement是预编译的,也叫JDBC存储过程 //PreparedStatement对象的参数被强制进行类型转换,确保数据与底层的数据库格式匹配 //防止 SQL 注入 preparedStatement = connection.prepareStatement(selectstudent); preparedStatement.setString(1, Name); System.out.println(Name); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setSex(resultSet.getString("sex")); student.setAge(resultSet.getInt("age")); student.setEmail(resultSet.getString("email")); list.add(student); } }catch(SQLException e){ e.printStackTrace(); }finally { try{ resultSet.close(); preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return list; } //insert()方法插入数据 public int insert(int Id, String Name, String Sex, int Age,String Email){ String sql = "insert into student values(?,?,?,?,?)"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,Id); preparedStatement.setString(2,Name); preparedStatement.setString(3,Sex); preparedStatement.setInt(4,Age); preparedStatement.setString(5,Email); //表示插入的记录数量 i=preparedStatement.executeUpdate(); }catch (SQLException e){ e.printStackTrace(); }finally { try { preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; } public int deleteStudent(int Id){ String sql = "delete FROM student where id=?"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,Id); i = preparedStatement.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); }finally{ try{ preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; } public int updateStudent(String Name, String Sex, int Age, String Email, int Id){ String sql = "update Student set name =?,sex =?,age=?,email=? where id=?"; int i = 0; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,Name); preparedStatement.setString(2,Sex); preparedStatement.setInt(3,Age); preparedStatement.setString(4,Email); preparedStatement.setInt(5,Id); i = preparedStatement.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); }finally{ try{ preparedStatement.close(); connection.close(); }catch (SQLException e){ e.printStackTrace(); } } return i; } }
最后
以上就是碧蓝溪流最近收集整理的关于【Java】Java写一个JSP页面,Springboot实现Mysql数据库的增删改查一、目标二、环境配置三、hello world效果四、连接测试是否连接成功数据库五、开始正式进入JSP页面实现数据库的增删改查六、全部的Dao.java代码的全部内容,更多相关【Java】Java写一个JSP页面,Springboot实现Mysql数据库的增删改查一、目标二、环境配置三、hello内容请搜索靠谱客的其他文章。
发表评论 取消回复