Springboot 调用mysql的.sql文件,执行mysql语句

2 篇文章 0 订阅

代码:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.sql.*;

@Component
public class ExecuteSQLUtil {
    @Value("${master.jdbc.url}")
    private String DB_URL;
    @Value("${spring.datasource.druid.username}")
    private String DB_USERNAME;
    @Value("${spring.datasource.druid.password}")
    private String DB_PWD;

    @Scheduled(cron = "0 */1 * * * ?")
    public void test (){
        executeSql("static/test1.sql");
        executeSql("static/test2.sql");
        executeSql("static/test3.sql");
        executeSql("static/test4.sql");
        executeSql("static/test5.sql");
        executeSql("static/test6.sql");
        executeSql("static/testOne.sql");
    }



    public Connection executeSql(String sqlFileName){
        Connection connection = null;
        try {
            String driverClassName = "com.mysql.cj.jdbc.Driver";
// String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=true&rewriteBatchedStatements=true";
// String DB_USERNAME = "root";
// String DB_PWD = "123456";
//            Class.forName(driverClassName);
            connection = DriverManager.getConnection(DB_URL, DB_USERNAME , DB_PWD );
        } catch (Exception e) {
            e.printStackTrace();
        }
        ClassPathResource rc = new ClassPathResource(sqlFileName);
        EncodedResource er = new EncodedResource(rc, "utf-8");
        ScriptUtils.executeSqlScript(connection, er);
        return connection;
    }
}

sql文件在:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java执行SQL文件并将上亿条INSERT语句插入MySQL可以采用以下步骤: 1. 将SQL文件读取到Java程序。 2. 将SQL文件的所有INSERT语句拆分成单个语句。 3. 将单个INSERT语句批量执行,使用JDBC的批量处理功能可以大幅提高性能。可以使用PreparedStatement和addBatch方法实现。 4. 在批量执行完所有INSERT语句后,需要调用executeBatch方法提交事务。 以下是一个简单的示例代码: ```java try { // 读取SQL文件 BufferedReader reader = new BufferedReader(new FileReader("file.sql")); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line); } reader.close(); // 拆分INSERT语句 String[] insertStatements = stringBuilder.toString().split(";"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "username", "password"); PreparedStatement preparedStatement = connection.prepareStatement(""); // 批量执行INSERT语句 for (String insertStatement : insertStatements) { preparedStatement.addBatch(insertStatement); } preparedStatement.executeBatch(); connection.commit(); } catch (IOException | SQLException e) { e.printStackTrace(); } ``` 需要注意的是,这个代码示例仅仅是一个简单的示例,实际应用还需要考虑以下问题: 1. SQL文件可能包含多种类型的语句,需要进行语句类型的判断。 2. 在批量执行INSERT语句时,需要注意内存消耗和数据库连接池的使用。 3. 在执行过程需要考虑异常处理和事务控制。 4. 对于大数据量的插入操作,可能需要采用多线程或分布式处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值