Java implementation DBHelper support large data deletion and modification _java

Source: Internet
Author: User

In the project, the technology selection is very important, at the bottom of the method directly affect our access to large data and the speed of modification, in Java there are many excellent ORM framework, such as: Jpa,hibernate and so on, as we said, the framework has the benefits of the framework, Of course there are some can improve the place, this time, we need for different business needs, different traffic, to the underlying structure of the encapsulation, to support large data-adding and pruning.

Code:

Import java.io.*; 
Import java.sql.*; 
Import java.util.*; 
Import Java.util.logging.Level; 
 
Import Java.util.logging.Logger; 
 
Import javax.servlet.jsp.jstl.sql.*;  /** * DBHelper * @author QMX * */public class DBHelper {private String sql; 
The SQL statement to be passed in is public void SetSQL (String sql) {this.sql = SQL; Private List sqlvalues; 
The parameter of the SQL statement is public void setsqlvalues (List sqlvalues) {this.sqlvalues = sqlvalues; Private list<list> Sqlvalue; 
The parameter of the SQL statement is public void Setsqlvalue (list<list> sqlvalues) {this.sqlvalue = sqlvalues; Private Connection con; 
Connection object public void Setcon (Connection con) {this.con = con; 
   Public DBHelper () {this.con=getconnection ()///To Connection object assigns an initial value}/** * Get database connection * @return 
    * * Private Connection getconnection () {String driver_class=null; 
    String Driver_url=null; 
    String Database_user=null; 
    String Database_password=null; 
      try {InputStream Fis=this.getclass (). getResourceAsStream ("/db.properties"); 
      Load the database configuration file into memory Properties P=new properties (); 
       
      P.load (FIS);   Driver_class=p.getproperty ("Driver_class"); 
      Gets the database configuration file Driver_url=p.getproperty ("Driver_url"); 
      Database_user=p.getproperty ("Database_user"); 
   
       
      Database_password=p.getproperty ("Database_password"); 
      Class.forName (Driver_class); 
       
       
    Con=drivermanager.getconnection (Driver_url,database_user,database_password); 
    catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); 
    catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
    catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); 
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); 
  return con; } 
   
  
   
 
  /** * Close Database * @param con * @param PST * @param rst/private void CloseAll (Connection con,preparedst 
      Atement Pst,resultset rst) {if (rst!=null) {try {rst.close (); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
      } if (Pst!=null) {try {pst.close (); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
      } if (Con!=null) {try {con.close (); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * Close database * @param con * @param PST * @param rst/private VO 
      ID closeall (Connection con,statement Pst,resultset rst) {if (rst!=null) {try {rst.close (); catch (SQLException e) {//TODO auto-generated catch blocK E.printstacktrace (); 
      } if (Pst!=null) {try {pst.close (); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
      } if (Con!=null) {try {con.close (); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); /** * Find * @param SQL * @param sqlvalues * @return * * Public Resul 
    T ExecuteQuery () {result result=null; 
    ResultSet Rst=null; 
    PreparedStatement Pst=null; 
      try {pst=con.preparestatement (SQL); 
      if (Sqlvalues!=null&&sqlvalues.size () >0) {//When a placeholder exists in the SQL statement setsqlvalues (pst,sqlvalues); 
    } rst=pst.executequery (); Result=resultsupport.toresult (RST); Be sure to complete the conversion before closing the database} catch (SQLException e) {//TODO auto-generated catch block e.printstacktrAce (); 
    }finally{This.closeall (Con, PST, RST); 
  return result; 
    /** * additions and deletions * @return * * * public int executeupdate () {int result=-1; 
    PreparedStatement Pst=null; 
      try {pst=con.preparestatement (SQL); 
      if (Sqlvalues!=null&&sqlvalues.size () >0) {//When a placeholder exists in the SQL statement setsqlvalues (pst,sqlvalues); 
    } result=pst.executeupdate (); 
    catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
    }finally{This.closeall (con, PST, NULL); 
  return result; /** * Use PreparedStatement plus Batch method * @return * * Public int[] Executeupdatemore () {int[] re    
    Sult=null; try{preparedstatement prest =con.preparestatement (sql,resultset.type_scroll_sensitive,resultset.concur_read_only 
       ); 
     for (List Sqlvaluestring:sqlvalue) {for (int i=0;i<sqlvaluestring.size (); i++) {     try {prest.setobject (I+1,sqlvaluestring.get (i)); 
          catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
       } prest.addbatch ();  
     } prest.executebatch (); 
    /* CON.COMMIT ()/* This.closeall (con, prest, null);   
    The catch (SQLException ex) {Logger.getlogger (Dbhelper.class.getName ()). log (Level.severe, Null,ex); 
     
  return result; /** * Using PreparedStatement plus batch method, strvalue: * "INSERT intoadlogs (ip,website,yyyymmdd,hour,object_id) VALUE S (' 192.168.1.3 ', ' localhost ', ' 20081009 ', 8, ' 23123 ') "* @return * @throws SQLException/public int[] Execute 
    Updatemorenotauto () throws sqlexception{int[] result =null;   
    Con.setautocommit (FALSE);    
    Statement stmt =con.createstatement (resultset.type_scroll_sensitive, resultset.concur_read_only);String[] sqlstring= null;   
    for (String strvalue:sqlstring) {stmt.execute (strvalue);  
    } con.commit (); 
  return result; /** * Using PreparedStatement plus batch method, strvalue: * "INSERT intoadlogs (ip,website,yyyymmdd,hour,object_id VALUES (' 192.168.1.3 ', ' localhost ', ' 20081009 ', 8, ' 23123 ') "* @return * @throws SQLException/public int[] 
    Executemorenotauto () throws sqlexception{//Save the current autocommit mode Boolean booleanautocommit=false; 
    String[] sqlstring= null; 
     Int[] result= null; 
       try {booleanautocommit=con.getautocommit (); 
      Turn off autocommit con.setautocommit (false);  
      Statement stmt =con.createstatement (resultset.type_scroll_sensitive, resultset.concur_read_only); 
      Use statement to collect multiple SQL statements at the same time/*stmt.addbatch (INSERT_SQL1); 
      Stmt.addbatch (INSERT_SQL2); Stmt.addbatch (UPDATE_SQL3); */for (String strvalue:sqlstring) {STMT.ADDBAtch (strvalue); 
       ()//Submit all SQL statements Stmt.executebatch () at the same time; 
       Submit modification Con.commit (); 
       Con.setautocommit (Booleanautocommit); 
     This.closeall (con, stmt, null); 
      catch (Exception e) {e.printstacktrace ();  Con.rollback (); 
  Set Setautocommit (false) does not carry on the connection rollback operation in the catch, the Operation table will be locked, causes the database deadlock} return results; /** * Assigns a value to a placeholder in an SQL statement * @param PST * @param sqlvalues/private void Setsqlvalues (PREPA Redstatement pst,list sqlvalues) {for (int i=0;i<sqlvalues.size (); i++) {try {pst.setobject (i+1,sql 
      Values.get (i)); 
      catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();  } 
    } 
  } 
     
}

Our information in Db.properties is written to access the database:

Driver_class=com.mysql.jdbc.driver 
driver_url=jdbc:mysql://192.168.22.246:3306/importexceltest 
Database_user=basic 
Database_password=basic 

Test:

Import java.util.*; 
    public class Importexceltest {public static void main (string[] args) {/*dbhelper db = new DBHelper (); 
    String sql = "INSERT into Tb_coursetype (id,coursetypename) VALUES (' 2012003 ', ' qmx3 ')"; 
    Db.setsql (SQL); 
    Db.executeupdate (); * */*dbhelper DB1 = new DBHelper (); 
    String SQL1 = "INSERT into Tb_coursetype (id,coursetypename) VALUES (?,?)"; 
    List sqlvalues = new ArrayList (); 
    Sqlvalues.add ("2012004"); 
    Sqlvalues.add ("qmx4"); 
    Db1.setsqlvalues (sqlvalues); 
    Db1.setsql (SQL1); 
    Db1.executeupdate (); */DBHelper db = new DBHelper (); 
    String sql = "INSERT into Tb_coursetype (id,coursetypename) VALUES (?,?)"; 
    list<list> sqlvalues = new ArrayList (); 
    List sqlvaluestring =new ArrayList (); 
    Sqlvaluestring.add ("2012010"); 
    Sqlvaluestring.add ("qmx10"); 
    Sqlvalues.add (sqlvaluestring); 
    List sqlValueString1 =new ArrayList (); 
    Sqlvaluestring1.add ("2012011"); SqlvaLuestring1.add ("qmx11"); 
    Sqlvalues.add (SQLVALUESTRING1); 
    List sqlValueString2 =new ArrayList (); 
    Sqlvaluestring2.add ("2012012"); 
    Sqlvaluestring2.add ("qmx12"); 
    Sqlvalues.add (SQLVALUESTRING2); 
    List sqlValueString3 =new ArrayList (); 
    Sqlvaluestring3.add ("2012013"); 
    Sqlvaluestring3.add ("qmx13"); 
    Sqlvalues.add (SQLVALUESTRING3); 
    Db.setsqlvalue (sqlvalues); 
    Db.setsql (SQL); 
     
  Db.executeupdatemore ();  } 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.