数据库工具类DBUtil类

		package com.chenyu.www.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 通用的数据操作方法
 * @author 86323
 */
public class DBUtil {

    private static final String URL  ="jdbc:mysql://localhost:3306/market?useUnicode=true&characterEncoding=UTF-8" ;
    private static final String JDBC_NAME = "com.mysql.jdbc.Driver";
    private static final String USERNAME  ="root" ;
    private static final String PASSWORD  ="2289360" ;
    private static PreparedStatement pstmt = null ;
    private static Connection connection = null ;
    private static ResultSet rs = null ;
    static{
        try {
            Class.forName(JDBC_NAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 通用的增删改方法
     * @param sql sql语句
     * @param params 增删改的集合
     * @return 成功与否
     */
    public static boolean executeUpdate(String sql,Object[] params) {
        try {
            //setXxx()方法的个数 依赖于 ?的个数, 而?的个数 又和 数组params的个数一致
            //setXxx()方法的个数 ->数组params的个数一致
            pstmt = createPreParedStatement(sql,params);
            int count = pstmt.executeUpdate() ;
            if(count>0){
                return true ;
                }
            else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false ;
        }catch (Exception e) {
            e.printStackTrace();
            return false ;
        }
        finally {
            closeAll(null,pstmt,connection);
        }
    }

    /**
     * 关闭
     * @param rs ResultSet
     * @param stmt Statement
     * @param connection Connection
     */
    private static void closeAll(ResultSet rs, Statement stmt, Connection connection)
    {
        try {
            if (rs!=null) {
                rs.close();
            }

            if(pstmt!=null){
                pstmt.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


    /**
     * 获得Connection
     * @return Connection
     * @throws SQLException SQLException
     */
    private static Connection getConnection() throws SQLException {
        return  DriverManager.getConnection( URL,USERNAME,PASSWORD ) ;
    }


    /**
     * 将集合按顺序插入PreparedStatement
     * @param sql sql语句
     * @param params 集合
     * @return PreparedStatement
     * @throws SQLException SQLException
     */
    private static PreparedStatement createPreParedStatement(String sql, Object[] params) throws SQLException {
        pstmt = getConnection() .prepareStatement( sql) ;
        if(params!=null ) {
            for(int i=0;i<params.length;i++) {
                pstmt.setObject(i+1, params[i]);
            }
        }
        return pstmt;
    }


    /**
     * 通用的查  :适合任何查询
     * @param sql sql语句
     * @param params  更新的集合
     * @return
     */
    public static ResultSet executeQuery( String sql ,Object[] params)
    {
        try {

            pstmt = createPreParedStatement(sql,params);
            rs =  pstmt.executeQuery() ;
            return rs ;
        } catch (SQLException e) {
            e.printStackTrace();
            return null ;
        }catch (Exception e) {
            e.printStackTrace();
            return null ;
        }finally {
            closeAll(null,pstmt,connection);
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
m_pDbProcPic = new CDBProcess("sqlserver"); m_pDbProc->openDB("mysvr", "mydb", "myusername", "mypwd"); m_pDbProcPic = new CDBProcess("mysql"); m_pDbProc->openDB("localhost", "mydb", "root", "password"); m_pDbProcPic = new CDBProcess("access"); m_pDbProc->openDB("", strMDB, "sa", "password"); m_pDbProcPic = new CDBProcess("sqlite"); m_pDbProcPic->openDB("", "mysqlitedb"); CDBProcess使用说明 构造函数: CDBProcess(const QString strType); 参数:为数据库型,不区分大小写,支持的型有 sqlite mysql access sqlserver 例: CDBProcess db("sqlite"); -------------------------------------------------- 打开数据库 bool openDB(const QString strSvrName, //服务器名 const QString strDBname, //数据库名 const QString strUserID="", //用户名 const QString strUserPwd=""); //密码 打开数据库成功,返回true,否则返回false 对于sqlite,只有strDBname有效,其它参数忽略,如db.openDB("", "mysqlite.db") 对于MS Access数据库,strSvrName为空,用户名默认为"sa",如db.openDB("", "myaccess.mdb"); 对MSAccess数据库,也可通过一个UDL文件,打开,如db.openDB("my.udl", ""); mysql和sqlserver,就是各个参数依次提供,如db.openDB("svr1", "mydb", "user1", "abcd"); ----------------------------------------------------- 关闭数据库,CDBProcess析构时,亦会自动关闭数据库 void closeDB(); //db.closeDB(); ------------------------------------------------------ 执行Sql语句 bool excuteSQL(const QString); ------------------------------------------------------- 打开记录集 bool openRecordsetBySql(const QString strSql, //Sql语句 int idx = -1); //记录集id,用于标识记录集,默认值为-1 例: db.openRecordsetBySql("SELECT * FROM TB1", 5); 打开一个记录集,标识号为5,后面操作这个记录集,也要提供这个标识号 -------------------------------------------------------- 关闭记录集 void closeRecordset(int idx = -1); 例: db.closeRecordset(5); 关闭之前打开的标识为5的记录集 ----------------------------------- 数据库是否处在打开状态 bool isOpen() const; ------------------------------------ 记录集游标是否在结尾,参数为记录集标识 bool recEOF(int idx = -1) const; 例:bool b = db.RecBOF(5); ------------------------------------ 记录集游标是否在第一条记录之前,参数为记录集标识 bool recBOF(int idx = -1) const; ---------------------------------------- 删除一个表 bool dropTable(const QString); --------------------------------------------- 读取标识为idx记录集的当前记录的各字段值,方法如示例: bool getFieldsValueFromRec(int idx, const char* szFldInfo, ...) const; int iSN; QString strName; double dHeight; QDateTime dt; QByteArray ba; db.getFieldsValueFromRec(5, //记录集id "sn%d", &iSN, //字段名加型 sn为字段名%d表示整型,&iSN传入指针,保存返回字段值 "name%s", &strName, //字段名加型 name为字段名%s表示字符串(QString) "height&f", &dHeight, //字段名加型 height为字段名%f表示小数(double) "birthday%t", &dt, //字段名加型 birthday为字段名%t表示时间(QDateTime) "photo%b", &ba, //字段名加型 photo为字段名%b表示二进制流(QByteArray) CDBProcess::szEnd); //结束标志,"|" 执行后,各字段值就保存在iSN, strName等变量中了。 参数:第一个参数为记录集id 后面可变参数,字段%型标识,用于保存返回值的指针, 型标识:%d-int %s-QString %f-double %t-QDateTime %b-QByteArray --------------------------------------------------------------------------- 向一个数据表中填加一条记录 bool addFieldsValueToTbl(const QString strTbl, const char* szFldInfo, ...); 参数:第一个参数strTbl,为表名 后面是可变参数,为"字段%型标识",值(或指针),注int,double型直接传值,其它传指针 例: db.addFieldsValueToTbl("TB1", //表名 "sn%d", iSN, //字段名加型 sn为字段名%d表示整型,iSN传入值 "name%s", &strName, //字段名加型 name为字段名%s表示字符串(QString), 传入QString变量指针 "height&f", dHeight, //字段名加型 height为字段名%f表示小数(double),dHeight传入值 "birthday%t", &dt, //字段名加型 birthday为字段名%t表示时间(QDateTime),传入指针 "photo%b", &ba, //字段名加型 photo为字段名%b表示二进制流(QByteArray),传入指针 CDBProcess::szEnd); //结束标志,"|" ----------------------------------------------------------- 修改表中符合WHERE子句条件的记录 bool updateTblFieldsValue(const QString strTbl, QString strWhere, const char * szFldInfo, ... ); strTbl表名 strWhere SQL WHERE子句,如"WHERE sn=20" const char * szFldInfo, ...可变参数,同addFieldsValueToTbl 例: db.updateTblFieldsValue("TB1", "WHERE sn=20", "height&f", dHeight, "photo%b", &ba, CDBProcess::szEnd); ---------------------------------------------------------------- 以下几个函数分别是获取记录数,和记录光标移动。参数为记录集标识 long getRecordCount(int idx = -1) const; bool moveFirst(int idx = -1) const; bool movePrevious(int idx = -1) const; bool moveNext(int idx = -1) const; bool moveLast(int idx = -1) const; bool moveTo(int n, int idx = -1) const; -------------------------------------------------------------------- 返回数据库名 QString getDbName() const; ------------------------ 以下几个函数未验证 bool execStoreProcOfArgList(int idx, const QString strStoreProc, const char* szFldsInfo, ...); bool exexProc(const QString strStoreProc, QString str1, QString& str2); bool transaction(); bool commit(); bool rollback();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值