Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't Send More Than One Query To The Server (Using Jdbc And Ocsf)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    01
    public void handleMessageFromClient (Object msg1, ConnectionToClient client) {
    02
                String msg=((String)msg1).toLowerCase();
    03
                if ( !( msg.startsWith("select") || msg.startsWith("insert") ||
    04
                        msg.startsWith("update") ) ) {
    05
                    System.out.println("Message received: " + msg + " from " + client);
    06
                    this.sendToAllClients(msg);
    07
                    return;
    08
                }
    09
                try {
    10
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
    11
                }
    12
                catch (Exception ex) {
    13
                    System.out.println("Couldn't load MySQL driver...");
    14
                }
    15
                try {
    16
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","Braude");
    17
                      System.out.println("Connected to the database");  // indication on server only
    18
                      Statement st = conn.createStatement();
    19
                      // user sent a "select" query
    20
                      if (msg.startsWith("select")){
    21
                          int isResultEmpty=1;  // for indicating if the query returned no results
    22
                          ResultSet rs=st.executeQuery(msg);
    23
                          ResultSetMetaData rsmd=rs.getMetaData();
    24
                          int numOfCols=rsmd.getColumnCount();
    25
                          Vector result=new Vector();
    26
                          while (rs.next()){
    27
                              String[] tuple=new String[numOfCols];
    28
                              for (int i=1;i<=numOfCols;i++)
    29
                                  tuple[i-1]=rs.getString(i);
    30
                              result.addElement(tuple);
    31
                              isResultEmpty=0;  // indicating at least one tuple in the result
    32
                          }
    33
                          if (isResultEmpty==0)
    34
                              this.sendToAllClients(result);
    35
                          else
    36
                              this.sendToAllClients("No appropriate results!");
    37
                          rs.close();
    38
                      }  // end of select case
    39
     
    40
                      // if user tries to only update the db (no resultset is returned)
    41
                      if (msg.startsWith("insert")  ||  msg.startsWith("update") ){
    42
                          st.executeUpdate(msg);
    43
                          this.sendToAllClients("Action successful!!");
    44
                      }  // end of update case
    45
                      st.close();
    46
                      conn.close();
    47
                }  // end of try
    48
                catch (SQLException ex) { // handle any errors
    49
                        this.sendToAllClients("SQLException: " + ex.getMessage());
    50
                        this.sendToAllClients("SQLState: " + ex.getSQLState());
    51
                        this.sendToAllClients("VendorError: " + ex.getErrorCode());
    52
                }
    53
     
    54
      } // 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:

    01
    public void handleMessageFromClientUI(String message)
    02
      {
    03
        try
    04
        {
    05
            sendToServer(message);
    06
        }
    07
        catch(IOException e)
    08
        {
    09
          clientUI.display
    10
            ("Could not send message to server.  Terminating client.");
    11
          quit();
    12
        }
    13
      }

    the sendToServer() method is inherited from the OCSF, i didn't touch it.
    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..


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Send More Than One Query To The Server (Using Jdbc And Ocsf)

    P.S.
    here is the stack trace of the exception:

    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)

Similar Threads

  1. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM
  2. [SOLVED] Server Client does not send file
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 12:38 PM
  3. Query
    By amol_kale in forum JDBC & Databases
    Replies: 0
    Last Post: February 18th, 2011, 07:04 AM
  4. Replies: 1
    Last Post: November 1st, 2009, 09:23 PM
  5. UDP server sends thread application
    By Koren3 in forum Threads
    Replies: 2
    Last Post: April 20th, 2009, 11:46 AM

Tags for this Thread