Java 實現Dbhelper 支援大資料增刪改
引言
在做項目的時候,技術選型很重要,在底層的方法直接影響了我們對大資料訪問以及修改的速度,在Java中有很多優秀的ORM架構,比如說:JPA,Hibernate 等等,正如我們所說的,架構有架構的好處,當然也存在一些可以改進的地方,這個時候,就需要我們針對於不同的業務不同的需求,不同的訪問量,對底層的架構重新封裝,來支援大資料增刪改。
代碼:
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; //要傳入的sql語句 public void setSql(String sql) {this.sql = sql;} private List sqlValues; //sql語句的參數public void setSqlValues(List sqlValues) {this.sqlValues = sqlValues;}private List<list> sqlValue; //sql語句的參數public void setSqlValue(List<list> sqlValues) {this.sqlValue = sqlValues;}private Connection con; //連線物件public void setCon(Connection con) {this.con = con;}public Dbhelper(){this.con=getConnection(); //給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"); //載入資料庫設定檔到記憶體中 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-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con; } /** * 關閉資料庫 * @param con * @param pst * @param rst */ private void closeAll(Connection con,PreparedStatement pst,ResultSet rst){ if(rst!=null){ try {rst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } if(pst!=null){ try {pst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } if(con!=null){ try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } } /** * 關閉資料庫 * @param con * @param pst * @param rst */ private void closeAll(Connection con,Statement pst,ResultSet rst){ if(rst!=null){ try {rst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } if(pst!=null){ try {pst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } if(con!=null){ try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } } /** * 尋找 * @param sql * @param sqlValues * @return */ public Result executeQuery(){ Result result=null; ResultSet rst=null; PreparedStatement pst=null; try { pst=con.prepareStatement(sql);if(sqlValues!=null&&sqlValues.size()>0){ //當sql語句中存在預留位置時setSqlValues(pst,sqlValues);} rst=pst.executeQuery(); result=ResultSupport.toResult(rst); //一定要在關閉資料庫之前完成轉換} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{this.closeAll(con, pst, rst);} return result; } /** * 增刪改 * @return */ public int executeUpdate(){ int result=-1; PreparedStatement 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-generated catch blocke.printStackTrace();}finally{this.closeAll(con, pst, null);} return result; } /** * 使用PreparedStatement加批量的方法 * @return */ public int[] executeUpdateMore(){ int[] result=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="" catch="" sqlexception="" todo="" auto-generated="" block="" return="" strvalue:="" insert="" throws="" public="" result="null;" statement="" stmt="con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE," sqlstring="null;" string="" strvalue="" :="" boolean="" booleanautocommit="false;" exception="" param="" pst="" sqlvalues="" private="" void="" preparedstatement="" list="" int="" i="0;i<sqlValues.size();i++){" pre="">我們的在db.properties中寫入訪問資料庫的資訊:<p></p><p></p><p></p><pre class="brush:java;">driver_class=com.mysql.jdbc.Driverdriver_url=jdbc:mysql://192.168.22.246:3306/importexceltestdatabase_user=basicdatabase_password=basic</pre><p></p><p>測試:</p><p></p><p></p><pre class="brush:java;">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();}}</list></pre></p></sqlvaluestring.size();i++){></list></list>