chapter12_数据库编程_1_综述

  • 标准SQL语言是一种__非过程性__的数据库操作语言,不能实现控制流程、函数、子程序。

    因此,各个数据库软件都进行了扩展

  • 过程的类型

    (1) 批处理

    将一个或多个SQL语句作为一组,从应用程序发送到数据库服务器。每组将会编译为单个可执行单元

    MySQL添加批处理:在连接数据库URL时,设置参数rewriteBatchedStatements = true,另外还要使用addBatch和executeBatch

    示例

      public class Solution {
    
          private static String driverName = "com.mysql.jdbc.Driver";
          private static String jdbcURL = "jdbc:mysql://localhost:3306/temp?rewriteBatchedStatements=true";
          private static String databaseUserName = "acba";
          private static String databasePassword = "123456";
    
          private static String sql = "INSERT INTO temp VALUES (?, ?)";
    
          public static void main(String[] args) {
    
              Connection connection = null;
    
              try {
                  Class.forName(driverName);
                  connection = DriverManager.getConnection(jdbcURL, databaseUserName, databasePassword);
    
              } catch (ClassNotFoundException | SQLException e) {
                  e.printStackTrace();
              }
    
              PreparedStatement preparedStatement = null;
      
              try {
                  preparedStatement = connection.prepareStatement(sql);
          
                  // 插入10000条数据
                  for (int i = 0; i < 10000; i++) {
                      preparedStatement.setInt(1, i);
                      preparedStatement.setString(2, String.valueOf(i) + "_bc");
    
                      preparedStatement.addBatch();
                  }
    
                  long startTime = System.currentTimeMillis();
    
                  preparedStatement.executeBatch();
    
                  long endTime = System.currentTimeMillis();
    
                  System.out.println((endTime - startTime)/1000.0);
    
              } catch (SQLException e) {
                  e.printStackTrace();
              } 
          }
      }
    

    实测的结果是,添加了rewriteBatchedStatements = true之后,需要的时间约为0.1秒;不添加大概需要23秒

    (2) 存储过程和触发器

    (3) 脚本

    编写一个.sql文件,然后用mysql自带的Workbench导入执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值