码迷,mamicode.com
首页 > 数据库 > 详细

java 读取src下的db.properties

时间:2016-08-23 15:03:17      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

此也是将数据库与java 的JDBC连接

//这是将db.properties数据流放入内存中
            InputStream ins =  Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
            pro.load(ins);
            driver=pro.getProperty("driver");
            url=pro.getProperty("url");
            username=pro.getProperty("username");
            password=pro.getProperty("password");
            ins.close();

db.properties

#MySql
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
#username=root
#password=root

#oracle
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=scott
password=tiger

DBUtil.java

package com.edusk.util;

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

public class DBUtil {
    private static String driver = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";
    private Connection conn = null;
    
    /*
     * 静态代码块
     */
    static{
        Properties pro = new Properties();
        try {
       //这是将db.properties数据流放入内存中 InputStream ins
= Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"); pro.load(ins); driver=pro.getProperty("driver"); url=pro.getProperty("url"); username=pro.getProperty("username"); password=pro.getProperty("password"); ins.close(); } catch (IOException e) { e.printStackTrace(); } } public Connection getConnection() { try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return conn; } /** * 关闭链接 * @param con * @param ps * @param rs */ public static void close(Connection conn,PreparedStatement ps,ResultSet rs){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

 

测试文件

TestConnection.java

package com.edusk.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.edusk.util.DBUtil;

public class TestConnection {
    
    public static void main(String[] args) {
        Connection conn = new DBUtil().getConnection();
        try {
            PreparedStatement ps = conn.prepareStatement("select empno, ename from emp");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getInt(1)+" "+rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

java 读取src下的db.properties

标签:

原文地址:http://www.cnblogs.com/w198/p/5799075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!