create Derby, MySQL, Oracle Connection - Java java.sql

Java examples for java.sql:MySQL

Description

create Derby, MySQL, Oracle Connection

Demo Code


//package com.java2s;

import java.sql.*;
import java.util.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        String user = "java2s.com";
        String pwd = "java2s.com";
        String host = "java2s.com";
        String port = "java2s.com";
        String instanceName = "java2s.com";
        System.out.println(createDerbyConnection(user, pwd, host, port,
                instanceName));/*  ww w . j  a va 2 s.c  o  m*/
    }

    static String dbName = "";
    private static String dbError = "";
    protected static Map<String, String> loginDetails = new HashMap<String, String>();

    public static Object createDerbyConnection(String user, String pwd,
            String host, String port, String instanceName) {
        loginDetails.put("user", user);
        loginDetails.put("password", pwd);
        loginDetails.put("host", host);
        loginDetails.put("port", port);
        loginDetails.put("instance", instanceName);
        loginDetails.put("database", "Derby");
        Connection connection = getDatabaseConnectionFor("Derby");
        boolean result;
        if (connection != null) {
            try {
                connection.close();
                return true;
            } catch (Exception e) {
            }
        }
        return dbError; // when no connection was established
    }

    public static Connection getDatabaseConnectionFor(
            String databaseToConnect) {
        String user = loginDetails.get("user");
        String pwd = loginDetails.get("password");
        String host = loginDetails.get("host");
        String port = loginDetails.get("port");
        String instanceName = loginDetails.get("instance");
        dbName = instanceName;
        String driverName = "";
        String url = "";
        Connection connection = null;

        try {
            if (databaseToConnect.equals("Oracle")) {
                // ORACLE CONNECTION
                driverName = "oracle.jdbc.driver.OracleDriver";
                url = "jdbc:oracle:thin:" + user + "/" + pwd + "@" + host
                        + ":" + port + ":" + instanceName;
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);

            } else if (databaseToConnect.equals("Derby")) {
                // DERBY CONNECTION

                driverName = "com.ibm.db2.jcc.DB2Driver";
                url = "jdbc:derby:net:" + "//" + host + ":" + port + "/"
                        + instanceName;
                Class.forName(driverName);
                connection = DriverManager.getConnection(url, user, pwd);
            } else if (databaseToConnect.equals("Mysql")) {
                // MySQL Connection
                driverName = "com.mysql.jdbc.Driver";
                url = "jdbc:mysql://user:pwd@host:port/instanceName";
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);

            } else if (databaseToConnect.equals("Mssql")) {
                // Microsoft SQL Server
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);
            } else if (databaseToConnect.equals("Db2")) {
                // DB2 Connection
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);
                Class.forName(driverName);
                connection = DriverManager.getConnection(url);

            }
        } catch (Exception e) {
            dbError = e.getLocalizedMessage(); // saving the error
        }

        return connection;
    }
}

Related Tutorials