javaweb_day7(JDBC)的配置信息提取到配置文件

JDBC配置信息提取到配置文件


1.首先需要在工程下创建一个jdbc.properties文件

【jdbc.properties内容】

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///jdbc_demo
username=root
password=root

这里写图片描述


2.读取文件里面的信息
通过:Properties properties =new Properties();对象里面的load方法
这里写图片描述

3.配置文件的工具类

package com.jdbc.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Jdbcutils {

    private static final String driverClassName;//驱动
    private static final String url;//连接数据库的地址
    private static final String username;//用户名
    private static final String password;//密码 




    static{
            //创建对象
             Properties properties =new Properties();

            try {

                //加载配置文件
                properties.load(new FileInputStream("src/jdbc.properties"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //读取到的配置信息在赋值
            driverClassName=properties.getProperty("driverClassName");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");




    }
    /**
     * 注册驱动的方法
     */
    public static void loadDriver(){
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得连接的方法
     */
    public static Connection getConnection(){
        Connection conn = null;
        try{
            // 将驱动一并注册:
            loadDriver();
            // 获得连接
            conn = DriverManager.getConnection(url,username, password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }


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

            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    public static void release(ResultSet rs,Statement stmt,Connection conn){
        // 资源释放:
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

【测试工具类】
案例:查询

package com.jdbc.demo2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

import com.jdbc.utils.Jdbcutils;

public class Demo2 {

    // 查询
    @Test
    public void testSeach() throws Exception {

        Connection connection = null;// 连接对象
        Statement statement = null;// 执行sql语句对象
        ResultSet resultSet = null;// 返回结果集对象

        connection = Jdbcutils.getConnection();// 获取连接

        statement = connection.createStatement();// 创建执行sql语句对象
        String sql = "select * from t_user";//编写sql语句
        resultSet = statement.executeQuery(sql);//执行sql语句

        //遍历结果集
        while (resultSet.next()) {

System.out.println(resultSet.getInt("id") + "  " + resultSet.getString("name"));

        }

        //释放资源
        Jdbcutils.release(resultSet, statement, connection);

    }

}

测试成功:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值