使用 properties配置文件 中配置的 datebase 连接数据库

我的jeesite.properties 配置文件

c3p0连接代码:



/**
 * 
 */
package com.thinkgem.jeesite.modules.projectmanage.util;


import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


import org.junit.Test;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sun.xml.internal.rngom.digested.DDataPattern;


/**
 * @Description:
 * @Author: 杨舜玺
 * @Company:
 * @CreateDate: 2018-3-30
 */


public class C3P0Util {

private static ComboPooledDataSource ds = null;


@Test
public Connection dd() throws FileNotFoundException, IOException, PropertyVetoException, SQLException{  
      
        Properties pr = new Properties();  
        //获得src下的c3p0.properties的路径  
        String c3p0Properties = this.getClass().getClassLoader().getResource("jeesite.properties").getPath();  
        //路径的编码是UTF-8  
        c3p0Properties = URLDecoder.decode(c3p0Properties, "utf-8");  
        //得到文件c3p0.properties文件  
        java.io.File c3p0File = new java.io.File(c3p0Properties);  
        //读取c3p0文件的内容  
        pr.load(new FileInputStream(c3p0File));  
          
//      pr.save(new FileOutputStream(c3p0File), null);  
        //使用c3p0操作数据库  
        ComboPooledDataSource cpds = new ComboPooledDataSource();  
        //加载数据驱动  
        cpds.setDriverClass(pr.getProperty("jdbc.driver"));  
        //连接特定的数据库  
        cpds.setJdbcUrl(pr.getProperty("jdbc.url"));  
        //数据库用户名  
        cpds.setUser(pr.getProperty("jdbc.username"));  
        //数据库用户密码  
        cpds.setPassword(pr.getProperty("jdbc.password"));  
        //获得连接  
        Connection conn = cpds.getConnection();  
        return conn;  
    }



static{




try {

Properties pr = new Properties();  
     //获得src下的c3p0.properties的路径  
     String c3p0Properties = C3P0Util.class.getClassLoader().getResource("jeesite.properties").getPath();  
     //路径的编码是UTF-8  
c3p0Properties = URLDecoder.decode(c3p0Properties, "utf-8");

     //得到文件c3p0.properties文件
     java.io.File c3p0File = new java.io.File(c3p0Properties);  
     //读取c3p0文件的内容  
pr.load(new FileInputStream(c3p0File));
//   pr.save(new FileOutputStream(c3p0File), null);  
     //使用c3p0操作数据库  
     ds = new ComboPooledDataSource();
     //加载数据驱动  
     ds.setDriverClass(pr.getProperty("jdbc.driver"));  
     //连接特定的数据库  
     ds.setJdbcUrl(pr.getProperty("jdbc.url"));  
     //数据库用户名  
     ds.setUser(pr.getProperty("jdbc.username"));  
     //数据库用户密码  
     ds.setPassword(pr.getProperty("jdbc.password")); 

     
    

/* ds = new ComboPooledDataSource();
ds.setDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ds.setJdbcUrl("${url}"); //jdbc:sqlserver://192.168.0.112:1433;DatabaseName=scywgl
ds.setUser("sa");
ds.setPassword("TaiXin@123");*/


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




catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}  

}



public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }


public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            pstmt = null;
        }


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


}


猜你喜欢

转载自blog.csdn.net/qq_39930369/article/details/79963463