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