| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 551 人关注过本帖
标题:[讨论]远程不同数据库系统中数据的传送!
取消只看楼主 加入收藏
xianfeng_28
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-12-6
收藏
 问题点数:0 回复次数:3 
[讨论]远程不同数据库系统中数据的传送!

小弟我在一公司刚开始实习,今天带我的哥们给我布置了一道题:要求用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-03-13 14:58
xianfeng_28
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-12-6
收藏
得分:0 

怎么没人回呢?郁闷!

[此贴子已经被作者于2006-3-14 9:52:32编辑过]

2006-03-14 09:46
xianfeng_28
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-12-6
收藏
得分:0 
好在我这边还是有进展!做完了在于大家分享!
2006-03-14 09:48
xianfeng_28
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-12-6
收藏
得分:0 

运用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;
}

}

}

2006-03-15 14:07
快速回复:[讨论]远程不同数据库系统中数据的传送!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024561 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved