import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestConnectToMoreThanOneDatabase {
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:scorpian";
String username = "userName";
String password = "pass";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/tiger";
String username = "root";
String password = "root";
Class.forName(driver); // load MySQL driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args) {
Connection oracleConn = null;
Connection mysqlConn = null;
try {
oracleConn = getOracleConnection();
mysqlConn = getMySqlConnection();
System.out.println("oracleConn=" + oracleConn);
System.out.println("mysqlConn=" + mysqlConn);
} catch (Exception e) {
// handle the exception
e.printStackTrace();
System.exit(1);
} finally {
// release database resources
try {
oracleConn.close();
mysqlConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
|