Source Code : Connect to more than one database
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Connect to more than one database
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();
}
}
}
}
Thank with us