Optimizing database inserts java -


i relatively new java , database , therefore asking code optimization. have around 20 text files comma separated values.each text files has around 10000 lines based on the 3rd value in each line, insert data different tables. each time check 3rd value , use different methods save data. code follows. please tell me if proper way operation. in advance.

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();     (file file : dir.listfiles()) {         bufferedreader br;         try {             br = new bufferedreader(new filereader(file));             while ((currentline = br.readline()) != null) {                 list<string> 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<string> values, connection connection) throws sqlexception {     // todo auto-generated method stub     string write_data = "insert 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 db, error: " + e.getmessage());     }     return; } 

  1. don't create database connection in loop. expensive operation , should create once.
  2. don't create preparedstatement in loop. create once , reuse it.
  3. don't commit after every single insert. read using batches inserting. reduces "commit-overhead" dramatically if make commit every let's 200 inserts.

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -