0

I need to insert info on a database using Java. It's about Information Retrieval, I have a collection of 1400 documents each of them containing: ID, Title, Author, Area, Abstract. I have already organized the collection into 5 separate String arrays eliminating unnecesary characters and multiple spaces. What can I do to store this data into a table called Documents within my collection database? I'm thinking about something like...

for(int i=0; i<=1400; i++){
    user.execute("INSERT INTO Documents (idDoc, title, author, area,abstract)")
    VALUES("+ID[i]+","+Title[i]+","+Author[i]+","+Area[i]+","+Abstract[i]");
}

Is this the correct syntax? Is there an easier way to do it? Any help would be very appreciated.

PS: I'm adding this before that loop:

String url = "jdbc:mysql//localhost:3306/database";
Class.forName("com.mysql.jdbc.Driver").newInstance(); // register driver.
Connection con = DriverManager.getConnection(url, "root", "root");
Statement user = con.createStatement();

1 Answer 1

1

Use JDBC PreparedStatement batch update. Take a look at following sites. http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html

dbConnection.setAutoCommit(false);
String insertTableSQL = "INSERT INTO Documents (idDoc, title, author, area,abstract) VALUES "
        "(?,?,?,?,?)";              
PreparedStatement = dbConnection.prepareStatement(insertTableSQL);

for(int i=0; i<=ID.length; i++){
   preparedStatement.setInt(1, ID[i]);
   preparedStatement.setString(2, Title[i]);
   preparedStatement.setString(3, Author[i]);
   preparedStatement.setString(4, Area[i]);
   preparedStatement.setString(5, Abstract[i]);
   preparedStatement.addBatch();
}

preparedStatement.executeBatch();

To be safe I would suggest you to create a Document Class and hold all these Documents record into a List before inserting into database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.