java将值列表插入Mysql
如何将值列表插入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 :)");
}
}
# 1 楼答案
这是我用来在Oracle SQL数据库中插入一些数据的方法
您可以准备
insert
语句并调用此函数来执行操作。使用连接对象和查询调用它。如果出现问题,应在完成时返回true
。如果要记录任何错误,请使用e.getMessage()
在异常块中以字符串形式获取错误消息如评论中所述,尝试使用
PreparedStatement
对象来避免SQL注入攻击,并尝试修剪数据中可能存在的任何'