Java中Dao模式中Dao的数据库操作(BaseDao的写法)

Dao模式是Java面向设计时的一种模式。而Dao中的BaseDao无非是对数据进行CURD(增删改查),下面是最常用的连接,增删改查的方法。 


package dao;


import java.sql.*;


/**
 * 数据库访问类
 */

public class BaseDao {
    //数据库驱动
    private static String driver = "com.mysql.jdbc.Driver";
    //数据库连接地址
    private static String url = "jdbc:mysql://127.0.0.1:3306/flight?useSSL=False";
    //数据库用户名
    private static String user = "root";
    //数据库密码
    private static String password = "123456";


    //数据库连接
    protected Connection conn = null;
    //statement对象
    protected PreparedStatement pstmt = null;


    /**
     * 连接数据库
     *
     * @return 数据库连接
     * @throws ClassNotFoundException 未找到驱动类异常
     * @throws SQLException           数据库异常
     */
    protected Connection getConn() throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName(driver);
        //获取连接
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }


    /**
     * 数据库查询方法
     *
     * @param sql    预编译sql语句
     * @param params 预编译参数
     * @return 查询结果集
     * @throws SQLException 数据库异常
     */
    protected ResultSet executeQuerySQL(String sql, Object[] params) throws SQLException {
        ResultSet rs = null;
        try {
            conn = getConn();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        pstmt = conn.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        rs = pstmt.executeQuery();
        return rs;
    }


    /**
     * 数据库增删改方法
     *
     * @param sql    预编译sql语句
     * @param params 预编译参数
     * @return 数据库影响行数
     * @throws SQLException 数据库异常
     */
    protected int executeUpdate(String sql, Object[] params) throws SQLException {
        try {
            conn = getConn();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        int num = 0;
        pstmt = conn.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        num = pstmt.executeUpdate();


        return num;
    }


    /**
     * 关闭数据库连接
     * @param conn 数据库连接
     * @param pstmt  statement对象
     * @param rs  ResultSet对象
     */
    protected void closeALL(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值