![](https://csdnimg.cn/release/download_crawler_static/87936518/bg4.jpg)
private BufferedReader in;//定义输入流
private PrintWriter out;//定义输出流
int no;//定义申请的 jicq 号码
public ServerThread(Socket s) throws IOException {//线程构造函数
socket=s;//取得传递参数
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//创
建输入流
out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream())),true);//创建输出流
start();//启动线程
}
public void run(){//线程监听函数
try{ while(true){
String str=in.readLine();//取得输入字符串
if(str.equals("end"))break;//如果是结束就关闭连接
else if(str.equals("login")) {//如果是登录
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//连接数据库
Connection c=DriverManager.getConnection("jdbc:odbc:javaicq"," ","
");
String sql="select nickname,password from icq where icqno=?";
//准备从数据库选择呢称和密码
PreparedStatement prepare=c.prepareCall(sql);//设定数据库查寻条件
String icqno=in.readLine();
int g=Integer.parseInt(icqno);//取得输入的 jicq 号码
System.out.println(icqno);
String passwd=in.readLine().trim();//取得输入的密码
System.out.println(passwd);
prepare.clearParameters();
prepare.setInt(1,g);//设定参数
ResultSet r=prepare.executeQuery();//执行数据库查寻
if(r.next()){//以下比较输入的号码于密码是否相同
String pass=r.getString("password").trim();
System.out.println(pass);
if(passwd.regionMatches(0,pass,0,pass.length()))
{ out.println("ok");
//如果相同就告诉客户 ok
//并且更新数据库用户为在线
//以及注册用户的 ip 地址
//*************register ipaddress
String setip="update icq set ip=? where icqno=?";
PreparedStatement prest=c.prepareCall(setip);
prest.clearParameters();