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..