Java JDBC Oracle Connection getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password)

Here you can find the source of getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password)

Description

get Connection

License

Open Source License

Parameter

Parameter Description
dbType a parameter
dbName a parameter
userName a parameter
password a parameter

Exception

Parameter Description
ClassNotFoundException an exception
IllegalAccessException an exception
InstantiationException an exception
SQLException an exception

Declaration

public static Connection getConnection(String dbType, String dbIp, String dbPort, String dbName,
        String userName, String password)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 

Method Source Code


//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;
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection(String connectString)
  6. getConnection(String ip, String serviceName, String uid, String pwd)
  7. getDatabaseConnection()
  8. getDBVersion(String dbSourceName, String server, int port, String dbName, String userid, String pwd)
  9. getJDBCConn(String hostname, String port, String sid, String username, String pwd)