Results 1 to 13 of 13
  1. #1
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

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

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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?

  3. #3
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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:

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Run this on both machines: "java -version". Does the output match?

  5. #5
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    What CachedRowSetImpl is the above code using?
    Is it the same as that used by the server?

  8. #8
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    both client and server are using same CachedRowSetImpl.

    everything is same. :confused:

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Which one is it?

  10. #10
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    And what jdbc driver is the server side using (against what database)?

  12. #12
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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 ?

  13. #13
    gagangujral is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

Posting Permissions

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