Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing database inserts java
    primarykey
    data
    text
    <p>I am relatively new to java and database and therefore asking your help for my code optimization. I have around 20 text files with comma separated values.Each text files has around 10000 lines Based on the the 3rd value in each line, I insert the data into different tables. Each time I check the 3rd value and use different methods to save this data. My code is as follows. Could someone please tell me if this is the proper way to do this operation. Thanks in advance.</p> <pre><code>public void readSave() throws SQLException { File dir = new File("C:\\Users\\log"); String url = Config.DB_URL; String user= Config.DB_USERNAME; String password= Config.DB_PASSWORD; con= DriverManager.getConnection(url, user, password); con.setAutoCommit(false); String currentLine; if (!dir.isDirectory()) throw new IllegalStateException(); for (File file : dir.listFiles()) { BufferedReader br; try { br = new BufferedReader(new FileReader(file)); while ((currentLine = br.readLine()) != null) { List&lt;String&gt; values = Arrays.asList(currentLine.split(",")); if (values.get(2).contentEquals("0051")) save0051(values,con); else if(values.get(2).contentEquals("0049")) save0049(values,con); else if(values.get(2).contentEquals("0021")) save0021(values,con); else if(values.get(2).contentEquals("0089")) save0089(values,con); if(statement!=null) statement.executeBatch(); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { con.commit(); statement.close(); con.close(); } catch (Exception e) {} } private void save0051(List&lt;String&gt; values, Connection connection) throws SQLException { // TODO Auto-generated method stub String WRITE_DATA = "INSERT INTO LOCATION_DATA" + "(loc_id, timestamp, message_id" + ) VALUES (?,?,?)"; try { statement = connection.prepareStatement(WRITE_DATA); statement.setString(1, values.get(0)); statement.setLong(2, Long.valueOf(values.get(1))); statement.setInt(3, Integer.valueOf(values.get(2))); statement.addBatch(); } catch (SQLException e) { e.printStackTrace(); System.out.println("Could not save to DB, error: " + e.getMessage()); } return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload