Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java服务器到客户端从MySql数据库获取信息_Java_Mysql - Fatal编程技术网

从Java服务器到客户端从MySql数据库获取信息

从Java服务器到客户端从MySql数据库获取信息,java,mysql,Java,Mysql,我一直在努力让客户端和服务器发送和接收正确的信息。程序应该能够将查询从客户端发送到服务器。然后,服务器应从数据库中检索必要的信息,并将其作为数组发送到客户端 Java服务器: public class Server { public static void main(String[] args) throws Exception { ServerSocket m_ServerSocket = new ServerSocket(1211, 2, InetAddres

我一直在努力让客户端和服务器发送和接收正确的信息。程序应该能够将查询从客户端发送到服务器。然后,服务器应从数据库中检索必要的信息,并将其作为数组发送到客户端

Java服务器:

    public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket m_ServerSocket = new ServerSocket(1211, 2, InetAddress.getLocalHost());
        int id = 0;
        while (true) {
            Socket clientSocket = m_ServerSocket.accept();
            System.out.println(clientSocket.isConnected());


            ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
            cliThread.start();
        }
     }
     }

    class ClientServiceThread extends Thread
    {
    ResultSet res = null;
    Connection conn;


    ObjectOutputStream objOut;
    PrintWriter   out;
    BufferedReader   in;

    Socket clientSocket;
    int clientID = -1;
    boolean running = true;

    ClientServiceThread(Socket s, int i)
    {
        clientSocket = s;
        clientID = i;
    }

    public void run()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/videostore", "root", "IronFire");

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        try 
        {
            System.out.println("Server");
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            objOut= new ObjectOutputStream(clientSocket.getOutputStream());

            while (running)
            {
                String command = in.readLine();
                System.out.println("Query:  " + command);
                if(command.substring(0, command.indexOf(" ")).equalsIgnoreCase("SELECT+"))
                {
                    System.out.println("IN IF"); // for test
                    surname(command);
                }
                if (command.equalsIgnoreCase("quit"))
                {
                    running = false;
                    System.out.print("Stopping client thread for client : " + clientID);
                }
                else 
                {
                    out.println(command);
                    out.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void surname(String query)
    {
        try
        {
            System.out.println("In Surname"); // for test
            int count = 0; 
            int whileCounter = 0;
            Statement state = conn.createStatement();

            res = state.executeQuery(query.replace("+", ""));

            while(res.next())
            {
                count++;
            }
            String [] stringResult = new String[count];
            byte[] buff = new byte[count];
            res.beforeFirst();

            while(res.next())
            {
                stringResult[whileCounter] = res.getString(1) +"  "+  res.getString(2) +"  " + res.getString(3)  +"  "+ res.getString(5)  +"  "+ res.getString(10) +"  "+ res.getString(11) +"";
                whileCounter++;
            }

            for(int i = 0; i < stringResult.length; i++)
            {
                System.out.println(stringResult[i]);
            }

            objOut.reset();
            objOut.writeObject(stringResult);
            objOut.flush();


            System.out.println("After object");
            //objOut.close();


        }
        catch(Exception ex)
        {

        }

    }
}
到目前为止,这个例外是我最大的问题

java.io.StreamCorruptedException: invalid type code: 53
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at FuncListener2$1.actionPerformed(FuncListener2.java:108)

您是想调用
readObject
两次吗?您正在检查它是否为空,但您是否应该保存它,以便不执行第二次读取?

我很少看到流
重置
java.io.StreamCorruptedException: invalid type code: 53
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at FuncListener2$1.actionPerformed(FuncListener2.java:108)