jdbc连接数据库

jdbc封装(其中一种):
·创建数据库连接工具类JdbcUtil.java,将数据库参数写在配置文件jdbc.properties中
·dao(数据访问)层创建BaseDao.java类,该类是所有数据访问层进行增删改查操作类的父类

·配置文件jdbc.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf-8
username=root
password=admin
·解析配置文件工具类ConfigManager.java
/**
 * @author 落俗
 *解析配置文件
 */
public class ConfigManager {

    private static Properties props = null;
    static{
        InputStream is = null;
        is = ConfigManager.class.getClassLoader().getResourceAsStream("jdbc.properties");
        if(is == null){
            throw new RuntimeException("文件读取失败");
        }
        props = new Properties();
        try{
            props.load(is);
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try{
                is.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    public static String getProperty(String key){
        return props.getProperty(key);
    }
}
·数据库连接工具类JdbcUtil.java
/**
 * @author 落俗
 *
 */
public class JdbcUtils {
    //读取配置文件
    private static String driver = ConfigManager.getProperty("driverClass");
    private static String url = ConfigManager.getProperty("url");
    private static String username = ConfigManager.getProperty("username");
    private static String password = ConfigManager.getProperty("password");
    private static Connection con = null;
    //静态代码块加载数据库驱动
    static{
        try{
            Class.forName(driver);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //打开数据库连接
    public static Connection getConnection(){
        //Connection con = null;
        try {
            if(con == null || con.isClosed()){
                con = DriverManager.getConnection(url,username,password);
            }else{
                return con;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭连接
     * @param con
     * @param pstmt
     * @param rs
     */
    public static void closeAll(Connection con, PreparedStatement pstmt,ResultSet  rs){
        try {
            if(rs != null && !rs.isClosed()){
                rs.close();
            }
            if(pstmt != null && !pstmt.isClosed()){
                pstmt.close();
            }
            if(con != null && !con.isClosed()){
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
·数据访问层的父类BaseDao.java
public class BaseDao {

    private Connection conn = null;
    public BaseDao(Connection conn){
        this.conn = conn;
    }
    /**
     * 增删改操作
     * @param sql
     * @param params
     * @return  (1)数据操作语句DML的行数(2)对于无返回内容的sql语句返回0
     */
    public int executeUpdate(String sql, Object...params){  //Object...params 可变长的Object类型的数组
        int result = 0;
        PreparedStatement pstmt = null;//执行sql
        try{
            pstmt = conn.prepareStatement(sql);
            if(params != null){
                for(int i = 0; i < params.length; i++){
                    pstmt.setObject(i+1, params[i]);
                    //等价于pstmt.set类型(i+1, xxx);
                }
            }
            result = pstmt.executeUpdate();//执行操作
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            JdbcUtils.closeAll(null, pstmt, null);
        }
        return result;
    }
    /**
     * 查询操作,最后不关闭rs和pstmt
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql, Object...params){

        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            if(params != null){
                for(int i = 0; i < params.length; i++){
                    pstmt.setObject(i+1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值