プロジェクトに取り組むとき、技術選択は非常に重要です。基礎となる方法は、アクセス速度とビッグデータの変更に直接影響します。 JPA、Hibernateなど、Javaには多くの優れたORMフレームワークがあります。私たちが言ったように、フレームワークにはフレームワークの利点があり、もちろん改善できる領域がいくつかあります。現時点では、ビッグデータの追加、削除、変更をサポートするために、さまざまなビジネスニーズとさまざまな訪問のために、基礎となるアーキテクチャを再カプセル化する必要があります。
コード:
java.io.*をインポートします。 Java.sql。*をインポートします。 java.util.logging.levelをインポートします。 java.util.logging.loggerをインポートします。 javax.servlet.jsp.jstl.sql。*; / ** * dbhelper * @author qmx * */ public class dbhelper {private string sql; // public void setSql(String SQL){this.sql = sql; }プライベートリストsqlvalues; // sqlステートメントのシステムパラメーターpublic void setsqlvalues(list sqlvalues){this.sqlvalues = sqlvalues; }プライベートリスト<list> sqlvalue; // sqlステートメントのシステムパラメーターpublic void setsqlvalue(list <list> sqlvalues){this.sqlvalue = sqlvalues; }プライベート接続CON; //接続オブジェクトpublic void setcon(connection con){this.con = con; } public dbhelper(){this.con = getConnection(); //接続オブジェクトに初期値を割り当てる}/***データベース接続を取得* @return*/private connection getConnection(){string driver_class = null;文字列driver_url = null;文字列database_user = null;文字列database_password = null; try {inputstream fis = this.getClass()。getResourceAsStream( "/db.properties"); //データベース構成ファイルをメモリプロパティにロードするp = new Properties(); P.Load(FIS); driver_class = p.getproperty( "driver_class"); //データベース構成を取得ファイル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-enerated catch block e.printstacktrace(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); } catch(filenotfoundexception e){// dodo auto-enerated catch block e.printstacktrace(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); } con; } / ** *データベースを閉じます * @param con * @param pst * @param rst * / private void closeall(connection con、preated -statement pst、resultset rst){if(rst!= null){try {rst.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(pst!= null){try {pst.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(con!= null){try {con.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(con!= null){try {con.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(con!= null){try {con.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}}} / ** *データベースを閉じる * @param con * @param pst * @param rst * / private void closeall(connection con、statement pst、results rst){if(rst!= null){try {rst.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(pst!= null){try {pst.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(pst!= null){try {pst.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(con!= null){try {con.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}} / ** * find * @param sql * @param sqlvalues * @return * / public result executequery(){result result = null;結果rst = null; reprentStatement pst = null; try {pst = con.preparestatement(sql); if(sqlvalues!= null && sqlvalues.size()> 0){//プレースホルダーがsqlステートメントに存在する場合、setsqlvalues(pst、sqlvalues); } rst = pst.executequery(); result = resultupport.toresult(rst); //データベースを閉じる前にコンバージョンを完了してください} catch(sqlexception e){// dodo auto-enerated catch block e.printstacktrace(); }最後に{this.closeall(con、pst、rst); } return result; } / ** *追加、削除、変更、 * @return * / public int executeUpdate(){int result = -1; reprentStatement pst = null; try {pst = con.preparestatement(sql); if(sqlvalues!= null && sqlvalues.size()> 0){//プレースホルダーがsqlステートメントsetsqlvalues(pst、sqlvalues)に存在する場合; } result = pst.executeUpdate(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }最後に{this.closeall(con、pst、null); } return result; } / *** batchesを追加する準備を使用したメソッド* @return* / public int [] executeUpdateMore(){int [] result = null; try {preatedStatement perst = 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-enerated catch block e.printstacktrace(); }} prest.addbatch(); } prest.executeBatch(); /* con.commit();*/ this.closeall(con、perst、null); } catch(sqlexception ex){logger.getLogger(dbhelper.class.getName())。log(level.severe、null、ex); } return result; } / ** * reparedStatementを使用してバッチメソッド、strvalue: * "Insertingadlogs(ip、webサイト、yyyymmdd、hour、object_id)値( '192.168.1.3'、 'localhost'、 '20081009'、8、 '23123') executeUpDateMorenotauto()throws sqlexception {int [] result = null; con.setautocommit(false);ステートメントstmt = con.createStatement(resultset.type_scroll_sensitive、resultSet.concur_read_only); string [] sqlstring = null; for(string strvalue:sqlstring){stmt.execute(strvalue); } con.commit();返品結果; } / ** * reparedStatementを使用してバッチメソッド、strvalue: * "Insertingadlogs(ip、webサイト、yyyymmdd、hour、object_id)値( '192.168.1.3'、 'localhost'、 '20081009'、8、 '23123')" sqlexception {//現在の自動提出モードBoolean Booleanautocommit = false; string [] sqlstring = null; int [] result = null; try {booleanautocommit = con.getautocommit(); //自動送信con.setautocommit(false)を閉じる;ステートメントstmt = con.createStatement(resultset.type_scroll_sensitive、resultSet.concur_read_only); //ステートメントを使用して、複数のSQLステートメントを同時に収集する/*stmt.addbatch(insert_sql1); stmt.addbatch(insert_sql2); stmt.addbatch(update_sql3); */ for(string strvalue:sqlstring){stmt.addbatch(strvalue); } //すべてのSQLステートメントを同時に送信しますstmt.executeBatch(); //修正con.commit()を送信します。 con.setautocommit(booleanautocommit); this.closeall(con、stmt、null); } catch(Exception e){e.printstacktrace(); con.rollback(); // SetAutoCommit(false)のセットは、キャッチ内の接続のロールバック操作を実行せず、操作の表がロックされ、データベースがデッドロックされます}戻り結果。 } / ** * sqlステートメントのプレースホルダーに値を割り当てます * @param pst * @param sqlvalues * / private void setsqlvalues(preated -statement pst、list sqlvalues){for(int i = 0; i <sqlvalues.size(); i ++){try {pst.setobject.get; } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}}}}} db.propertiesのデータベースにアクセスするための情報を書き込む:
driver_class = com.mysql.jdbc.driver driver_url = jdbc:mysql://192.168.22.246:3306/importexceltest database_user = basic database_password = basic
テスト:
Java.util。*をインポートします。パブリッククラスImportExcelTest {public static void main(string [] args){ /*dbhelper db = new dbhelper(); string sql = "tb_coursetype(id、coursetypename)values( '2012003'、 'qmx3')"; db.setsql(sql); db.executeupdate();* / /*dbhelper db1 = new dbhelper(); string sql1 = "tb_coursetype(id、coursetypename)values(?、?)に挿入します。リストsqlvalues = new ArrayList(); sqlvalues.add( "2012004"); sqlvalues.add( "qmx4"); db1.setsqlvalues(sqlvalues); db1.setsql(sql1); db1.executeupdate();*/ dbhelper db = new dbhelper(); string sql = "tb_coursetype(id、coursetypename)values(?、?)";リスト<list> sqlvalues = new ArrayList();リストsqlvaluestring = new ArrayList(); sqlvaluestring.add( "2012010"); sqlvaluestring.add( "qmx10"); sqlvalues.add(sqlvaluestring);リストsqlvaluestring1 = new ArrayList(); sqlvaluestring1.add( "2012011"); sqlvaluestring1.add( "qmx11"); sqlvalues.add(sqlvaluestring1);リストsqlvaluestring2 = new ArrayList(); sqlvaluestring2.add( "2012012"); sqlvaluestring2.add( "qmx12"); sqlvalues.add(sqlvaluestring2);リストsqlvaluestring3 = new ArrayList(); sqlvaluestring3.add( "2012013"); sqlvaluestring3.add( "qmx13"); sqlvalues.add(sqlvaluestring3); db.setsqlvalue(sqlvalues); db.setsql(sql); db.executeupdatemore(); }}