• JDBC预编译statement(preparedstatement)和statement的比较、execute与executeUpdate的区别


    和 Statement一样,PreparedStatement也是用来执行sql语句的
    与创建Statement不同的是,需要根据sql语句创建PreparedStatement
    除此之外,还能够通过设置参数,指定相应的值,而不是Statement那样使用字符串拼接

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
        
    public class TestJDBC {
        public static void main(String[] args) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
      
            String sql = "insert into hero values(null,?,?,?)";
            try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin");
                // 根据sql语句创建PreparedStatement
                PreparedStatement ps = c.prepareStatement(sql);
            ) {
                 
                // 设置参数
                ps.setString(1, "提莫");
                ps.setFloat(2, 313.0f);
                ps.setInt(3, 50);
                // 执行
                ps.execute();
      
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        }
    }

    Statement 需要进行字符串拼接,可读性和维护性比较差

    String sql = "insert into hero values(null,"+"'提莫'"+","+313.0f+","+50+")";

    PreparedStatement 使用参数设置,可读性好,不易犯错

    String sql = "insert into hero values(null,?,?,?)";
    public class TestJDBC {
        public static void main(String[] args) {
      
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
      
            String sql = "insert into hero values(null,?,?,?)";
            try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin"); 
                Statement s = c.createStatement(); 
                PreparedStatement ps = c.prepareStatement(sql);
            ) {
                // Statement需要进行字符串拼接,可读性和维修性比较差
                String sql0 = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")";
                s.execute(sql0);
      
                // PreparedStatement 使用参数设置,可读性好,不易犯错
                // "insert into hero values(null,?,?,?)";
                ps.setString(1, "提莫");
                ps.setFloat(2, 313.0f);
                ps.setInt(3, 50);
                ps.execute();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      
        }
    }

    原文地址:http://how2j.cn/k/jdbc/jdbc-preparedstatement/388.html#nowhere

    execute与executeUpdate的相同点:都可以执行增加,删除,修改

    public class TestJDBC {
        public static void main(String[] args) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
     
            try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin");
                Statement s = c.createStatement();) {
     
                String sqlInsert = "insert into Hero values (null,'盖伦',616,100)";
                String sqlDelete = "delete from Hero where id = 100";
                String sqlUpdate = "update Hero set hp = 300 where id = 100";
     
                // 相同点:都可以执行增加,删除,修改
     
                s.execute(sqlInsert);
                s.execute(sqlDelete);
                s.execute(sqlUpdate);
                s.executeUpdate(sqlInsert);
                s.executeUpdate(sqlDelete);
                s.executeUpdate(sqlUpdate);
     
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
        }
    }

    不同点:

    不同1:
    execute可以执行查询语句
    然后通过getResultSet,把结果集取出来
    executeUpdate不能执行查询语句
    不同2:
    execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等
    executeUpdate返回的是int,表示有多少条数据受到了影响

    public class TestJDBC {
        public static void main(String[] args) {
      
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
     
            try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin");
                Statement s = c.createStatement();) {
      
                // 不同1:execute可以执行查询语句
                // 然后通过getResultSet,把结果集取出来
                String sqlSelect = "select * from hero";
      
                s.execute(sqlSelect);
                ResultSet rs = s.getResultSet();
                while (rs.next()) {
                    System.out.println(rs.getInt("id"));
                }
      
                // executeUpdate不能执行查询语句
                // s.executeUpdate(sqlSelect);
      
                // 不同2:
                // execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等
                boolean isSelect = s.execute(sqlSelect);
                System.out.println(isSelect);
      
                // executeUpdate返回的是int,表示有多少条数据受到了影响
                String sqlUpdate = "update Hero set hp = 300 where id < 100";
                int number = s.executeUpdate(sqlUpdate);
                System.out.println(number);
      
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      
        }
    }

    原文地址:http://how2j.cn/k/jdbc/jdbc-execute/389.html#nowhere

  • 相关阅读:
    加密的PDF文件怎么查看
    SQL Server数据库中怎么加密数据
    虚拟磁盘也加密
    怎么用密码保护您的文件夹
    如何用cmd命令加密文件夹
    快压轻松创建加密压缩包
    GPS学习的一些链接
    【最新软件预告】邮件群发器Console版
    第六格很酷的滚动活动会员条
    邀请您加入移动开发专家联盟
  • 原文地址:https://www.cnblogs.com/churujianghudezai/p/11441171.html
Copyright © 2020-2023  润新知