import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class PS
{
public static void main(String[] a)
{
ServerSocket server = null;
server_thread thread;
Socket you = null;
System.out.println("服务器已经启动!");
while(true)
{
try
{
server = new ServerSocket(4331);
}
catch(IOException e){}
try
{
you=server.accept();
}
catch(IOException e){}
if(you!=null)
{
System.out.println("接收到来自客户端的一个请求!");
new server_thread(you).start();//是下面两条命令之和
//server_thread qq = new server_thread(you);
//qq.start();
}
else
{
continue;
}
}
}
}
class server_thread extends Thread
{
Socket socket;
Connection con = null;
Statement stmt = null;
DataInputStream in = null;
DataOutputStream out = null;
int n = 0;
String s = null;
server_thread(Socket t)
{
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1){}
try
{
con = DriverManager.getConnection("jdbc:odbc:san");
stmt = con.createStatement();
}
catch(SQLException e2){}
}
public void run()
{
while(true)
{
try
{
s = in.readUTF();
}
catch(IOException e){}
try
{
if(!(s.equalsIgnoreCase("Quit")))
{
n=0;
ResultSet rs = stmt.executeQuery("select * from danci");
while(rs.next())
{
String d1=rs.getString(2);
String d2=rs.getString(1);
if(s.equals(d2))//字符串比较
{
out.writeUTF(d1);
n=1;
break;
}
else if(s.equals(d1))//字符串比较
{
out.writeUTF(d2);
n=1;
break;
}
}
if(n==0)
{
out.writeUTF("没有该单词!");
}
}
else if(s.equalsIgnoreCase("Quit"))
{
out.writeUTF("你选择了退出!");
con.close();
//Thread.currentThread().yield();
break;
}
sleep(1);
}
catch(InterruptedException e1){}
catch(IOException e2){}
catch(SQLException e3){}
}
}
}