import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class serverduan {
private static final int port=4331;
private ServerSocket server;
private Socket socket;
DataInputStream in1;
DataOutputStream out1;
But1 but1;
int inbut;
public static void main(String args[])
{ serverduan a=new serverduan();
System.out.println(1);
a.kaishixuanzeanniu();
}
public serverduan(){
System.out.println(true); }
public void kaishixuanzeanniu() {
try
{ server=new ServerSocket(port);
socket=server.accept();
in1=new DataInputStream(socket.getInputStream());
out1=new DataOutputStream(socket.getOutputStream());
} catch (Exception e)
{
//System.out.println("服务器连接出现问题");
}
while(true){
try{inbut=in1.readInt();}catch (Exception e){System.out.println("按钮读入出现问题");}
System.out.println("等待选择按钮");
if(inbut==1)
{
System.out.println("按钮一的输入信号接收");
but1=new But1(socket);
but1.fasongshuzus();
}
if(inbut==2)
{ // Thread threadbut2=new Thread(new But2(socket));
System.out.println("按钮二的输入信号接收");
//threadbut2.start();
}
if(inbut==3)
{ // Thread threadbut3=new Thread(new But3(socket));
System.out.println("按钮三的输入信号接收");
//threadbut3.start();
}
}
}
}
class But1 {
private Socket socket;
Object shuzus[][];
private ObjectOutputStream outobj;
private ObjectInputStream inobj;
private Chaxun biao;
private ResultSet biaoSet;
private int lastnumber;
public But1 (Socket socket){
this.socket=socket;
System.out.println("初始化But1");
}
public void fasongshuzus(){
biao=new Chaxun();
try{biao.lianjie();}catch(Exception e){}
System.out.println("a");
try{biao.chset();}catch(Exception e){}
System.out.println("b");
try{biaoSet=biao.getRs();}catch(Exception e){}
System.out.println("c");
try{biaoSet.last();}catch(Exception e){}
try{lastnumber=biaoSet.getRow();}catch(Exception e){}
System.out.println("结果集shuzus行数 "+lastnumber);
shuzus=new Object[lastnumber][3];
int k=0;
try{biaoSet.beforeFirst();}catch(Exception e){} //第一行前面
try{while(biaoSet.next()){
//System.out.println(biaoSet.getString(0));
System.out.println(biaoSet.getString(1));
System.out.println(biaoSet.getString(2));
System.out.println(biaoSet.getString(3));
System.out.println(biaoSet.getString(4));
shuzus [k] [0] = (biaoSet.getString(1)); //到这里就卡死了。为什么!!!!!!!!!!!!11
System.out.println("shuzus["+k+"]"+"[0]内容: "+biaoSet.getString(1)); //
shuzus [k] [1] = (biaoSet.getString(2));
System.out.println("shuzus["+k+"]"+"[1]内容: "+biaoSet.getString(2)); //
shuzus [k] [2] = (biaoSet.getString(4));
System.out.println("shuzus["+k+"]"+"[2]内容: "+biaoSet.getString(4)); //
k++; }
inobj=new ObjectInputStream(socket.getInputStream());
outobj=new ObjectOutputStream(socket.getOutputStream());
outobj.writeObject(shuzus);
}catch(Exception e){}
}
}
class Chaxun{
private String str1,str2,str3,str4;
private ResultSet rs;
private boolean flag;
private PreparedStatement sqlpPreparedStatement;
private Statement sqlStatement;
private Connection con;
Chaxun(String str1,String str2,String str3,String str4)
{ sqlpPreparedStatement=null;
this.str1=str1;
this.str2=str2;
this.str3=str3;
this.str4=str4;
}
Chaxun()
{
}
public void lianjie()
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e){}
try{
String str="jdbc:odbc:PIMS";
con=DriverManager.getConnection(str, "test", "1234");
//sqlStatement=con.createStatement();
}catch(SQLException e){}
}
public void chset()
{ try
{String str;
sqlStatement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
str="Select * From account";
rs=sqlStatement.executeQuery(str);
while(rs.next())
{
System.out.println("账号:"+rs.getString(1));
}
} catch (SQLException e){}
}
public void setre()
{ try
{ String str;
flag=false;
str="INSERT INTO account(accountnumber,name,password,accountamount)"+"VALUES(?,?,?,?)";
sqlpPreparedStatement=con.prepareStatement(str);
sqlpPreparedStatement.setString(1,str1);
sqlpPreparedStatement.setString(2,str2);
sqlpPreparedStatement.setString(3,str3);
sqlpPreparedStatement.setString(4,str4);
sqlpPreparedStatement.execute();
System.out.println("执行插入成功");
flag=true;
} catch (Exception e){}
}
public ResultSet getRs()
{ System.out.println("取得查询结果");
return rs;
}
public boolean getflag()
{
return flag;
}
public void closedata()
{try
{con.close();} catch (Exception e)
{}
}
}
求指导 在上面