I am trying to retrieve data from SYBASE database and copy retrieved data in a table in MySQL. I am able to connect both databases separately (i.e) using jTDS driver for SYBASE and Jdbc_driver for MySQL.
Now I want to connect both databases simultaneously in a single program. But I confused what should be written in Class.forName()
.
I have used Class.forName(JDBC_DRIVER);
for MySQL and Class.forName("net.sourceforge.jtds.jdbc.Driver");
for SYBASE.
Sybase:
public static void main(String[] args) {
String a;
String b;
String c;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:jtds:sybase://10.159.252.29:4100/fmdb","sa","Changeme_123");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("selecttbl_alm_log_2000000000.Csn,"
+ "tbl_alm_log_2000000000.IsCleared,"
+ "tbl_alm_log_2000000000.Id"
+ "From fmdb.dbo.tbl_alm_log_2000000000"
+ "Where IsCleared = 0");
while(rs.next()) {
a = rs.getString(1);
b = rs.getString(2);
c = rs.getString(3);
System.out.println(a+" "+b+" "+c);
}
con.close();
} catch(Exception e) {
System.out.println(e);
}
}
MySQL:
try {
Class.forName(JDBC_DRIVER);
System.out.println("connecting to database");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("connected to database successfully");
System.out.println("creating table in given database");
// stmt = conn.createStatement();
String sql = "CREATE TABLE newtable "
+ "(id INTEGER not NULL, "
+ "first VARCHAR(255), "
+ "PRIMARY KEY ( id ))";
stmt = conn.prepareStatement(sql);
stmt.executeUpdate(sql);
System.out.println("created table in database");
}
These are just snippets. I am just trying to merge above code. Help me by telling if this is possible or not and sharing some insights into this.
Multiple connections in a single program, can be created like this
public static void main(String[] args) {
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:jtds:sybase://10.159.252.29:4100/fmdb","sa","Changeme_123");
Class.forName(JDBC_DRIVER);
Connection con2 = DriverManager.getConnection(DB_URL, USER, PASS);
///After getting both connections, write your code
String a;
String b;
String c;
Statement stmt= con1.createStatement();
ResultSet rs=stmt.executeQuery("select tbl_alm_log_2000000000.Csn, tbl_alm_log_2000000000.IsCleared, tbl_alm_log_2000000000.Id From fmdb.dbo.tbl_alm_log_2000000000 Where IsCleared = 0");
while(rs.next()) ///If your query result is single row, use if instead of while
{
a = rs.getString(1);
b = rs.getString(2);
c = rs.getString(3);
System.out.println(a+" "+b+" "+c);
}
String sql = "CREATE TABLE newtable " + "(id INTEGER not NULL, " + "first VARCHAR(255), " + "PRIMARY KEY ( id ))";
stmt = con2.prepareStatement(sql);
stmt.executeUpdate(sql);
con1.close();
con2.close();
}catch(Exception e){ System.out.println(e);}
}
}