JDBC学习笔记(4) -- 类路径读取jdbcUtil的配置文件

应使用类路径的读取

. 代表java命令运行的目录
1.在java项目下,. java命令的运行目录从项目的根目录开始
2.在web项目下, . java命令的而运行目录从tomcat/bin目录开始

所以不能使用点.

 FileInputStream  in = new FileInputStream("./src/db.properties");
使用类路径的读取方式
/ : 斜杠表示classpath的根目录
1.在java项目下,classpath的根目录从bin目录开始
2.在web项目下,classpath的根目录从WEB-INF/classes目录开始
  InputStream in = Demo_Jdbc.class.getResourceAsStream("/db.properties");
package com.huan.dao;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Created by pc on 17-4-27.
 */
public class Demo_Jdbc {
    //连接数据库的URL
    public static String url = null;
    public static String user = null;
    public static String password = null;
    public static String driverClass=null;
    static {
        try {
            Properties props = new Properties();
//          FileInputStream  in = new FileInputStream("./src/db.properties");

            InputStream in = Demo_Jdbc.class.getResourceAsStream("/db.properties");
            props.load(in);
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");
            driverClass = props.getProperty("driverClass");
            System.out.println(url);
            System.out.println(user);
            System.out.println(password);
            System.out.println(driverClass);

            Class.forName(driverClass);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("加载失败");
        }
    }

    @Test
    public static   Connection Test_sql2() throws Exception {


        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        return conn;
    }

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

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

db.properties文件
url = jdbc:mysql://localhost:3306/eurasia_echarts?characterEncoding=UTF-8
user = root
password = root
driverClass = com.mysql.jdbc.Driver

JDBC学习笔记(4) -- 类路径读取jdbcUtil的配置文件_第1张图片

你可能感兴趣的:(JDBC学习笔记(4) -- 类路径读取jdbcUtil的配置文件)