PreparedStatement statement = connection.prepareStatement(sql);和 Statement statement = connection.createStatement()有什么区别
时间: 2024-04-18 11:31:00 浏览: 10
PreparedStatement statement = connection.prepareStatement(sql) 和 Statement statement = connection.createStatement() 有以下区别:
1. 参数化查询:PreparedStatement 允许使用参数化查询,可以通过占位符(?)来动态设置查询参数的值。这样可以防止 SQL 注入攻击,并且提高了代码的安全性。
2. 编译查询:PreparedStatement 在创建时会将 SQL 语句编译成预编译状态,然后将其发送到数据库。这样可以提高查询的执行效率,特别是对于需要多次执行的查询语句。
3. 可读性和维护性:PreparedStatement 使得 SQL 语句与代码分离,易于阅读和维护。你可以将 SQL 语句放在一个单独的字符串中,并使用占位符来表示参数,而不需要在代码中拼接字符串。
4. 执行效率:由于 PreparedStatement 的查询语句在创建时已经编译过,所以在执行时可以直接使用预编译的语句,而 Statement 需要每次执行都进行编译,这可能导致一些性能上的差异。
综上所述,PreparedStatement 在安全性、性能和代码可读性方面都优于 Statement。除非你有特殊的需求,一般情况下建议使用 PreparedStatement。
相关问题
改正程序。 con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * FROM student"; PreparedStatement pstm = con.prepareStatement(sqlStatement); rs = sql.executeQuery(sqlStatement); int count =0; while(rs.next()) { count++; } rs = sql.executeQuery(sqlStatement); Object[][] info=new Object[count][6]; String []title= {"学号","姓名","出生日期","性别","专业","宿舍楼"}; count=0; while(rs.next()) { info[count][0]=rs.getString("sno"); info[count][1]=rs.getString("sname"); info[count][2]=rs.getString("sbirth"); info[count][3]=rs.getString("ssex"); info[count][4]=rs.getString("sclass"); info[count][5]=rs.getString("saddress"); count++; }
改正后的程序如下:
```
con = DriverManager.getConnection(url, user, pwd);
String sqlStatement;
sqlStatement = "SELECT * FROM student";
PreparedStatement pstm = con.prepareStatement(sqlStatement);
rs = pstm.executeQuery();
int count = 0;
while(rs.next()) {
count++;
}
rs.beforeFirst();
Object[][] info = new Object[count][6];
String[] title = {"学号", "姓名", "出生日期", "性别", "专业", "宿舍楼"};
count = 0;
while(rs.next()) {
info[count][0] = rs.getString("sno");
info[count][1] = rs.getString("sname");
info[count][2] = rs.getString("sbirth");
info[count][3] = rs.getString("ssex");
info[count][4] = rs.getString("sclass");
info[count][5] = rs.getString("saddress");
count++;
}
```
修改的内容如下:
1. 将原来的 `sql` 对象改为 `pstm` 对象,并使用 `executeQuery()` 方法执行查询语句。
2. 在第一个 `while` 循环中统计查询结果的行数,并使用 `beforeFirst()` 方法将结果集指针返回到第一行。
3. 在第二个 `while` 循环中遍历查询结果,并将每行数据存储到 `info` 数组中。
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>