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