JDBC连接工厂

来源:互联网 发布:网络大v公知 编辑:程序博客网 时间:2024/05/16 16:06

看到论坛上很多朋友还在问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,oracledbms=sqlserver#connecttype 连接方式,可指定为:jdbc或者datasource以及provider方式connecttype=jdbc#当指定了参数connecttype为jdbc时,以下参数必需提供driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver://localhost:1039;DatabaseName=kingtbls;SelectMethod=cursorusername=kingpassword=king#当指定了参数connecttype为datasource时,以下参数必需提供datasource=
原创粉丝点击