import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String url = "jdbc:sqlserver://192.168.0.1:1433;DatabaseName=my";
String user = "sa";
String password = "123456";
String url1 = "jdbc:sqlserver://192.168.0.2:1433;DatabaseName=your";
String user1 = "sa";
String password1 = "654321";
Connection conn = null;
Connection conn1 = null;
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
conn = DriverManager.getConnection(url, user, password);
conn1 = DriverManager.getConnection(url1, user1, password1);
String sql1 = "select 欄目1,欄目2 from table1 where 欄目1=1";
Statement stmt = conn.createStatement();
Statement stmt1 = conn1.createStatement();
ResultSet rs1 = stmt1.executeQuery(sql1);
int i=0;
while(rs1!=null && rs1.next()){
String 欄目1_1 = rs1.getString("欄目1").trim();
String 欄目2_2 = rs1.getString("欄目2").trim();
String insertSql = " INSERT INTO table2 (欄目, 欄目) " +
"VALUES ('"+欄目1_1+"','"+欄目2_2+"') ";
stmt.executeUpdate(insertSql);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
conn1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}