Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何分别插入每个值_Java_Arraylist - Fatal编程技术网

Java 如何分别插入每个值

Java 如何分别插入每个值,java,arraylist,Java,Arraylist,我需要你的帮助在列表中插入每个值 private List<String> selectedCertificate = new ArrayList<String>(); 这将有助于: (String certificate: certificates) { preparedStatement.setInt(1, 50); preparedStatement.setString(2, selectedCertificate); } 也许那样的事情应该可以 St

我需要你的帮助在列表中插入每个值
private List<String> selectedCertificate = new ArrayList<String>();
这将有助于:

 (String certificate: certificates) 
{   
preparedStatement.setInt(1, 50);
preparedStatement.setString(2, selectedCertificate);
}

也许那样的事情应该可以

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());

}

那你有什么问题?你知道如何写循环吗?只要一个while循环,它就会解决这个问题。你可以使用while循环,但是for循环可能更容易使用。与字符串证书类似:证书{/*对证书*/}执行某些操作可能重复,或者此链接可能会有所帮助
 (String certificate: certificates) 
{   
preparedStatement.setInt(1, 50);
preparedStatement.setString(2, selectedCertificate);
}
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());

}