zoukankan      html  css  js  c++  java
  • Java自学-JDBC execute与executeUpdate的区别

    JDBC中 execute与executeUpdate的区别

    execute与executeUpdate的区别

    步骤 1 : 相同点

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

    package jdbc;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    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();
            }
     
        }
    }
    

    步骤 2 : 不同点

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

    package jdbc;
      
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
      
    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();
            }
      
        }
    }
    

    更多内容,点击了解: JDBC中 execute与executeUpdate的区别

  • 相关阅读:
    Glide加载网络图片与本地图片尺寸不一致
    android BLE 40 setCharacteristicNotification接收不到数据
    Android中颜色透明度对应16进制值
    模拟器不能运行 Failed to start emulator: Cannot run program "/home/kroaity/Downloads/android-sdk-linux//tools/emulator": error=2
    android SDK Manager 代理服务器设置
    if the parser found inconsistent certificates on the files in the .apk.104
    win7自带桌面便签
    unable to connect to the virtual device Genymotion 神器启动问题
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    获得root权限system/app下文件无法删除
  • 原文地址:https://www.cnblogs.com/jeddzd/p/13340650.html
Copyright © 2011-2022 走看看