IEDA 创建java web 程序
mysql 数据库链接
package com.example.demo.DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @创建人 jiaya
* @创建时间 2021/11/1616:24
* @作者联系方式:Jyanyong56@163.com
*/
public class DataBase {
private static String URL = "jdbc:mysql://IP:port/databsase"; // CAPTION 数据库地址
private static String userName = name;
private static String password = password;
private static String Driver = "com.mysql.cj.jdbc.Driver";
private static Connection connection = null;
public DataBase(){}
public static Connection getConnection(){
if(connection == null){
try{
Class.forName(Driver);
connection = DriverManager.getConnection(URL, userName, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return connection;
}
}
java线程
package com.example.demo.Controller;
import com.example.demo.DAO.DataBase;
import javax.xml.stream.FactoryConfigurationError;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* @创建人 jiaya
* @创建时间 2021/11/1712:48
* @作者联系方式:Jyanyong56@163.com
*/
public class InsertThread extends DataBase implements Runnable{
private Thread t;
private String threadName;
Connection connection = null;
String staticetis_sql = "INSERT INTO rain VALUES"; // CAPTION SQL命令
private ArrayList<String> valuesThread = new ArrayList<>(); // CAPTION SQL数据
public InsertThread(String name, ArrayList<String> values) throws SQLException {
this.threadName = name;
connection = getConnection();
connection.setAutoCommit(false);
System.out.println("线程" + threadName);
this.valuesThread.addAll(values);
}
@Override
public void run() {
for(int i=0; i< valuesThread.size()-1;i++){
staticetis_sql = staticetis_sql + valuesThread.get(i) + ",";
}
staticetis_sql = staticetis_sql + valuesThread.get(valuesThread.size() - 1);
System.out.println(staticetis_sql);
try {
PreparedStatement ps = (PreparedStatement) connection.prepareStatement(staticetis_sql);
ps.execute();
connection.commit(); // CAPTION sql事务提交
} catch (SQLException e) {
e.printStackTrace();
}
}
public void start(){
if(t==null){
t = new Thread(this, threadName);
t.start();
}
}
}
链接
链接:https://pan.baidu.com/s/1tl3nG6xq_iof4VK14tUxgQ
提取码:1h2g
–来自百度网盘超级会员V4的分享