JDBC数据库连接之配置文件

为了使代码灵活切易于扩展和维护,我们一般将数据库配置信息放入文件中,比如:db.properties

url=jdbc:mysql://localhost:3306/day17
user=root
password=root
driverClass=com.mysql.jdbc.Driver
这时候使用类路径读取:

InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");
“/”表示classpath的根目录(maven项目中放在resource目录下即可,因为项目打包资源文件放在class下)

代码如下:

public class JdbcUtil {
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driverClass = null;

    static{
        try {
        Properties prop = new Properties();
        InputStream in = JdbcUtil.class.getResourceAsStream("/bd.properties");
        prop.load(in);

        //读取配置信息
        url = prop.getProperty("url");
        user = prop.getProperty("user");
        password = prop.getProperty("password");
        driverClass = prop.getProperty("driverClass");

        //加载驱动
        Class.forName(driverClass);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取配置文件失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加载驱动失败");
        }
    }

    /**
     * 连接
     */
    public static Connection getConnection(){
        try {
            Connection con = DriverManager.getConnection(url, user, password);
            return con;
        } catch(SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询
     */
    public static void queryall(){
        try {

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询
     */
    public static void insert(Object obj){
        try {

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 释放资源的方法
     */
    public static void close(Connection conn, Statement stmt){
        try{
            if(stmt!=null){
                stmt.close();
                stmt = null;
            }
        } catch(SQLException e){
            e.printStackTrace();
        }finally{
            try{
                if(conn!=null){
                    conn.close();
                    conn = null;
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
                rs = null;
            }
        } catch(SQLException e){
            e.printStackTrace();
        }finally {
            close(conn, stmt);
        }

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值