BaseDao工具类

package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver;			// 数据库驱动字符串
    private static String url;	// 连接URL字符串
    private static String user; 						// 数据库用户名
    private static String password; 						// 用户密码
    Connection conn = null;
    // 数据连接对象
    static{//静态代码块,在类加载的时候执行
        init();
    }
    /**
     * 初始化连接参数,从配置文件里获得
     */
    public static void init(){
        Properties params=new Properties();
        String configFile = "database.properties";//配置文件路径
        //加载配置文件到输入流中
        InputStream is = BaseDao.class.getClassLoader()
                .getResourceAsStream(configFile);
        try {
            //从输入流中读取属性列表
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据指定的获取对应的值
        driver=params.getProperty("driver");
        url=params.getProperty("url");
        user=params.getProperty("username");
        password=params.getProperty("password");
    }

    /**
     * 获取数据库连接对象
     */
    public Connection getConnection() {
        try {
            if(conn==null || conn.isClosed()) {
                // 获取连接并捕获异常
                try {
                    Class.forName(driver);
                    conn = DriverManager.getConnection(url, user, password);
                } catch (Exception e) {
                    e.printStackTrace();							// 异常处理
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;											// 返回连接对象
    }
    /**
     * 关闭数据库连接
     * @param conn 数据库连接
     * @param stmt Statement对象
     * @param rs 结果集
     */
    public void closeAll(Connection conn, Statement stmt,
                         ResultSet rs) {
        // 若结果集对象不为空,则关闭
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 若Statement对象不为空,则关闭
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 若数据库连接对象不为空,则关闭
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 增、删、改的操作
     * @param preparedSql 预编译的 SQL 语句
     * @param param 参数的字符串数组
     * @return 影响的行数
     */
    public int exceuteUpdate (String preparedSql, Object[] param) {
        PreparedStatement pstmt = null;
        int num = 0;
        conn =  getConnection();
        try {
            pstmt = conn.prepareStatement(preparedSql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    //为预编译sql设置参数
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            closeAll(conn,pstmt,null);
        }
        return num;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值