BaseDao.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class BaseDao {    

    public final static String DRIVER = "com.ibm.db2.jcc.DB2Driver";                 // 数据库驱动

    public final static String URL    = "jdbc:db2://localhost:50000/company";   // url

    public final static String DBNAME = "ZhangQ";                                                           // 数据库用户名

    public final static String DBPASS = "100188";                                                           // 数据库密码

 

    /**

     * 得到数据库连接

     * @throws ClassNotFoundException

     * @throws SQLException

     * @return 数据库连接

     */

    public Connection getConn() throws ClassNotFoundException, SQLException{

        Class.forName(DRIVER);                                                    //注册驱动

        Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS);        //获得数据库连接

        //System.out.println("连接成功");

        return conn ;                                                            //返回连接

    }

 

    /**

     * 释放资源

     * @param conn 数据库连接

     * @param pstmt PreparedStatement对象

     * @param rs 结果集

     */

    public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {

        /*  如果rs不空,关闭rs  */

        if(rs != null){

            try { rs.close();} catch (SQLException e) {e.printStackTrace();}

        }

        /*  如果pstmt不空,关闭pstmt  */

        if(pstmt != null){

            try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}

        }

        /*  如果conn不空,关闭conn  */

        if(conn != null){

            try { conn.close();} catch (SQLException e) {e.printStackTrace();}

        }

    }

 

    /**

     * 执行SQL语句,可以进行增、删、改的操作,不能执行查询

     * @param sql  预编译的 SQL 语句

     * @param param  预编译的 SQL 语句中的‘?’参数的字符串数组

     * @return 影响的条数

     */

    public int executeSQL(String preparedSql,String[] param) {

        Connection        conn  = null;

        PreparedStatement pstmt = null;

        int               num   = 0;

 

        /*  处理SQL,执行SQL  */

        try {

            conn = getConn();                              // 得到数据库连接

            pstmt = conn.prepareStatement(preparedSql);    // 得到PreparedStatement对象

            if( param != null ) {

                for( int i = 0; i < param.length; i++ ) {

                    pstmt.setString(i+1,param[i]);         // 为预编译sql设置参数

                }

            }

            num = pstmt.executeUpdate();                    // 执行SQL语句

        } catch (ClassNotFoundException e) {

            e.printStackTrace();                            // 处理ClassNotFoundException异常

        } catch (SQLException e) {

            e.printStackTrace();                            // 处理SQLException异常

        } finally {

            closeAll(conn,pstmt,null);                     // 释放资源

        }

        return num;

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值