Solved

java.io.EOFException

Posted on 2004-10-28
289 Views
Last Modified: 2012-06-21
Hello everybody,

I am executing this Java progam in Linux and trying to retrieve some data from mySQL database which is on the windows machine.It will retrieve if it find new job(row) in the database table.
This is giving following run time error. Can anybody help me how to control this.The exception is as follows.please see below for my code.

===================
Exception ::
===================

Exception is com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.io.EOFException

STACKTRACE:

java.io.EOFException
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1619)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1828)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2179)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:629)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:1406)
        at com.mysql.jdbc.Connection.<init>(Connection.java:350)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:218)
        at java.sql.DriverManager.getConnection(DriverManager.java:525)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at DBToLinux.main(DBToLinux.java:21)

=======================

My Java code ::

===========
import java.sql.*;
import java.io.*;

public class DBToLinux
{
         
     public static void main(String args[])
     {    
          while(true)
          {    
             try    
             {          
                         
                    Class.forName("org.gjt.mm.mysql.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://129.217.184.62:3306/Thesiswork","root","");
                    Statement sta=con.createStatement();
                    Statement st1=con.createStatement();
                    Statement st2=con.createStatement();
                   
                    ResultSet rs=sta.executeQuery("select * from submitjobs where Processed=0");
                    //ResultSet rs=sta.executeQuery("Select execute from submitjobs");
                    while(rs.next())
                    {
                         
                         String exe=rs.getString("execute");
                         String Fn=rs.getString("filename");
                         
                         
                         int i=st1.executeUpdate("update submitjobs set Processed=1 where execute='"+exe+"' and filename='"+Fn +"'");
                         System.out.println("File Name is :"+Fn);
                         
                         String del2="delete from submitjobs where  Processed=1";
                         st2.executeUpdate(del2);    
                         
                         try
                          {
                            BufferedWriter out = new BufferedWriter(new FileWriter("PBSJob.txt"));
                            out.write(exe);
                            out.close();
                           
                            // System.exit(1);
                           
                        }
                         catch(EOFException eof1)
                         {
                                     System.out.println("Exception is:" + eof1);
                         }
                         catch (IOException e)
                         {
                             System.out.println("Exception is:"+ e);
                        }
                   
                        //}while(rs.next())
                   
                       
                       
                        //  **** To submit the Job file retrieved from database to PBS ****
                       
                        try
                         {
                              Process p = Runtime.getRuntime().exec("qsub PBSJob.txt");
                              InputStream str = p.getInputStream();
                              StringBuffer buff = new StringBuffer();
                              int c = str.read();
                              while(c > 0)
                              {
                                   buff.append((char)c);
                                   c = str.read();
                              }
                              System.out.println(buff.toString());
                         }
                        catch(EOFException eof1)
                         {
                                System.out.println("Exception is:" + eof1);
                         }
                        catch(IOException e1)
                         {
                             System.out.println("Exception is:"+ e1);
                         }
                       
                             
                         //  **** End of Job submission to PBS ****
                         
                         
                   
               } // while(rs.next())
                   
                   
        }// try
       
        catch(SQLException sql)
        {
                  System.out.println("Exception is "+sql);
        }
       
        catch(NullPointerException ne)
        {
                  System.out.println("Exception is "+ne);
        }
        catch(Exception e)
        {
                   e.printStackTrace();
       }//catch
     

       
       
     } // while for every minute
         
     } // main
     
} // class




0
Question by:ramsin
    7 Comments
     
    LVL 9

    Accepted Solution

    by:
    you will have to close the resultset:

                            //  **** To submit the Job file retrieved from database to PBS ****
                           
                            try
                             {
                                  Process p = Runtime.getRuntime().exec("qsub PBSJob.txt");
                                  InputStream str = p.getInputStream();
                                  StringBuffer buff = new StringBuffer();
                                  int c = str.read();
                                  while(c > 0)
                                  {
                                       buff.append((char)c);
                                       c = str.read();
                                  }
                                  System.out.println(buff.toString());
                             }
                            catch(EOFException eof1)
                             {
                                    System.out.println("Exception is:" + eof1);
                             }
                            catch(IOException e1)
                             {
                                 System.out.println("Exception is:"+ e1);
                             }
                           
                                 
                             //  **** End of Job submission to PBS ****
                             
                             
                       
                   } // while(rs.next())
                   rs.close();  <--------------------- here
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    It would help a bit if you could identify in your debug statements *where* the exception has occurred ...
    0
     
    LVL 86

    Assisted Solution

    by:CEHJ
    >>while(c > 0)

    should be

    while(c >= 0)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Or better

    while(c > -1)
    0
     
    LVL 12

    Assisted Solution

    by:Giant2
    put out of the while cycle these lines:
                        Class.forName("org.gjt.mm.mysql.Driver");
                        Connection con=DriverManager.getConnection("jdbc:mysql://129.217.184.62:3306/Thesiswork","root","");
    and put them in a try catch block.
    you can do the connection only one time.

    The code will be like the following:
    try{
                        Class.forName("org.gjt.mm.mysql.Driver");
                        Connection con=DriverManager.getConnection("jdbc:mysql://129.217.184.62:3306/Thesiswork","root","");
    while(true){
    ....
    }//end while
    }catch (Exception ex){System.out.println("ERROR IN THE CONNECTION "+ex);}


    The error I see (you post) is about the getConnection method.

    Bye, Giant.
    0
     
    LVL 92

    Assisted Solution

    by:objects
                                 Process p = Runtime.getRuntime().exec("qsub PBSJob.txt");
                                  InputStream str = p.getInputStream();
                                  StringBuffer buff = new StringBuffer();
                                  int c = str.read();
                                  while(c > 0)
                                  {
                                       buff.append((char)c);
                                       c = str.read();
                                  }
                                  System.out.println(buff.toString());

    instead of that code it is easier to use

                                  Process p = Runtime.getRuntime().exec("qsub PBSJob.txt");
                                  BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                  StringBuffer buff = new StringBuffer();
                                  while (null!=(line=in.readLine()))
                                  {
                                     buff.append(line);
                                     buff.append('\n');
                                  }
                                  System.out.println(buff.toString());

    0
     
    LVL 92

    Expert Comment

    by:objects
    do you get the exception everytime you run it?
    It could be caused by a network problem.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Title # Comments Views Activity
    Need some clarity on Rest API 5 37
    bunnyEars challenge 6 28
    Books that can get me started on JAVA 2 19
    pairstar challenge 2 10
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now