我怎样把下面两个程序连接起来,实现把从手机端用户的注册信息保存到数据库中M_USER表中呢?
//服务器程序
import java.net.*;
import java.io.*;
import java.lang.*;
public class IMTouchServer extends Thread{
protected Socket client;
protected OutputStream os;
protected DataOutputStream dos;
protected InputStream is;
protected DataInputStream dis;
String str;
public IMTouchServer(Socket client)
{
this.client=client;
}
public static void main(String[] args)
{
long index=0;
ServerSocket server;
// String str=null;
// System.out.println(str.length());
try{
server=new ServerSocket(8888);
System.out.println("创建socket......");
while(true)
{
new IMTouchServer(server.accept()).start();
System.out.println("正在监听:"+(++index));
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void run()
{
System.out.println("running......");
try
{
is=client.getInputStream();
dis=new DataInputStream(is);
os=client.getOutputStream();
dos=new DataOutputStream(os);
int request=dis.readInt();
//注册
if(request==111)
{
System.out.println(dis.readUTF());
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
System.out.println(dis.readUTF());
dos.writeBoolean(true);
dos.writeUTF("恭喜您!您已经注册成功,请记住你的ID号:1234565");
dos.flush();
}
}
catch(Exception e)
{
System.out.println("error......"+e.toString());
}
}
}//连接数据库的程序
import java.sql.*; public class Select
{
public static void main(String args[])
{
String driverclass= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Stringurl="jdbc:microsoft:sqlserver://202.119.163.53:1433;DatabaseName=MIM";
Connection con;
Statement stmt;
String query= "select * from MIM..M_USER";
try
{
Class.forName(driverclass);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con = DriverManager.getConnection(url, "sa", "70826394");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query) System.out.println ("M_USER表中的数据如下");
while (rs.next())
{
String ID = rs.getString("ID");
String LOGINID= rs.getString("LOGINID");
String STATUS= rs.getString("STATUS");
String NIKENAME= rs.getString("NIKENAME");
String SEX= rs.getString("SEX");
String AGE = rs.getString("AGE");
String EMAIL = rs.getString("EMAIL");
String CITY = rs.getString("CITY");
System.out.println("用户ID: "+LOGINID);
System.out.println("状态: "+STATUS);
System.out.println("呢称: "+NIKENAME);
System.out.println("性别: "+SEX);
System.out.println("年龄: "+AGE);
System.out.println("Email: "+EMAIL);
System.out.println("城市: "+CITY);
}
stmt.close();
con.close(); }
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage()); }
}
}
//服务器程序
import java.net.*;
import java.io.*;
import java.lang.*;
public class IMTouchServer extends Thread{
protected Socket client;
protected OutputStream os;
protected DataOutputStream dos;
protected InputStream is;
protected DataInputStream dis;
String str;
public IMTouchServer(Socket client)
{
this.client=client;
}
public static void main(String[] args)
{
long index=0;
ServerSocket server;
// String str=null;
// System.out.println(str.length());
try{
server=new ServerSocket(8888);
System.out.println("创建socket......");
while(true)
{
new IMTouchServer(server.accept()).start();
System.out.println("正在监听:"+(++index));
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void run()
{
System.out.println("running......");
try
{
is=client.getInputStream();
dis=new DataInputStream(is);
os=client.getOutputStream();
dos=new DataOutputStream(os);
int request=dis.readInt();
//注册
if(request==111)
{
System.out.println(dis.readUTF());
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
System.out.println(dis.readUTF());
dos.writeBoolean(true);
dos.writeUTF("恭喜您!您已经注册成功,请记住你的ID号:1234565");
dos.flush();
}
}
catch(Exception e)
{
System.out.println("error......"+e.toString());
}
}
}//连接数据库的程序
import java.sql.*; public class Select
{
public static void main(String args[])
{
String driverclass= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Stringurl="jdbc:microsoft:sqlserver://202.119.163.53:1433;DatabaseName=MIM";
Connection con;
Statement stmt;
String query= "select * from MIM..M_USER";
try
{
Class.forName(driverclass);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con = DriverManager.getConnection(url, "sa", "70826394");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query) System.out.println ("M_USER表中的数据如下");
while (rs.next())
{
String ID = rs.getString("ID");
String LOGINID= rs.getString("LOGINID");
String STATUS= rs.getString("STATUS");
String NIKENAME= rs.getString("NIKENAME");
String SEX= rs.getString("SEX");
String AGE = rs.getString("AGE");
String EMAIL = rs.getString("EMAIL");
String CITY = rs.getString("CITY");
System.out.println("用户ID: "+LOGINID);
System.out.println("状态: "+STATUS);
System.out.println("呢称: "+NIKENAME);
System.out.println("性别: "+SEX);
System.out.println("年龄: "+AGE);
System.out.println("Email: "+EMAIL);
System.out.println("城市: "+CITY);
}
stmt.close();
con.close(); }
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage()); }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货