get Connection String for each different database system - Java java.sql

Java examples for java.sql:Connection

Description

get Connection String for each different database system

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String database = "java2s.com";
        String host = "java2s.com";
        String port = "java2s.com";
        String db = "java2s.com";
        System.out.println(getConnectionString(database, host, port, db));
    }/*w  ww.ja  v a2s .  co  m*/

    private static final String connectionDB2 = "jdbc:db2://<HOST>:<PORT>/<DB>";
    private static final String connectionJdbcOdbc = "jdbc:odbc:<DB>";
    private static final String connectionMsSQLServer = "jdbc:weblogic:mssqlserver4:<DB>@<HOST>:<PORT>";
    private static final String connectionMsSQLServerJTurbo = "jdbc:JTurbo://<HOST>:<PORT>/<DB>";
    private static final String connectionMsSQLServerSprinta = "jdbc:inetdae:<HOST>:<PORT>?database=<DB>";
    private static final String connectionMsSQLServer2000 = "jdbc:microsoft:sqlserver://<HOST>:<PORT>[;DatabaseName=<DB>]";
    private static final String connectionMySQL = "jdbc:mysql://<HOST>:<PORT>/<DB>";
    private static final String connectionOracle8i = "jdbc:oracle:oci8:@<SID>";
    private static final String connectionOracle9i = "jdbc:oracle:oci:@<SID>";
    private static final String connectionOracleThinSID = "jdbc:oracle:thin:@<HOST>:<PORT>:<SID>";
    private static final String connectionOracleThinService = "jdbc:oracle:thin:@<HOST>:<PORT>/<SERVICE>";
    private static final String connectionPointBase = "jdbc:pointbase://embedded[:<PORT>]/<DB>";
    private static final String connectionCloudscape = "jdbc:cloudscape:<DB>";
    private static final String connectionCloudscapeRmi = "jdbc:rmi://<HOST>:<PORT>/jdbc:cloudscape:<DB>";
    private static final String connectionFirebird = "jdbc:firebirdsql:[//<HOST>[:<PORT>]/]<DB>";
    private static final String connectionIdsServer = "jdbc:ids://<HOST>:<PORT>/conn?dsn='<DB>'";
    private static final String connectionInformixDynamic = "jdbc:informix-sqli://<HOST>:<PORT>/<DB>:INFORMIXSERVER=<DB>";
    private static final String connectionInstantDB_v3_13 = "jdbc:idb:<DB>";
    private static final String connectionInstantDB_v3_14 = "jdbc:idb:<DB>";
    private static final String connectionInterbase = "jdbc:interbase://<HOST>/<DB>";
    private static final String connectionHypersonicSQL_v1_2 = "jdbc:HypersonicSQL:<DB>";
    private static final String connectionHypersonicSQL_v1_3 = "jdbc:HypersonicSQL:<DB>";
    private static final String connectionPostgreSQL_v6_5 = "jdbc:postgresql://<HOST>:<PORT>/<DB>";
    private static final String connectionPostgreSQL_v7 = "jdbc:postgresql://<HOST>:<PORT>/<DB>";
    private static final String connectionSysbase_v4_2 = "jdbc:sybase:Tds:<HOST>:<PORT>";
    private static final String connectionSysbase_v5_2 = "jdbc:sybase:Tds:<HOST>:<PORT>";

    private static String getConnectionString(String database, String host,
            String port, String db) {
        String strConnect = null;
        switch (database.toUpperCase()) {
        case "CLOUDSCAPE":
            strConnect = connectionCloudscape;
            break;
        case "CLOUDSCAPE_RMI":
            strConnect = connectionCloudscapeRmi;
            break;
        case "DB2":
            strConnect = connectionDB2;
            break;
        case "FIREBIRD":
            strConnect = connectionFirebird;
            break;
        case "HYPERSONIC_v1_2":
            strConnect = connectionHypersonicSQL_v1_2;
            break;
        case "HYPERSONIC_v1_3":
            strConnect = connectionHypersonicSQL_v1_3;
            break;
        case "IDSSERVER":
            strConnect = connectionIdsServer;
            break;
        case "INFORMIX":
            strConnect = connectionInformixDynamic;
            break;
        case "INSTANTDB_V3_13":
            strConnect = connectionInstantDB_v3_13;
            break;
        case "INSTANTDB_V3_14":
            strConnect = connectionInstantDB_v3_14;
            break;
        case "INTERBASE":
            strConnect = connectionInterbase;
            break;
        case "JDBCODBC":
            strConnect = connectionJdbcOdbc;
            break;
        case "MSSQLSERVER":
            strConnect = connectionMsSQLServer;
            break;
        case "MSSQLSERVER2000":
            strConnect = connectionMsSQLServer2000;
            break;
        case "MSSQLSERVERJTURBO":
            strConnect = connectionMsSQLServerJTurbo;
            break;
        case "MSSQLSERVERSPRINTA":
            strConnect = connectionMsSQLServerSprinta;
            break;
        case "MYSQL":
            strConnect = connectionMySQL;
            break;
        case "ORACLE8I":
            strConnect = connectionOracle8i;
            break;
        case "ORACLE9I":
            strConnect = connectionOracle9i;
            break;
        case "ORACLETHINSID":
            strConnect = connectionOracleThinSID;
            break;
        case "ORACLETHINSERVICE":
            strConnect = connectionOracleThinService;
            break;
        case "POINTBASE":
            strConnect = connectionPointBase;
            break;
        case "POSTGRESQL_V6_5":
            strConnect = connectionPostgreSQL_v6_5;
            break;
        case "POSTGRESQL_V7":
            strConnect = connectionPostgreSQL_v7;
            break;
        case "SYSBASE_V4_2":
            strConnect = connectionSysbase_v4_2;
            break;
        case "SYSBASE_V5_2":
            strConnect = connectionSysbase_v5_2;
            break;
        default:
            //return null;
        }

        strConnect = strConnect.replace("<HOST>", host);
        strConnect = strConnect.replace("<PORT>", port);
        strConnect = strConnect.replace("<SERVICE>", db);
        strConnect = strConnect.replace("<DB>", db);
        strConnect = strConnect.replace("<SID>", db);

        return strConnect;
    }
}

Related Tutorials