Java中MySql的DBHelper類_第1頁
Java中MySql的DBHelper類_第2頁
Java中MySql的DBHelper類_第3頁
Java中MySql的DBHelper類_第4頁
Java中MySql的DBHelper類_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、-作者xxxx-日期xxxxJava中MySql的DBHelper類【精品文檔】package hzvtc.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.ArrayList;import mons.dbutils.DbUtils;import mons.dbutils.QueryRunner;import mons.dbutils.ResultSetHandler;import mons.dbutils.handlers.Be

2、anHandler;import mons.dbutils.handlers.BeanListHandler;import mons.logging.Log;import mons.logging.LogFactory;public class DbHelper private String dbms; / 數(shù)據(jù)庫類型private String dbmsDriver; / 數(shù)據(jù)庫驅(qū)動(dòng)private String host; / 主機(jī)private String port; / 端口private String db; / 數(shù)據(jù)庫名private String user; / 用戶名priva

3、te String password; / 密碼private Class beanClass = null;private Connection conn = null;private final Log logger = LogFactory.getLog(DbHelper.class);sql.Driver; else if (dbms.equalsIgnoreCase(mysql) dbmsDriver = org.gjt.mm.mysql.Driver;/* 根據(jù)數(shù)據(jù)庫類型設(shè)置端口* param dbms 數(shù)據(jù)庫類型*/private void fillDbmsPort(String

4、 dbms) if (dbms.equalsIgnoreCase(postgresql) port = 5432; else if (dbms.equalsIgnoreCase(mysql) port = 3306;/* 設(shè)置數(shù)據(jù)連接時(shí)需要的參數(shù)* param dbms 數(shù)據(jù)庫類型* param host 主機(jī)* param db 數(shù)據(jù)庫名* param user 用戶名* param password 密碼*/public DbHelper(String dbms, String host, String db, String user, String password) this.dbms

5、 = dbms; fillDbmsDriver(dbms); this.host = host; fillDbmsPort(dbms); this.db = db; this.user = user; this.password = password;/* 設(shè)置數(shù)據(jù)連接時(shí)需要的參數(shù)* param dbms 數(shù)據(jù)庫類型* param host 主機(jī)* param port 端口* param db 數(shù)據(jù)庫名* param user 用戶* param password 密碼*/public DbHelper(String dbms, String host, String port, Strin

6、g db, String user, String password) this.dbms = dbms; fillDbmsDriver(dbms); this.host = host; this.port = port; this.db = db; this.user = user; this.password = password;/* 加載驅(qū)動(dòng)*/public void connect() if (conn != null) return; String strConn = String.format(jdbc:%s:/%s:%s/%s, dbms, host, port, db); D

7、bUtils.loadDriver(dbmsDriver); try conn = DriverManager.getConnection(strConn, user, password); catch (SQLException e) logger.error(Database connection failed!); logger.error(e.getMessage(); /* 關(guān)閉Connection*/public void close() try DbUtils.close(conn); conn = null; catch (SQLException e) logger.erro

8、r(Database close failed!); logger.error(e.getMessage(); /* 根據(jù)無參sql語句進(jìn)行查詢,并返回一個(gè)對(duì)象,用于單條記錄的查詢* param sql * return Object*/public Object query(String sql) if (conn = null | beanClass = null) return null; QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanHandler(beanClass); Object result

9、= null; try result = run.query(conn, sql, h); catch (SQLException e) (SQLException in + sql + ); logger.error(e.getMessage(); return result;/* 根據(jù)有參sql語句進(jìn)行查詢,并返回一個(gè)對(duì)象,用于單條記錄的查詢* param sql * param args * return Object*/public Object query(String sql, Object. args) if (conn = null | beanClass

10、 = null) return null; QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanHandler(beanClass); Object result = null; try result = run.query(conn, sql, args, h); catch (SQLException e) (SQLException in + sql + ); logger.error(e.getMessage(); return result;/* 根據(jù)無參sql語句進(jìn)行查詢,并返回一

11、個(gè)數(shù)據(jù)集,用于多條記錄的查詢* param sql* return ArrayList*/public ArrayList queryRecords(String sql) if (conn = null | beanClass = null) return null; QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanListHandler(beanClass); ArrayList result = null; try result = (ArrayList) run.query(conn, sql, h);

12、 / obtain the quaried records catch (SQLException e) (SQLException in + sql + ); logger.error(e.getMessage(); return result;/* 根據(jù)有參sql語句進(jìn)行查詢,并返回一個(gè)數(shù)據(jù)集,用于多條記錄的查詢* param sql* param args* return*/public ArrayList queryRecords(String sql, Object. args) if (conn = null | beanClass = null) retur

13、n null; QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanListHandler(beanClass); ArrayList result = null; try result = (ArrayList) run.query(conn, sql, args, h); System.out.println(result.size(); / obtain the quaried records catch (SQLException e) (SQLException in + sql +

14、 ); logger.error(e.getMessage(); return result;/* 根據(jù)無參sql語句進(jìn)行數(shù)據(jù)更新,并返回更新后影響的記錄數(shù)* param sql* return int*/public int update(String sql) if (conn = null) return 0; QueryRunner run = new QueryRunner(); int result = 0; try result = run.update(conn, sql); catch (SQLException e) (SQLException in

15、+ sql + ); logger.error(e.getMessage(); return result;/* 根據(jù)有參sql語句進(jìn)行數(shù)據(jù)更新,并返回更新后影響的記錄數(shù)* param sql* param args* return int*/public int update(String sql, Object. args) if (conn = null) return -1; QueryRunner run = new QueryRunner(); int result = 0; try result = run.update(conn, sql, args); catch (SQLE

16、xception e) (SQLException in + sql + ); logger.error(e.getMessage(); return result;/* 設(shè)置存放查詢結(jié)果的Bean類,每個(gè)Bean類對(duì)象對(duì)應(yīng)一條查詢的結(jié)果記錄* param beanClass,如User.class*/public void setBeanClass(Class beanClass) this.beanClass = beanClass;/* 設(shè)置使用外部的Connection對(duì)象,如來自數(shù)據(jù)庫連接池的連接* param conn Connection對(duì)象*/public

17、 void setConn(Connection conn) this.conn = conn;Overrideprotected void finalize() throws Throwable / TODO Auto-generated method stub close(); super.finalize();DbTestpackage hzvtc.util.test;import hzvtc.util.DbHelper;import java.util.List;public class DbTest /* param args*/public static void main(Str

18、ing args) / TODO Auto-generated method stub int n = 3; DbHelper dh = new DbHelper(mysql, localhost, test, root, 123456); dh.connect(); dh.setBeanClass(User.class); /u是從數(shù)據(jù)庫查詢出來的記錄對(duì)象 User u = (User) dh.query(SELECT id,name,age,address FROM t_user WHERE id=9); /u2是從數(shù)據(jù)庫查詢出來的記錄對(duì)象 User u2 = (User) dh.query(SELECT id,name,age,address FROM t_user WHERE id=?, n); / users和user2存放著從數(shù)據(jù)庫查詢出來的記錄對(duì)象集合 List u

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論