一、前言
通过jena-sdb源码包(1.5.1)中定义com.hp.hpl.jena.sdb.sql.JDBC、com.hp.hpl.jena.sdb.sql.DatabaseType各个数据库驱动类,进行常用数据库(Derby/H2/HSQLDB/MySQL/PostgreSQL/SQLServer/Oracle/DB2/SAP等)连接字符串拼合、驱动类加载loadDriver、创建数据库连接createConnection等。
二、源码说明
1.JDBC操作类
package com.hp.hpl.jena.sdb.sql;@b@@b@import com.hp.hpl.jena.sdb.SDBException;@b@import com.hp.hpl.jena.sdb.shared.Access;@b@import com.hp.hpl.jena.sdb.shared.SDBNotFoundException;@b@import com.hp.hpl.jena.sdb.store.DatabaseType;@b@import java.sql.Connection;@b@import java.sql.DriverManager;@b@import java.sql.SQLException;@b@import java.util.HashMap;@b@import java.util.Locale;@b@import java.util.Map;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class JDBC@b@{@b@ private static Logger log = LoggerFactory.getLogger(JDBC.class);@b@ public static final String jdbcNone = "jdbc:none";@b@ private static Map<DatabaseType, String> driver = new HashMap();@b@@b@ public static String getDriver(DatabaseType dbType)@b@ {@b@ return ((String)driver.get(dbType)); }@b@@b@ public static void loadDriverHSQL() {@b@ loadDriver((String)driver.get(DatabaseType.HSQLDB)); }@b@@b@ public static void loadDriverH2() { loadDriver((String)driver.get(DatabaseType.H2)); }@b@@b@ public static void loadDriverMySQL() { loadDriver((String)driver.get(DatabaseType.MySQL)); }@b@@b@ public static void loadDriverPGSQL() { loadDriver((String)driver.get(DatabaseType.PostgreSQL)); }@b@@b@ public static void loadDriverDerby() { loadDriver((String)driver.get(DatabaseType.Derby)); }@b@@b@ public static void loadDriverSQLServer() { loadDriver((String)driver.get(DatabaseType.SQLServer)); }@b@@b@ public static void loadDriverOracle() { loadDriver((String)driver.get(DatabaseType.Oracle)); }@b@@b@ public static void loadDriverDB2() { loadDriver((String)driver.get(DatabaseType.DB2)); }@b@@b@ public static void loadDriverSAP() { loadDriver((String)driver.get(DatabaseType.SAP)); }@b@@b@ public static void loadDriver(String className) { loadClass(className);@b@ }@b@@b@ public static String guessDriver(String type) {@b@ return getDriver(DatabaseType.fetch(type));@b@ }@b@@b@ public static Connection createConnection(String url, String user, String password)@b@ throws SQLException@b@ {@b@ if (log.isDebugEnabled())@b@ log.debug("Create JDBC connection: " + url);@b@@b@ if (url.equals("jdbc:none"))@b@ return null;@b@@b@ if (user == null)@b@ user = Access.getUser();@b@ if (password == null)@b@ password = Access.getPassword();@b@@b@ return DriverManager.getConnection(url, user, password);@b@ }@b@@b@ private static void loadClass(String className)@b@ {@b@ try@b@ {@b@ Class.forName(className);@b@ } catch (ClassNotFoundException ex) {@b@ throw new SDBNotFoundException("Class.forName(" + className + ")", ex); }@b@ }@b@@b@ public static String makeURL(String type, String host, String dbName) {@b@ return makeURL(type, host, dbName, null, null);@b@ }@b@@b@ public static String makeURL(String type, String host, String dbName, String user, String password)@b@ {@b@ String s;@b@ if (log.isDebugEnabled())@b@ log.debug(String.format("Create JDBC URL: (type=%s, host=%s, dbName=%s)", new Object[] { type, host, dbName }));@b@@b@ type = type.toLowerCase(Locale.ENGLISH);@b@@b@ if (user == null)@b@ user = Access.getUser();@b@ if (password == null)@b@ password = Access.getPassword();@b@@b@ if (type.equals("mysql"))@b@ {@b@ s = String.format("jdbc:%s://%s/%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.startsWith("hsql"))@b@ {@b@ s = String.format("jdbc:%s:%s:%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.startsWith("h2"))@b@ {@b@ if ((type.startsWith("h2:tcp")) || (type.startsWith("h2:ssl")))@b@ {@b@ s = String.format("jdbc:%s://%s/%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ s = String.format("jdbc:%s:%s", new Object[] { type, dbName });@b@ return s;@b@ }@b@@b@ if ((type.startsWith("pgsql")) || (type.equals("postgresql")))@b@ {@b@ s = String.format("jdbc:%s://%s/%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.equals("derby"))@b@ {@b@ s = String.format("jdbc:%s:%s;create=true", new Object[] { type, dbName });@b@ return s;@b@ }@b@@b@ if ((type.equals("mssqlserver")) || (type.equals("sqlserver")))@b@ {@b@ s = String.format("jdbc:%s://%s;databaseName=%s", new Object[] { "sqlserver", host, dbName });@b@ return s;@b@ }@b@@b@ if ((type.equals("mssqlserverexpress")) || (type.equals("sqlserverexpress")))@b@ {@b@ s = String.format("jdbc:%s://%s\\SQLEXPRESS;databaseName=%s", new Object[] { "sqlserver", host, dbName });@b@ return s;@b@ }@b@@b@ if (type.startsWith("oracle:"))@b@ {@b@ s = String.format("jdbc:%s:@%s:%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.equals("oracle"))@b@ {@b@ s = String.format("jdbc:%s:thin:@%s:%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.equals("db2"))@b@ {@b@ s = String.format("jdbc:%s://%s/%s", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.equals("sap"))@b@ {@b@ s = String.format("jdbc:%s://%s:3%s15", new Object[] { type, host, dbName });@b@ return s;@b@ }@b@@b@ if (type.equals("none"))@b@ return "jdbc:none";@b@@b@ throw new SDBException("Don't know how to construct a JDBC URL for " + type);@b@ }@b@@b@ static@b@ {@b@ driver.put(DatabaseType.MySQL, "com.mysql.jdbc.Driver");@b@ driver.put(DatabaseType.PostgreSQL, "org.postgresql.Driver");@b@ driver.put(DatabaseType.H2, "org.h2.Driver");@b@ driver.put(DatabaseType.HSQLDB, "org.hsqldb.jdbcDriver");@b@ driver.put(DatabaseType.Derby, "org.apache.derby.jdbc.EmbeddedDriver");@b@@b@ driver.put(DatabaseType.SQLServer, "com.microsoft.sqlserver.jdbc.SQLServerDriver");@b@ driver.put(DatabaseType.Oracle, "oracle.jdbc.driver.OracleDriver");@b@ driver.put(DatabaseType.DB2, "com.ibm.db2.jcc.DB2Driver");@b@ driver.put(DatabaseType.SAP, "com.sap.db.jdbc.Driver");@b@ }@b@}
2.DatabaseType数据类型类
package com.hp.hpl.jena.sdb.store;@b@@b@import com.hp.hpl.jena.sdb.SDBException;@b@import com.hp.hpl.jena.sdb.shared.SymbolRegistry;@b@import com.hp.hpl.jena.sparql.util.Named;@b@import com.hp.hpl.jena.sparql.util.Symbol;@b@import java.util.List;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class DatabaseType extends Symbol@b@ implements Named@b@{@b@ public static final DatabaseType Derby = new DatabaseType("derby");@b@ public static final DatabaseType H2 = new DatabaseType("H2");@b@ public static final DatabaseType HSQLDB = new DatabaseType("HSQLDB");@b@ public static final DatabaseType MySQL = new DatabaseType("MySQL");@b@ public static final DatabaseType PostgreSQL = new DatabaseType("PostgreSQL");@b@ public static final DatabaseType SQLServer = new DatabaseType("SQLServer");@b@ public static final DatabaseType Oracle = new DatabaseType("Oracle");@b@ public static final DatabaseType DB2 = new DatabaseType("DB2");@b@ public static final DatabaseType SAP = new DatabaseType("sap");@b@ static SymbolRegistry<DatabaseType> registry = new SymbolRegistry();@b@@b@ public static DatabaseType fetch(String databaseTypeName)@b@ {@b@ if (databaseTypeName == null)@b@ throw new IllegalArgumentException("DatabaseType.convert: null not allowed");@b@@b@ DatabaseType t = (DatabaseType)registry.lookup(databaseTypeName);@b@ if (t != null) {@b@ return t;@b@ }@b@@b@ if (databaseTypeName.startsWith("oracle:"))@b@ return Oracle;@b@@b@ LoggerFactory.getLogger(DatabaseType.class).warn("Can't turn '" + databaseTypeName + "' into a database type");@b@ throw new SDBException("Can't turn '" + databaseTypeName + "' into a database type");@b@ }@b@@b@ static void init()@b@ {@b@ register(Derby);@b@ registerName("JavaDB", Derby);@b@@b@ register(HSQLDB);@b@ registerName("hsqldb:file", HSQLDB);@b@ registerName("hsqldb:mem", HSQLDB);@b@ registerName("hsql", HSQLDB);@b@@b@ register(H2);@b@ registerName("h2:file", H2);@b@ registerName("h2:mem", H2);@b@ registerName("h2:tcp", H2);@b@ registerName("h2", H2);@b@@b@ register(MySQL);@b@@b@ registerName("MySQL5", MySQL);@b@@b@ register(PostgreSQL);@b@@b@ register(SQLServer);@b@ registerName("MSSQLServer", SQLServer);@b@ registerName("MSSQLServerExpress", SQLServer);@b@@b@ register(Oracle);@b@@b@ register(DB2);@b@@b@ register(SAP); }@b@@b@ public static List<String> allNames() {@b@ return registry.allNames(); } @b@ public static List<DatabaseType> allTypes() { return registry.allSymbols();@b@ }@b@@b@ public static void register(String name) {@b@ if (name == null)@b@ throw new IllegalArgumentException("DatabaseType.register(String): null not allowed");@b@ register(new DatabaseType(name));@b@ }@b@@b@ public static void register(DatabaseType dbType)@b@ {@b@ if (dbType == null)@b@ throw new IllegalArgumentException("DatabaseType.register(DatabaseType): null not allowed");@b@ registry.register(dbType);@b@ }@b@@b@ public static void registerName(String databaseName, DatabaseType dbType)@b@ {@b@ if (dbType == null)@b@ throw new IllegalArgumentException("DatabaseType.registerName: null not allowed");@b@ registry.register(databaseName, dbType);@b@ }@b@@b@ private DatabaseType(String layoutName)@b@ {@b@ super(layoutName);@b@ }@b@@b@ public String getName()@b@ {@b@ return super.getSymbol();@b@ }@b@@b@ static@b@ {@b@ init();@b@ }@b@}