读取*.properties文件连接数据库

需求:系统A 需要为 系统B数据库插入数据。

应用场景:《核心系统》中计算出有关薪资的数据,为《财务系统》发送凭证。

实现过程:

  1. 新建 db.properties文件,编写数据库配置文件:
    driver=oracle.jdbc.driver.OracleDriver
    url=*********************
    username=******
    password=******

     

  2. 读取该文件,连接数据库:(通过ClassLoader 读取):

    package cn.com.cis.acic.util;
    import java.io.IOException;
    import java.io.InputStream;
    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 DBUtil {
        
        private static final String LOCATION="dbOracle.properties";
        private static Properties props=new Properties();
        static{
    
            ClassLoader loader=DBUtil.class.getClassLoader();
            InputStream is=loader.getResourceAsStream(LOCATION);
            try {
                props.load(is);
            } catch (IOException e) {
                System.out.println("填充Properties对象失败!");
                e.printStackTrace();
            }
        }
        
        private DBUtil(){}
        
        public static Connection getConnection(){
        
            String driver=props.getProperty("driver");
            String url=props.getProperty("url");
            String username=props.getProperty("username");
            String password=props.getProperty("password");
    
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                System.out.println("数据库驱动类加载失败!");
                e.printStackTrace();
            }
            
            Connection conn=null;
            try {
                conn=DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                System.out.println("连接数据库失败!");
                e.printStackTrace();
            }
            return conn;
        }
        
        public static void close(ResultSet rs,Statement stmt, Connection conn){
    
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    System.out.println("关闭结果集对象失败!");
                    e.printStackTrace();
                }
            }
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("关闭语句对象失败!");
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("关闭连接对象失败!");
                    e.printStackTrace();
                }
            }
        }
    
        public static void close(ResultSet rs, Statement stmt){
            close(rs,stmt,null);
        }
        public static void close(Statement stmt, Connection conn){
            close(null,stmt,conn);
        }
        public static void close(Connection conn){
            close(null,null,conn);
        }
    }

    也可以直接读取,如:

                //读取数据库链接:
                InputStream in = FeeManageServiceSpringImpl.class.getResourceAsStream("/accountjdbc.properties");//文件名称
                InputStream in = new BufferedInputStream(new
                FileInputStream("D:\\tomcat-7\\webapps\\sales\\WEB-INF\\classes\\accountjdbc.proerties"));//详细地址
                Properties p = new Properties();
                p.load(in);
                String className2 = p.getProperty("DRIVER");
                String url = p.getProperty("URL");
                String user = p.getProperty("USER");
                String password = p.getProperty("PASSWORD");
                Class.forName(className2);

     

posted on 2017-05-31 17:31  forever_2h  阅读(178)  评论(0编辑  收藏  举报

导航