java导入 .sql文件到数据库

1.链接数据库类

import java.sql.*;
import java.util.List;

/**
 * @author hao
 * @Description:
 * @date 2021/11/19下午1:34
 */
public class DBConnection {
/*
 con= DriverManager.getConnection("jdbc:mysql://10.30.10.124:3306/sati_code","root","123456" );
 可以配动态
 例如:
 @value("${sss.xxxx.xxx.xx}")
 private String url
*/
    public  Connection getConnection(){
        Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://10.30.10.124:3306/sati_code","root","123456" );
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到数据库驱动");
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
        return con;
    }

    public static void close(Connection con){
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                System.out.println("数据库关闭异常");
            }
        }
    }
    public boolean executeBatchByStatement(List<String> sqlList) {
        Connection conn=null;
        boolean ret = false;
        if (conn != null) {
            Statement stmt = null;
            try {
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                for (String s : sqlList) {
                    stmt.addBatch(s);
                }
                stmt.executeBatch();
                conn.commit();
                ret = true;
            } catch (BatchUpdateException e) {
                e.printStackTrace();
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.setAutoCommit(true);
                    if (stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }


}

2.SQL工具类

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.stereotype.Component;

import java.io.*;
import
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值