JDBC连接工厂

看到论坛上很多朋友还在问v的连接,于是写了一个jdbc连接工厂,其实是还没完工的,大家可以借鉴下:

 

在项目的包中导入相应的JDBC驱动包,然后把下面的代码放在一个放在class里面,一个放在jdbc.properties里面,注意jdbc.properties放在src目录下,然后直接调用getConnection()获取连接
,另外请修改jdbc.properties中的设置。

Java code
   
   
package ibatis.test.model; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * @author king * jdbc连接工厂 * */ public class JdbcFactory { private Connection conn = null ; private static final String FILEPATH = " src/jdbc.properties " ; private Connection connectionFactory(String filePath) throws Exception{ Map < String, Object > params = new HashMap < String, Object > (); try { params = readProperties(filePath); } catch (Exception e) { throw new Exception( " 文件不存在,请检查文件目录 " ); } int type = checkProperties(filePath); switch (type) { case 1 : return getJdbcConnection(params.get( " driver_class " ),params.get( " url " ),params.get( " username " ),params.get( " password " )); case 2 : return getDatasourceConnection(params.get( " datasource " )); case 3 : return null ; default : throw new Exception( " 错误的连接方式 " ); } } public static Connection getConnection() throws Exception{ return new JdbcFactory().connectionFactory(FILEPATH); } /** * 以Datasource方式获取连接 * @param datasource * @return */ private Connection getDatasourceConnection(Object datasource) { // TODO Datasource方式连接 return null ; } /** * 以JDBC方式获取连接 * @param driver_class * @param url * @param username * @param password * @return * @throws Exception */ private Connection getJdbcConnection(Object driver_class, Object url,Object username, Object password) throws Exception { String driver = (String) driver_class; String jdbc_url = (String) url; String jdbc_user = (String) username; String jdbc_pass = (String) password; try { Class.forName(driver); conn = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_pass); return conn; } catch (ClassNotFoundException e) { throw e; } catch (SQLException e) { throw e; } } /** * 验证配置文件设置 * dbms;DBMS类型 * connecttype连接方式 可指定为:jdbc或者datasource以及provider方式 * 当指定了参数connecttype为jdbc时,以下参数必需提供 * driver_class; * url; * username; * password; * 当指定了参数connecttype为datasource时,以下参数必需提供 * datasource; * @return * 1 jdbc * 2 datasource * 3 provider * @throws Exception */ private int checkProperties(String filePath) throws Exception{ Map < String, Object > params = new HashMap < String, Object > (); int type = 0 ; try { try { params = readProperties(filePath); } catch (Exception e) { throw new Exception( " 文件不存在,请检查文件目录 " ); } if (params != null ){ if (params.containsKey( " dbms " )){ if (params.get( " dbms " ).equals( "" )){ throw new Exception( " DBMS类型:dbms不能为空 " ); } } if (params.containsKey( " connecttype " )){ if (params.get( " connecttype " ).equals( "" )){ throw new Exception( " 连接方式:connecttype不能为空 " ); } else { if (params.get( " connecttype " ).equals( " jdbc " )){ type = 1 ; if (params.containsKey( " driver_class " )){ if (params.get( " driver_class " ).equals( "" )){ throw new Exception(params.get( " connecttype " ) + " 连接方式下driver_class不能为空 " ); } } if (params.containsKey( " url " )){ if (params.get( " url " ).equals( "" )){ throw new Exception(params.get( " connecttype " ) + " 连接方式下url不能为空 " ); } } if (params.containsKey( " username " )){ if (params.get( " username " ).equals( "" )){ throw new Exception(params.get( " connecttype " ) + " 连接方式下username不能为空 " ); } } if (params.containsKey( " password " )){ if (params.get( " password " ).equals( "" )){ throw new Exception(params.get( " connecttype " ) + " 连接方式下password不能为空 " ); } } } else if (params.get( " connecttype " ).equals( " datasource " )){ type = 2 ; if (params.containsKey( " datasource " )){ if (params.get( " datasource " ).equals( "" )){ throw new Exception(params.get( " connecttype " ) + " 连接方式下datasource不能为空 " ); } } } else if (params.get( " connecttype " ).equals( " provider " )){ type = 3 ; throw new Exception( " 暂时未添加此连接方式 " ); } else { throw new Exception( " 连接方式:connecttype可指定为:jdbc或者datasource以及provider方式 " ); } } } } return type; } catch (Exception e) { throw e; } } /** * 根据文件路径和key读取对应的value * @param key * @param filePath 文件路径 * @return 可对应value * @throws Exception 文件不存在或者错误的key */ @SuppressWarnings( " unused " ) private String readValue(String key,String filePath ) throws Exception{ Properties props = new Properties(); String value = "" ; try { InputStream in = new BufferedInputStream( new FileInputStream(filePath)); props.load(in); value = props.getProperty(key); return value; } catch (Exception e) { throw new Exception( " 文件不存在或错误的key,请检查文件路径和key是否正确 " ); } } /** * 读取properties的全部信息 * @param filePath */ @SuppressWarnings( " unchecked " ) private Map < String, Object > readProperties(String filePath) throws Exception { Map < String, Object > params = new HashMap < String, Object > (); Properties props = new Properties(); try { InputStream in = new BufferedInputStream( new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key) == null ? "" :props.getProperty(key); params.put(key, Property); } return params; } catch (Exception e) { throw new Exception( " 文件不存在,请检查文件路径 " ); } } }
Java code
   
   
#DBMS类型:sqlserver,mysql,oracle dbms = sqlserver #connecttype 连接方式,可指定为:jdbc或者datasource以及provider方式 connecttype = jdbc #当指定了参数connecttype为jdbc时,以下参数必需提供 driver_class = com.microsoft.sqlserver.jdbc.SQLServerDriver url = jdbc:sqlserver: // localhost:1039;DatabaseName=kingtbls;SelectMethod=cursor username = king password = king #当指定了参数connecttype为datasource时,以下参数必需提供 datasource =
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值