how to set arrayList with string objects to a prepared statement
I have a java ArrayList of a datastructure (string name, string email). The task is, to save the list in the database. I have an approach in my mind to use a for loop and use sta开发者_JAVA百科tement.setString( i, name) etc. Is there any better solution. Thanks in advance.
Since you're going to use plain JDBC, a batch statement might be useful to you:
PreparedStatement stmt = connection.prepareStatement(
"INSERT INTO xx (name, email) VALUES (?, ?)");
for (MyStructure s : list) {
stmt.setString(1, s.name);
stmt.setString(2, s.email);
stmt.addBatch();
}
stmt.executeBatch();
So the idea you already have seems good to me. You'll find more info here:
http://java.sun.com/developer/Books/JDBCTutorial/
精彩评论