小弟我在一公司刚开始实习,今天带我的哥们给我布置了一道题:要求用java写一段代码实现将本地Mysql 数据库中的一表中数据拷贝到一远程sqlserver 数据库中。小弟我这样实现了其功能:
package pack27;
import java.sql.*;
public class ConnDataBase{
public static void main(String args[]){
try{
//打开mysql数据库
Class.forName("com.mysql.jdbc.Driver"); //初始化并加载JDBC驱动程序
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/client?useUnicode=true&characterEncoding=gb2312","sundy","sundy");
//创建连接对象
Statement db = conn.createStatement(); //创建一个简单的statement对象
ResultSet result = db.executeQuery("select * from users"); //取出本地mysql数据中数据
//向SQLSERVER拷贝数据
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection connsqlserver = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=Client","sa","1");
Statement dbsqlserver = connsqlserver.createStatement();
//System.out.println("mysql 里面的数据:");
while(result.next()){ //循环向sqlserver数据库中插入数据
//System.out.println(result.getString("username"));
int value = dbsqlserver.executeUpdate("insert into users(username,password) values( '" + result.getString("username") + "','" + result.getString("password") +"')");
}
System.out.println("数据拷贝成功");
} catch(Exception e){
System.out.println("数据库连接出错:" + e);
}
}
}
功能基本实现了,可是那个们说真正运行到远程数据库上会很慢,相信大家一定还有没有更好的方法!相互讨论一下!ok?!
----------------解决方案--------------------------------------------------------
怎么没人回呢?郁闷!
[此贴子已经被作者于2006-3-14 9:52:32编辑过]
----------------解决方案--------------------------------------------------------
好在我这边还是有进展!做完了在于大家分享!
----------------解决方案--------------------------------------------------------
小弟还未入门道,不懂这些
----------------解决方案--------------------------------------------------------
运用socket,i/o,线程等,终于基本上成功了!虽然人气不高,我还是贴完吧!
package pack28;
import java.sql.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//***********************本地客户端***********************
public class ConnRemoteDatabaes {
public ConnRemoteDatabaes() {
}
public static void main(String[] args) {
Socket socket = null;
try{
//打开mysql数据库
Class.forName("com.mysql.jdbc.Driver"); //初始化并加载JDBC驱动程序
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/client?useUnicode=true&characterEncoding=gb2312","sundy","sundy");
//创建连接对象
Statement db = conn.createStatement(); //创建一个简单的statement对象
ResultSet result = db.executeQuery("select * from users");
socket = new Socket("127.0.0.1",3000);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
StringBuffer sb = new StringBuffer();
while(result.next()){
sb.append(result.getString("username").toString()+ "*" +result.getString("password").toString() + "\n");
}
pw.print(sb);
pw.flush();
socket.close();
System.out.println("数据传输完毕!");
} catch(Exception e){
e.printStackTrace();
}finally{
if(socket != null)
try{
socket.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
}
}
package pack28;
import java.sql.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//*******************服务端****************************
public class SavaData {
public SavaData() {
}
public static void main(String[] args) {
//向SQLSERVER拷贝数据
try{
ServerSocket serversocket = null;
try{
serversocket = new ServerSocket(3000);
System.out.println("开始监听3000端口: ");
} catch(IOException ex1){
System.out.println("不能监听3000端口: "+ ex1);
System.exit( -1);
}
while(true){
Socket socket = serversocket.accept();
new ThreadData(socket).start();
}
} catch(Exception ex){
}
}
}
package pack28;
import java.net.*;
import java.io.*;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//**********************线程体**********************
public class ThreadData extends Thread{
private Socket socket;
public ThreadData(Socket socket){
this.socket = socket;
}
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection connsqlserver = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=Client","sa","1");
Statement dbsqlserver = connsqlserver.createStatement();
System.out.println("username" + "\t\t" + "password");
while((line = br.readLine()) != null && line.length() > 2){
Conver conver = new Conver();
line = conver.converStr(line);
int a = line.indexOf('*');
String username = line.substring(0,a);
String password = line.substring(a + 1);
System.out.println(username + "\t\t" + password);
int value = dbsqlserver.executeUpdate("insert into users(username,password) values( '" + username + "','" + password + "')");
}
socket.close();
System.out.println("数据保存完毕!");
} catch(Exception ex){
ex.printStackTrace();
}
}
}
package pack28;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//**********************提取中文********************
public class Conver{
public Conver(){
}
public String converStr(String constr){
try{
byte[] b;
String utf8_value;
utf8_value = constr; //从HTTP流中取"NAME"的UTF8数据
b = utf8_value.getBytes("8859_1"); //中间用ISO-8859-1过渡
String endstr = new String(b,"GB2312"); //转换成GB2312字符
return endstr;
} catch(UnsupportedEncodingException ex){
ex.printStackTrace();
return null;
}
}
}
----------------解决方案--------------------------------------------------------
这么好的帖子怎么没人顶呢,搂主实在是太感谢了,我也正在解决这个问题呢,哈哈!
----------------解决方案--------------------------------------------------------
不错了 顶
----------------解决方案--------------------------------------------------------