Here you can find the source of getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password)
Parameter | Description |
---|---|
dbType | a parameter |
dbName | a parameter |
userName | a parameter |
password | a parameter |
Parameter | Description |
---|---|
ClassNotFoundException | an exception |
IllegalAccessException | an exception |
InstantiationException | an exception |
SQLException | an exception |
public static Connection getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
//package com.java2s; //License from project: Open Source License import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { public static final String DBTYPE_MYSQL = "mysql"; public static final String DBTYPE_ORACLE = "oracle"; public static final String DBTYPE_POSTGRESQL = "postgresql"; public static final String DBTYPE_SQLSERVER = "sqlserver"; public static final String DBTYPE_DB2 = "db2"; /**/*from w ww . j a v a2 s .co m*/ * * @auther aaron * * @param dbType * @param dbName * @param userName * @param password * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SQLException */ public static Connection getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { String driver = getDriverName(dbType); String url = getURL(dbType, dbIp, dbPort, dbName); Class.forName(driver).newInstance(); return DriverManager.getConnection(url, userName, password); } public static String getDriverName(String dbType) { String driverName = ""; switch (dbType) { case DBTYPE_MYSQL: driverName = "com.mysql.jdbc.Driver"; break; case DBTYPE_ORACLE: driverName = "oracle.jdbc.driver.OracleDriver"; break; case DBTYPE_POSTGRESQL: driverName = "org.postgresql.Driver"; break; case DBTYPE_SQLSERVER: driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; break; case DBTYPE_DB2: driverName = "com.ibm.db2.jdbc.app.DB2Driver"; break; default: driverName = "unknow dbType"; break; } return driverName; } public static final String getURL(String dbType, String dbIp, String dbPort, String dbName) { String url = null; switch (dbType) { case DBTYPE_MYSQL: url = "jdbc:mysql://" + dbIp + ":" + dbPort + "/" + dbName + ""; break; case DBTYPE_ORACLE: url = "jdbc:oracle:thin:@" + dbIp + ":" + dbPort + ":" + dbName; break; case DBTYPE_POSTGRESQL: url = "jdbc:postgresql://" + dbIp + ":" + dbPort + "/" + dbName; break; default: url = ""; break; } return url; } }