Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can't send more than one query to the server (using jdbc and ocsf)

    hello,
    i'm using JDBC and OCSF to make client-server connection including MySQL.
    i'm now stuck at a very strange problem:
    my server can receive either "select..." string, "insert..." string, "update..." string (those are treated as queries), or any other string, which is treated as a simple message to the server (i do nothing with in so far)

    here is the code of the server:

    Code:
    public void handleMessageFromClient (Object msg1, ConnectionToClient client) {
                String msg=((String)msg1).toLowerCase();
                if ( !( msg.startsWith("select") || msg.startsWith("insert") ||
                        msg.startsWith("update") ) ) {
                    System.out.println("Message received: " + msg + " from " + client);
                    this.sendToAllClients(msg);
                    return;
                }
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                }
                catch (Exception ex) {
                    System.out.println("Couldn't load MySQL driver...");
                }
                try {
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","Braude");
                      System.out.println("Connected to the database");  // indication on server only
                      Statement st = conn.createStatement();
                      // user sent a "select" query
                      if (msg.startsWith("select")){
                          int isResultEmpty=1;  // for indicating if the query returned no results
                          ResultSet rs=st.executeQuery(msg);
                          ResultSetMetaData rsmd=rs.getMetaData();
                          int numOfCols=rsmd.getColumnCount();
                          Vector result=new Vector();
                          while (rs.next()){
                              String[] tuple=new String[numOfCols];
                              for (int i=1;i<=numOfCols;i++)
                                  tuple[i-1]=rs.getString(i);
                              result.addElement(tuple);
                              isResultEmpty=0;  // indicating at least one tuple in the result
                          }
                          if (isResultEmpty==0)
                              this.sendToAllClients(result);
                          else
                              this.sendToAllClients("No appropriate results!");
                          rs.close();
                      }  // end of select case
    
                      // if user tries to only update the db (no resultset is returned)
                      if (msg.startsWith("insert")  ||  msg.startsWith("update") ){
                          st.executeUpdate(msg);
                          this.sendToAllClients("Action successful!!");
                      }  // end of update case
                      st.close();
                      conn.close();
                }  // end of try
                catch (SQLException ex) { // handle any errors
                        this.sendToAllClients("SQLException: " + ex.getMessage());
                        this.sendToAllClients("SQLState: " + ex.getSQLState());
                        this.sendToAllClients("VendorError: " + ex.getErrorCode());
                }
    
      } // end of function
    the problem is as follows:
    if i send any series of simple messages (one or more), everything works fine.
    when i send my first query (any of them, select / update / insert) , it executes.
    but, after i send the first query, trying to send any message (query / simple one) ,
    i get an error message, thrown by this code:

    Code:
    public void handleMessageFromClientUI(String message)
      {
        try
        {
            sendToServer(message);
        }
        catch(IOException e)
        {
          clientUI.display
            ("Could not send message to server.  Terminating client.");
          quit();
        }
      }
    the sendToServer() method is inherited from the OCSF, i didn't touch it.

    here is the stack trace of the exception:
    Code:
    java.net.SocketException: socket does not exist 
    at ocsf.client.AbstractClient.sendToServer(AbstractClient.java:141) 
    at client.ChatClient.handleMessageFromClientUI(ChatClient.java:85) 
    at ClientConsole.accept(ClientConsole.java:77) 
    at ClientConsole.main(ClientConsole.java:121)
    For conclusion, the problem is:
    after sending simple messages, all fine. after sending a query, nothing else can be sent.
    any idea will be very appreciated, I don't have a clue about what's wrong here..
    Last edited by webking12; 04-25-2011 at 10:03 PM. Reason: adding the stack trace of the exception


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •