我需要你的帮助才能在列表中插入每个值
private List<String> selectedCertificate = new ArrayList<String>();
所选证书的值为[&#34; A&#34;,&#34; B&#34;,&#34; C&#34;]我想在单独的行中插入每个项目相同的id(ex.50)
Record No.1 50 A Record No.1 50 B Record No.1 50 C
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String insertTableSQL = "INSERT INTO temp"
+ "(USER_ID, Certificate_Code) VALUES"
+ "(?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 50);
preparedStatement.setString(2, selectedCertificate);
// execute insert SQL stetement
preparedStatement.executeUpdate();
System.out.println("Record is inserted");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
这会有所帮助:
(String certificate: certificates)
{
preparedStatement.setInt(1, 50);
preparedStatement.setString(2, selectedCertificate);
}
答案 0 :(得分:0)
也许这样的事情应该没问题
String insertTableSQL = "INSERT INTO temp"
+ "(USER_ID, Certificate_Code) VALUES"
+ "(?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(insertTableSQL);
for(String certif : selectedCertificate) {
preparedStatement.setInt(1, 50);
preparedStatement.setString(2, certif);
preparedStatement.addBatch();
}
// execute insert SQL stetement
preparedStatement.executeUpdate();
dbConnection.commit();
System.out.println("Record is inserted");
} catch (SQLException e) {
System.out.println(e.getMessage());
}