gpt4 book ai didi

java - 将值列表插入 Mysql

转载 作者:行者123 更新时间:2023-12-01 06:39:35 27 4
gpt4 key购买 nike

如何将值列表插入 MySQL 表的列中。

这是我的项目:

public void settingAmount(List<String>lst)throws Exception{

// Accessing driver from jar files
Class.forName("com.mysql.jdbc.Driver");
// here we create a variable for the connection called con
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ozon","root","root");
// here we create our query
Statement stmt = (Statement) con.createStatement();
//performing insert operation :)
lst.toArray(ss);

for(String ins:ss){
double d=Double.parseDouble(ins);
String insert = "INSERT INTO storage_other(total) VALUES ("+ins+");";
//String insert = "INSERT INTO storage_other(amount) VALUES ("+ls+");";
// Here we are going to Execute the query
stmt.executeUpdate(insert);
System.out.print("Done Seccesfully :)");
}
}

最佳答案

您想要做的是使用批处理。批处理允许您发送要同时完成的语句列表。

这是一个例子

connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement("INSERT INTO storage_other(total) VALUES (?)");
for (String ins:ss){
ps.setObject(1, d);
ps.addBatch();
}
ps.executeBatch();
connection.commit();

这将比任何带有索引的表上的单独插入快得多。

关于java - 将值列表插入 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com