Statement stat = conn.createStatement();
时间: 2024-02-29 14:53:39 浏览: 29
这是一个Java代码片段,用于创建一个与数据库连接相关的声明对象。该声明对象可以用于执行SQL语句并返回结果。在这个代码片段中,假设已经建立了一个数据库连接对象conn,然后使用conn对象创建了一个声明对象stat。这个声明对象用于执行SQL语句并返回结果,例如查询数据库中的数据或者更新数据库中的记录。
相关问题
public class StudentDao { private static Connection conn = null; public static void initConnection() throws Exception { // 连接MySQL Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys", "root", "1234"); } // 返回所有学生 public ArrayList<Student> getAllStudents() throws Exception { ArrayList<Student> al = new ArrayList<Student>(); initConnection(); String sql = "SELECT num,name FROM tb_student"; Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { Student stu = new Student(); al.add(stu); } rs.close(); stat.close(); closeConnection(); return al; } // 添加学生信息到数据中 public int addStudent(Student student) throws Exception { initConnection(); //创建一个sql语句 String sql = "insert into tb_student(num,name) values (?, ?)"; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 ps.setString(1,student.getNum()); ps.setString(2,student.getName()); //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 conn.close(); return retv; } // 修改学生信息 public int updateStudent(Student student) throws Exception { //创建一个连接 initConnection(); //创建一个sql语句 String sql = ""; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 closeConnection(); //返回影响的行数 return retv; } // 删除学生 public int delStudent(Student student) throws Exception { //创建一个连接 initConnection(); //创建一个sql语句 String sql = ""; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 closeConnection(); //返回影响的行数 return retv; } public static void closeConnection() throws Exception { conn.close(); } }请把代码补充完整,并写出相应的增、删、改、查的.jsp代码
这里给出Student类的基本属性和方法:
public class Student {
private String num; // 学号
private String name; // 姓名
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
补充完整的StudentDao类如下:
import java.sql.*;
import java.util.ArrayList;
public class StudentDao {
private static Connection conn = null;
public static void initConnection() throws Exception {
// 连接MySQL
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys", "root", "1234");
}
// 返回所有学生
public ArrayList<Student> getAllStudents() throws Exception {
ArrayList<Student> al = new ArrayList<Student>();
initConnection();
String sql = "SELECT num,name FROM tb_student";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
Student stu = new Student();
stu.setNum(rs.getString("num"));
stu.setName(rs.getString("name"));
al.add(stu);
}
rs.close();
stat.close();
closeConnection();
return al;
}
// 添加学生信息到数据中
public int addStudent(Student student) throws Exception {
initConnection();
// 创建一个sql语句
String sql = "insert into tb_student(num,name) values (?, ?)";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
ps.setString(2, student.getName());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
return retv;
}
// 修改学生信息
public int updateStudent(Student student) throws Exception {
// 创建一个连接
initConnection();
// 创建一个sql语句
String sql = "update tb_student set name=? where num=?";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getName());
ps.setString(2, student.getNum());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
// 返回影响的行数
return retv;
}
// 删除学生
public int delStudent(Student student) throws Exception {
// 创建一个连接
initConnection();
// 创建一个sql语句
String sql = "delete from tb_student where num=?";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
// 返回影响的行数
return retv;
}
public static void closeConnection() throws Exception {
conn.close();
}
}
增删改查的.jsp代码如下:
<!-- 添加学生 -->
<form action="addStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
姓名:<input type="text" name="name"><br>
<input type="submit" value="添加">
</form>
<!-- 修改学生 -->
<form action="updateStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
姓名:<input type="text" name="name"><br>
<input type="submit" value="修改">
</form>
<!-- 删除学生 -->
<form action="delStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
<input type="submit" value="删除">
</form>
<!-- 查询学生 -->
<table border="1" cellpadding="5">
<tr>
<th>学号</th>
<th>姓名</th>
</tr>
<%
StudentDao dao = new StudentDao();
ArrayList<Student> list = dao.getAllStudents();
for (Student stu : list) {
%>
<tr>
<td><%=stu.getNum()%></td>
<td><%=stu.getName()%></td>
</tr>
<% } %>
</table>
String id = request.getParameter("id"); String dname = request.getParameter("dname"); String btime= request.getParameter("btime"); String title= request.getParameter("title"); Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library","root","123456"); Statement stat=conn.createStatement(); String sql="update books set count=count-1 where id="+id; stat.executeUpdate(sql); stat.close(); conn.close(); response.sendRedirect("borrow.jsp");使用批出理技术同时实现String sql=insert into borrow(dname,id,title,btime) values('"+dname+"','"+id+"','"+title+"','"+btime+"',);语句
可以使用PreparedStatement实现批量处理和参数化查询,代码如下:
```
String id = request.getParameter("id");
String dname = request.getParameter("dname");
String btime= request.getParameter("btime");
String title= request.getParameter("title");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library","root","123456");
String updateSql="update books set count=count-1 where id=?";
String insertSql="insert into borrow(dname,id,title,btime) values(?,?,?,?)";
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
updateStmt.setString(1, id);
PreparedStatement insertStmt = conn.prepareStatement(insertSql);
insertStmt.setString(1, dname);
insertStmt.setString(2, id);
insertStmt.setString(3, title);
insertStmt.setString(4, btime);
conn.setAutoCommit(false);
updateStmt.executeUpdate();
insertStmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
updateStmt.close();
insertStmt.close();
conn.close();
response.sendRedirect("borrow.jsp");
```
注意这里使用了参数化查询,可以避免SQL注入的问题。同时使用了批量处理技术,将两个SQL语句同时执行,可以提高效率。