BaseDao,jdbc连接数据库的7步骤

本文介绍了Java中BaseDao类用于连接数据库的实现过程,包括加载配置文件db.properties获取数据库连接参数,通过DriverManager获取连接,并提供关闭资源的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hrtc23.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * 
 * 操作数据库的基本类:其中三个属性提供了两个通用的方法,一个是获取连接的方法,一个是关闭资源的方法。
 *
 */
public class BaseDao {
    //1.三个属性
    protected InputStream inputStream=null;
    protected Connection ct=null;
    protected PreparedStatement pst=null;
    protected ResultSet rs=null;
    //2.获取连接的方法
    protected void getConnection(){
        try {     
            //通过类加载器加载了db.properties
            inputStream=this.getClass().getClassLoader().getResourceAsStream("db.properties");
                //通过properties类来读取db.properties中的每一个连接数据库的参数
                Properties pt=new Properties();
                //将保存了db.properties文件的输入流放置到properties的对象中去
                pt.load(inputStream);
                //通过getproperty()方法读取db.properties中的4个数据库的参数
                String driverClass= pt.getProperty("driverClass");
                String jdbcUrl= pt.getProperty("jdbcUrl");
                String user= pt.getProperty("user");
                String password= pt.getProperty("password");
                //3.通过以上读取到的4个连接数据库的参数,然后获取一个数据库的连接。
                //注册驱动:将需要连接的数据库注册驱动class类中
                Class.forName(driverClass);
                //通过getConnection(jdbcUrl, user, password)的方法能获取到一与个数据库的连接
                ct=DriverManager.getConnection(jdbcUrl, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //3.关闭资源
    protected void closeAll(){
        try {
            if(rs!=null){
                rs.close();
            }
            if(pst!=null){
                pst.close();
            }
            if(ct!=null){
                ct.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值