Java實(shí)現(xiàn)Dbhelper支持大數(shù)據(jù)增刪改
在做項(xiàng)目的時(shí)候,技術(shù)選型很重要,在底層的方法直接影響了我們對(duì)大數(shù)據(jù)訪問以及修改的速度,在Java中有很多優(yōu)秀的ORM框架,比如說:JPA,Hibernate 等等,正如我們所說的,框架有框架的好處,當(dāng)然也存在一些可以改進(jìn)的地方,這個(gè)時(shí)候,就需要我們針對(duì)于不同的業(yè)務(wù)不同的需求,不同的訪問量,對(duì)底層的架構(gòu)重新封裝,來支持大數(shù)據(jù)增刪改。
代碼:
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語句的參數(shù)
public void setSqlValues(List sqlValues) {
this.sqlValues = sqlValues;
}
private List<List> sqlValue; //sql語句的參數(shù)
public void setSqlValue(List<List> sqlValues) {
this.sqlValue = sqlValues;
}
private Connection con; //連接對(duì)象
public void setCon(Connection con) {
this.con = con;
}
public Dbhelper(){
this.con=getConnection(); //給Connection的對(duì)象賦初值
}
/**
* 獲取數(shù)據(jù)庫(kù)連接
* @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"); //加載數(shù)據(jù)庫(kù)配置文件到內(nèi)存中
Properties p=new Properties();
p.load(fis);
driver_class=p.getProperty("driver_class"); //獲取數(shù)據(jù)庫(kù)配置文件
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;
}
/**
* 關(guān)閉數(shù)據(jù)庫(kù)
* @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 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();
}
}
}
/**
* 關(guān)閉數(shù)據(jù)庫(kù)
* @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 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();
}
}
}
/**
* 查找
* @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){ //當(dāng)sql語句中存在占位符時(shí)
setSqlValues(pst,sqlValues);
}
rst=pst.executeQuery();
result=ResultSupport.toResult(rst); //一定要在關(guān)閉數(shù)據(jù)庫(kù)之前完成轉(zhuǎn)換
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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){ //當(dāng)sql語句中存在占位符時(shí)
setSqlValues(pst,sqlValues);
}
result=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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 {
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);
} catch (SQLException ex){
Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE, null,ex);
}
return result;
}
/**
* 使用PreparedStatement加批量的方法,strvalue:
* "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
* @return
* @throws SQLException
*/
public int[] executeUpdateMoreNotAuto() 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;
}
/**
* 使用PreparedStatement加批量的方法,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{
//保存當(dāng)前自動(dòng)提交模式
Boolean booleanautoCommit=false;
String[] SqlString= null;
int[] result= null;
try
{
booleanautoCommit=con.getAutoCommit();
//關(guān)閉自動(dòng)提交
con.setAutoCommit(false);
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//使用Statement同時(shí)收集多條sql語句
/*stmt.addBatch(insert_sql1);
stmt.addBatch(insert_sql2);
stmt.addBatch(update_sql3);*/
for(String strvalue : SqlString){
stmt.addBatch(strvalue);
}
//同時(shí)提交所有的sql語句
stmt.executeBatch();
//提交修改
con.commit();
con.setAutoCommit(booleanautoCommit);
this.closeAll(con, stmt, null);
}
catch(Exception e)
{
e.printStackTrace();
con.rollback(); //設(shè)定setAutoCommit(false)沒有在catch中進(jìn)行Connection的rollBack操作,操作的表就會(huì)被鎖住,造成數(shù)據(jù)庫(kù)死鎖
}
return result;
}
/**
* 給sql語句中的占位符賦值
* @param pst
* @param sqlValues
*/
private void setSqlValues(PreparedStatement pst,List sqlValues){
for(int i=0;i<sqlValues.size();i++){
try {
pst.setObject(i+1,sqlValues.get(i));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我們的在db.properties中寫入訪問數(shù)據(jù)庫(kù)的信息:
driver_class=com.mysql.jdbc.Driver driver_url=jdbc:mysql://192.168.22.246:3306/importexceltest database_user=basic database_password=basic
測(cè)試:
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();
}
}
相關(guān)文章
SpringBoot整合Solr實(shí)現(xiàn)文檔檢索
Solr高度可靠、可擴(kuò)展和容錯(cuò),提供分布式索引、復(fù)制和負(fù)載平衡查詢、自動(dòng)故障轉(zhuǎn)移和恢復(fù)、集中配置等,Solr 為世界上許多最大的 Internet 站點(diǎn)的搜索和導(dǎo)航功能提供支持,本文將給大家介紹SpringBoot整合Solr實(shí)現(xiàn)文檔檢索,需要的朋友可以參考下2023-08-08
Java實(shí)現(xiàn)Huffman編碼的示例代碼
Huffman編碼是一種編碼方式,本文主要介紹了Java實(shí)現(xiàn)Huffman編碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Java和C語言分別實(shí)現(xiàn)水仙花數(shù)及拓展代碼
這篇文章主要介紹了分別用Java和C語言實(shí)現(xiàn)水仙花數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題
今天小編就為大家分享一篇關(guān)于解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Mybatis Plus框架項(xiàng)目落地實(shí)踐分析總結(jié)
這篇文章主要為大家介紹了Mybatis Plus框架項(xiàng)目落地實(shí)踐分析總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
詳解Java中字符串緩沖區(qū)StringBuffer類的使用
StringBuffer與String類似,只不過StringBuffer在進(jìn)行字符串處理時(shí)不生成新的對(duì)象,下面我們就來詳解Java中字符串緩沖區(qū)StringBuffer類的使用:2016-06-06
Java利用讀寫的方式實(shí)現(xiàn)音頻播放代碼實(shí)例
這篇文章主要介紹了Java利用讀寫的方式實(shí)現(xiàn)音頻播放代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
MyBatis-Plus如何通過注解使用TypeHandler
這篇文章主要介紹了MyBatis-Plus如何通過注解使用TypeHandler,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

