Copy One DB to Another DB:-
package com;
package com;
import java.io.*;
import java.sql.*;
/**
*
* @author Akhilesh
*/
public class DBCopy {
public static void main(String args[])
throws SQLException, ClassNotFoundException, IOException {
//excel connection
Connection conDb1 =
prepareConnectionDB1();
Statement stmtDb1 =
conDb1.createStatement();
String queryDb1 = "select * from
[Sheet1$]";
ResultSet rsDb1 =
stmtDb1.executeQuery(queryDb1);
// mysql connection
Connection conDb2 =
prepareConnectionDB2();
Statement stmtDb2 =
conDb2.createStatement();
//String queryDb2a =
"Create table data(word varchar(45),meaning varchar(300))";
//stmtDb2.executeUpdate(queryDb2a);
ResultSet rsDb2 = null;
System.out.println("table
created");
String queryDb2b = "select * from
data";
rsDb2 = stmtDb2.executeQuery(queryDb2b);
System.out.println("Words\t\tMeanings");
while (rsDb2.next()) {
System.out.println(rsDb2.getString("word") + "\t" +
rsDb2.getString("meaning"));
}
rsDb1.close();
conDb1.close();
rsDb2.close();
conDb2.close();
}//main
public static Connection
prepareConnectionDB1() throws SQLException, ClassNotFoundException {
String driverClassName =
"sun.jdbc.odbc.JdbcOdbcDriver";
String url =
"jdbc:odbc:mydsn";
Class.forName(driverClassName);
return
DriverManager.getConnection(url);
}//prepareConnectionDB1
public static Connection
prepareConnectionDB2() throws SQLException, ClassNotFoundException {
String driverClassName =
"com.mysql.jdbc.Driver";
String url =
"jdbc:mysql://localhost:3306/dictionary";
String userName = "root";
String password = "root";
Class.forName(driverClassName);
return DriverManager.getConnection(url,
userName, password);
}//prepareConnectionDB2
}//class
No comments:
Post a Comment