Java中JDBC的封装BaseDao工具类代码

BaseDao

希望这个工具类对大家有帮助!
继承BaseDao类,即可调用BaseDao中的所有非私有成员。
节省了代码量,使代码更加简洁,通俗易懂。
只需调用方法,即可实现功能。

import java.sql.*;

/**
 * 数据库连接与关闭工具类
 */
public class BaseDao {
    private final String driver = "com.mysql.cj.jdbc.Driver";// 数据库驱动字符串
    private final String url = "jdbc:mysql://localhost:3306/Leave";// 连接URL字符串
    private final String user = "root"; // 数据库用户名
    private final String password = "admin."; // 用户密码
    Connection conn = null;				// 数据连接对象
    /**
     * 获取数据库连接对象
     */
    public Connection getConnection() {
        if(conn==null) {
            // 获取连接并捕获异常
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, user, password);
            } catch (Exception 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;
    }

    /**
     * 查询操作
     * @param preparedSql
     * @param param
     * @return
     */
    public ResultSet executeQuery (String preparedSql, Object... param) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        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]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            //closeAll(conn,pstmt,null);
        }
        return rs;
    }
    public int executeFirst (String preparedSql, Object... param) {
        PreparedStatement pstmt = null;
        ResultSet rs = 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]);
                }
            }
            rs = pstmt.executeQuery();
            if (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            //closeAll(conn,pstmt,rs);
        }
        return num;
    }
}


例如:

public class demo1 extends BaseDao {
    public static void main(String[] args) {
        //System.out.println(bd.executeUpdate("update tb_user set uname = ? where uname=?","嘎嘎","拉拉"));
        ResultSet rs = executeQuery("select * from tb_type");
        try {
            while (rs.next()) {
                int tid = rs.getInt("tid");
                String tname = rs.getString("tname");
                String tmenu = rs.getString("tmenu");
                System.out.println(tid+"~~"+tname+"~~"+tmenu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值