|NO.Z.00089|——————————|BigDataEnd|——|Java&MySQL.JDBC.V14|——|MySQL.v14|Jdbc开发_预处理对象的执行原理|

一、PreparedStatement的执行原理
### --- 分别使用 Statement对象 和 PreparedStatement对象进行插入操作

——>        代码示例
public class TestPS {
    
    public static void main(String[] args) throws SQLException {
        
        Connection con = JDBCUtils.getConnection();

        //获取 Sql语句执行对象
        Statement st = con.createStatement();

        //插入两条数据
        st.executeUpdate("insert into jdbc_user values(null,'张三','123','1992/12/26')
                         ");
        st.executeUpdate("insert into jdbc_user values(null,'李四','123','1992/12/26')");
        
    
        //获取预处理对象
        PreparedStatement ps = con.prepareStatement("insert into jdbc_user values(?,?,?,?)");

        //第一条数 设置占位符对应的参数
        ps.setString(1,null);
        ps.setString(2,"长海");
        ps.setString(3,"qwer");
        ps.setString(4,"1990/1/10");

        //执行插入
        ps.executeUpdate();

        //第二条数据
        ps.setString(1,null);
        ps.setString(2,"小斌");
        ps.setString(3,"1122");
        ps.setString(4,"1990/1/10");

        //执行插入
        ps.executeUpdate();

        //释放资源
        st.close();
        ps.close();
        con.close();
    }
}
### --- Statement 与 PreparedStatement的区别?

——>        Statement用于执行静态SQL语句,在执行时,必须指定一个事先准备好的SQL语句。
——>        PrepareStatement是预编译的SQL语句对象,语句中可以包含动态参数“?”,
——>        在执行时可以为“?”动态设置参数值。
——>        PrepareStatement可以减少编译次数提高数据库性能。
二、sql语句
package com.yanqi.jdbc05;

        import com.yanqi.jdbc05.JdbcUtils;

        import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.SQLException;
        import java.sql.Statement;

public class JdbcPs {

    public static void main(String[] args) throws SQLException {

        Connection connection = JdbcUtils.getConnection();

        //获取Statement
        Statement statement = connection.createStatement();

        //向数据库插入两条数据
        statement.executeUpdate("insert into jdbc_user values(null,'张三','123456','2000/12/26')");
        statement.executeUpdate("insert into jdbc_user values(null,'李四','654321','1900/12/26')");

        //获取预处理对象
        PreparedStatement ps = connection.prepareStatement("insert into jdbc_user values(?,?,?,?)");

        //先插入第一条数据
        ps.setObject(1,null);
        ps.setString(2,"小斌");
        ps.setString(3,"qwer");
        ps.setString(4,"1999/11/11");
        //执行插入
        ps.executeUpdate();

        //插入第二条数据
        ps.setObject(1,null);
        ps.setString(2,"长海");
        ps.setString(3,"asdf");
        ps.setString(4,"2000/11/11");
        //执行插入
        ps.executeUpdate();

        //释放资源
        statement.close();
        ps.close();
        connection.close();
    }

}

 
 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示