如何通过套接字查询时发送结果?

时间:2014-08-11 14:59:33

标签: java database sockets

我通过socket有关于查询数据库的代码,但我不知道如何接收结果,任何人都可以帮助我。这是我的代码:

服务器

private void server() throws IOException, SQLException {

    try {

        ServerSocket server = new ServerSocket(1994);
        System.out.println("Server is ready...");
        socket = server.accept();

        DataInputStream in = new DataInputStream(socket.getInputStream());

         DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        String query = in.readUTF();
        pst = conn.prepareStatement(query);
        rs = pst.executeQuery();

        while(rs.next())
        {

            String ID = rs.getString("ID");
            out.writeUTF(ID);
            String Name= rs.getString("Name");
            out.writeUTF(Name);

        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    socket.close();
}

客户端

public class Client {

private void connect() throws ClassNotFoundException {
    ResultSet rs = null;
    try {

        Socket socket = new Socket("localhost", 1994);
        DataInputStream in = new DataInputStream(socket.getInputStream());
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("Select * from sinhvientest");
        String ID = null;
        String Name = null;
        String s;
        while(!(s = in.readUTF()).equals(null))
        {
            ID = in.readUTF();
            System.out.print(ID + " ");
            Name = in.readUTF();
            System.out.println(Name);
        }

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

连接到数据库是好的,从数据库查询是好的,我想发送结果,但我不知道如何。它只发送了一行。有人可以帮帮我吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

你写了两个String值:

        String ID = rs.getString("ID");
        out.writeUTF(ID);
        String Name= rs.getString("Name");
        out.writeUTF(Name);

但你读了三篇:

    while(!(s = in.readUTF()).equals(null))
    {
        ID = in.readUTF();
        System.out.print(ID + " ");
        Name = in.readUTF();
        System.out.println(Name);
    }

更好地使用

    try {
        while( true )
        {
            String ID = in.readUTF();
            String Name = in.readUTF();
            System.out.println(ID + " " + Name);
        }
     } catch( EOFException eof ){
        // handle regular end of file
     } catch( IOException ioe ){
        // error
     } catch( UTFDataFormatException dfe ){
        // error
     }

稍后以下是使用readUTF的客户端/服务器连接的非常不典型的示例。

public class Client {

public Client( String host, int port ) throws Exception {
    Socket socket = new Socket(host, port);
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream dis = new DataInputStream(socket.getInputStream() );
    dos.writeUTF( "a line from client" );
    try {
         while( true ){
            String line = dis.readUTF();
            System.out.println( "got: " + line );
         }
    } catch( Exception e ){
    }
    dis.close();
    dos.close();
}

public static void main(String args[]) throws Exception {
    String host = args[0];
    int port = Integer.parseInt( args[1] );
    Socket socket = null;
    Client client = new Client( host, port );
}
}

public class Server {
public static void main(String[] args) throws Exception {
    int portNumber = Integer.parseInt(args[0]);

    while (true) {
        try {
            new Server(portNumber);
        } catch (java.net.SocketException se) {
        } catch (IOException ioe) {
        } catch (ClassNotFoundException cnf) {
        }
    }
}

public Server( int port ) throws Exception {
ServerSocket serverSocket = new ServerSocket(port );
    Socket socket = serverSocket.accept();

    DataInputStream dis = new DataInputStream(socket.getInputStream());
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    System.out.println("...connected...waiting for data...");
    String line = dis.readUTF();
    System.out.println( "got:" + line );
    for( int i = 1; i <= 3; i++ ){
        dos.writeUTF( "line " + i + " from server" );
    }
    dis.close();
    dos.close();
}
}