PreparedStatement与Statement在使用时的区别
1.Statement:
a.写sql语句
b.然后再执行executeUpdate(sql)或executeQuery(sql)
2.PreparedStatement:
a.写sql语句(可能存在占位符:?)
b.在创建PreparedStatement对象时,将sql预编译:prepareStatement(sql)
c.setXXX():替换占位符
d.最后执行executeUpdate()或executeQuery()
推荐使用PreparedStatement的原因:
1.编码更加简洁
避免了写sql语句时字符串的拼接。
2.提高性能
应用场景:录入100条数据
Statement:由其方法executeUpdate(sql)可知,要将sql语句编译100次
PStatement:因为其先进行预编译,然后再执行executeUpdate()
3.安全
可以有效防止sql注入
注意:PreparedStatement的强大之处体现在以下这段代码
先写sql语句,其次用占位符来实现Statement那繁琐的拼接字符串最后再执行增删改查方法String sql="insert into team values(?,?,?)";pstmt=con.prepareStatement(sql);//预编译pstmt.setInt(1, 8);pstmt.setString(2, "科比");pstmt.setInt(3, 40);System.out.println("获取数据对象成功!");pstmt.executeUpdate();pstmt.executeQuery();
应用实例:使用PreparedStatement来实现对数据库的增删改查
JDBCDemo2.java
package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JDBCDemo2 {// 连接字符串private static final String URL = "jdbc:mysql://127.0.0.1:3306/basketball";private static final String USERNAME = "root";private static final String PWD = "123abc";private static final String DRIVER="com.mysql.jdbc.Driver";//对数据库的增删改:executeUpdate()public static void update() {// 增删改Connection con = null;PreparedStatement pstmt=null;try {// a.导入驱动包:Class.forName()Class.forName(DRIVER);System.out.println("成功加载驱动!");// b.与数据库建立连接:DriverManager.getConnection()con = DriverManager.getConnection(URL,USERNAME,PWD);System.out.println("连接数据库成功!");//c.获取数据对象:con.prepareStatement()String sql="insert into team values(?,?,?)";pstmt=con.prepareStatement(sql);//预编译pstmt.setInt(1, 8);pstmt.setString(2, "科比");pstmt.setInt(3, 40);System.out.println("获取数据对象成功!");//d.操作数据对象:pstmt.executeUpdate()pstmt.executeUpdate();System.out.println("数据对象操作成功!");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {try {if (pstmt != null) pstmt.close();if (con != null) con.close();} catch (SQLException e) {e.printStackTrace();}}}//对数据库的增删改:executeQuery()public static void query() {Connection con=null;PreparedStatement pstmt=null;ResultSet rs=null;try {//1.加载具体的驱动类:Class.forName()Class.forName(DRIVER);System.out.println("加载驱动类成功!");//2.连接数据库:DriverManager.getConnection()con=DriverManager.getConnection(URL,USERNAME,PWD);System.out.println("连接数据库成功!");//3.获取操作数据库对象:prepareStatement()//String sql="select * from team where t_name like ?";String sql="select * from team";pstmt=con.prepareStatement(sql);//pstmt.setString(1, "%姆斯");//模糊查询System.out.println("获取操作数据库对象成功!");//4.查询数据:stmt.executeQuery()pstmt.executeQuery();//5.返回到结果集rsrs=pstmt.executeQuery(sql);System.out.println("查询数据成功!");//6.获取数据:rs.next(),rs.getXXX()while(rs.next()!=false) {//rs.next():判断该行是否有数据,返回值为true或falseint id=rs.getInt("t_id");//rs.getInt():获取数据表对应行的指定字段值的数据String name=rs.getString("t_name");int age=rs.getInt("t_age");System.out.println(id+"---"+name+"---"+age);}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {//关闭jdbc三幻神(类似与栈的顺序:先开先关)if(rs!=null) rs.close();if(pstmt!=null) pstmt.close();if(con!=null) con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {//增删改//update();//查询query();}}
测试截图
如果觉得《JDBC:PreparedStatement与Statement在使用时的区别》对你有帮助,请点赞、收藏,并留下你的观点哦!