从配置文件里读取数据,实现多数据库连接

配置文件:

db.properties

[java]  view plain copy
  1. driverClass=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://127.0.0.1/itbbs  
  3. username=root  
  4. password=sa  

加载配置文件:

[java]  view plain copy
  1. package com.ccl.db;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.InvalidPropertiesFormatException;  
  6. import java.util.Properties;  
  7.   
  8. public class LoadConfig {  
  9.   
  10.     /** 
  11.      * @author chengchanglun 
  12.      * @param args 
  13.      */  
  14.   
  15.     private Properties prop;  
  16.   
  17.     /** 
  18.      * @param path 
  19.      *            as "/db.properties" 
  20.      */  
  21.     public LoadConfig(String path) {  
  22.   
  23.         prop = new Properties();  
  24.   
  25.         InputStream in = null;  
  26.   
  27.         try {  
  28.   
  29.             in = getClass().getResourceAsStream(path);  
  30.   
  31.             prop.load(in);  
  32.   
  33.             in.close();  
  34.   
  35.         } catch (IOException e) {  
  36.   
  37.             e.printStackTrace();  
  38.         }  
  39.   
  40.     }  
  41.   
  42.     public String getDriverClass() {  
  43.   
  44.         return prop.getProperty("driverClass");  
  45.   
  46.     }  
  47.   
  48.     public String getURL() {  
  49.   
  50.         return prop.getProperty("url");  
  51.   
  52.     }  
  53.   
  54.     public String getUserName() {  
  55.   
  56.         return prop.getProperty("username");  
  57.     }  
  58.   
  59.     public String getPassWord() {  
  60.   
  61.         return prop.getProperty("password");  
  62.     }  
  63.   
  64.     public static void main(String args[]) {  
  65.   
  66.         LoadConfig c = new LoadConfig("/db.properties");  
  67.   
  68.         Properties p = new Properties();  
  69.   
  70.     }  
  71.   
  72. }  

连接数据库:

[java]  view plain copy
  1. package com.ccl.db;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5.   
  6. /** 
  7.  * @author chengchanglun 
  8.  * @category db 
  9.  */  
  10. public class ConnectionDataBase {  
  11.   
  12.     private static LoadConfig config;  
  13.   
  14.     private static Connection conn = null;  
  15.   
  16.     public ConnectionDataBase() {  
  17.   
  18.         init();  
  19.   
  20.     }  
  21.   
  22.     public static void init() {  
  23.   
  24.         config = new LoadConfig("/db.properties");  
  25.   
  26.     }  
  27.   
  28.     public ConnectionDataBase(String path) {  
  29.   
  30.         config = new LoadConfig(path);  
  31.   
  32.     }  
  33.   
  34.     public static Connection getConnection() {  
  35.   
  36.         init();  
  37.   
  38.         try {  
  39.   
  40.             Class.forName(config.getDriverClass());  
  41.   
  42.             conn = DriverManager.getConnection(config.getURL(),  
  43.                     config.getUserName(), config.getPassWord());  
  44.   
  45.         } catch (Exception e) {  
  46.   
  47.             System.out.println(e.getMessage());  
  48.         }  
  49.   
  50.         return conn;  
  51.   
  52.     }  
  53.   
  54.     public static void main(String[] args) {  
  55.   
  56.         Connection c = ConnectionDataBase.getConnection();  
  57.   
  58.         System.out.println(c);  
  59.   
  60.     }  
  61.   
  62. }  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值