Results 1 to 13 of 13
Thread: java.io.optionaldataexception
- 05-06-2011, 09:18 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
java.io.optionaldataexception
I created a client server program with client passing the queries to server.
Scene 1: when client and server on same machine
in this scenario, everything works perfect, i get the expected output.
Scene 2: When client and server on different system
in this scenario, i connected two pc's using lan wire and they can ping each other successfully.
the catch is when i try to send the query from client, it shows the error java.io.optionaldataexception. what to do ?
when i searched about this, what i could make out of all the results is that my stream is trying to read some data which is not there.
i'm adding the code, just the part which uses streams:
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: try { errvect = new java.util.Vector(); resultvect = new java.util.Vector(); queryvect = new java.util.Vector(); errvect.clear(); resultvect.clear(); queryvect.clear(); String query = jTextArea1.getSelectedText(); client = new Socket(address,port); input = new BufferedReader(new InputStreamReader(client.getInputStream())); output = new PrintWriter(client.getOutputStream(),true); ois = new ObjectInputStream(client.getInputStream()); output.println("Verify"); output.println(username); output.println(password); String msg = input.readLine(); if(msg.equals("accepted")) { output.println("Query"); output.println(query); while(!msg.equals("Final")) { msg = input.readLine(); if(msg.equals("Successful")) { String query2 = input.readLine(); CachedRowSetImpl crs = new CachedRowSetImpl(); crs = (CachedRowSetImpl)ois.readObject(); resultvect.addElement(crs); queryvect.addElement(query2); } if(msg.equals("Update")) { String query2 = input.readLine(); String result = input.readLine(); errvect.addElement("Result of Query : "+query2+" -> "+result); } if(msg.equals("Invalid Query")) { javax.swing.JOptionPane.showMessageDialog(null,"Invalid Query Format","Error!!",javax.swing.JOptionPane.ERROR_MESSAGE); break; } if(msg.equals("rejected")) { msg = input.readLine(); errvect.addElement(msg); break; } if(msg.equals("Failure")) { msg=input.readLine(); errvect.addElement(msg); } } for(int i=0;i<queryvect.size();i++) { CachedRowSetImpl crs = new CachedRowSetImpl(); crs = (CachedRowSetImpl)resultvect.elementAt(i); RowSetMetaData rsmd = (RowSetMetaData)crs.getMetaData(); int count = rsmd.getColumnCount(); String[] labels = new String[count]; for(int j=1;j<=count;j++) labels[j-1] = rsmd.getColumnName(j); int rowcount = crs.size(); String[][] data = new String [rowcount][count]; int j=0; while(crs.next()) { for(int col = 1; col <= count; col++) { data[j][col-1] = crs.getString(col); } j++; } javax.swing.JTable table = new javax.swing.JTable(data, labels); table.setEnabled(false); javax.swing.JLabel label = new javax.swing.JLabel("Result of query :"+(String)queryvect.elementAt(i)); jPanel1.add(label,-1); jPanel1.add(table,-1); jPanel1.revalidate(); } for(int i=0;i<errvect.size();i++) { javax.swing.JLabel label = new javax.swing.JLabel((String)errvect.elementAt(i)); jPanel2.add(label,-1); jPanel2.revalidate(); } } }catch(Exception e){System.out.println("Query Exception"+e);} }
P.S: I saw 1 strange thing when i use ping command from client to server it shows TTL=128ms and server to client it shows TTL=64ms, could that be the issue ?
- 05-06-2011, 10:12 PM #2
Have you had a look at OptionalDataException (Java Platform SE 6) ? It seems to point towards you trying to send a serialised object that doesn't matck up on both server and client (different Java-versions, perhaps?). Does anything at all get sent between server/client (most notably the login)? If so, Where does it fail?
- 05-11-2011, 01:12 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
yea man! i saw details of that exception.
part working: client can establish the connection using a user form which is successfully working.
user can view the name of databases, tables available( which my application intends to do)
BUT
when i try running a simple sql query it shows this error. which is not present when i do all the things on same machine.
i know this is not enough info from my side, but please guys, help me out if you can. ASAP. i gotta submit this tommorow. :(:confused:
- 05-11-2011, 01:28 PM #4
Run this on both machines: "java -version". Does the output match?
- 05-11-2011, 02:24 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
yes! i already did what you say. both machine running on same java version.
after some trouble shooting, i know the error is in line that says
crs = (CachedRowSetImpl)ois.readObject();
and this time the error was:
java.io.StreamCorruptedException: invalid type code: 00
- 05-11-2011, 02:32 PM #6
Since everything runs fine if everything runs on the same machine, odds are that there is some version difference somewhere. Perhaps the JDBC isn't the same on both machines, since it fails as soon as you try to read a database-object? If all versions of everything checks out, I'm somewhat out of ideas, unfortunately.
- 05-11-2011, 03:39 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What CachedRowSetImpl is the above code using?
Is it the same as that used by the server?
- 05-11-2011, 03:54 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
both client and server are using same CachedRowSetImpl.
everything is same. :confused:
- 05-11-2011, 04:16 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Which one is it?
- 05-11-2011, 04:24 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
i'm using com.sun.rowset.CachedRowSetImpl
though it gives me a warning that this feature may be removed in a future release(in both the cases, single machine and two machines). but my program is still working when run on same machine.
- 05-11-2011, 04:27 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
And what jdbc driver is the server side using (against what database)?
- 05-11-2011, 04:31 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
even server side is using same JDBC
Java Code:Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/database1"; con = DriverManager.getConnection(url, "root","gujral");
should i try using jtds ?
- 05-11-2011, 05:09 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks