0

I have the following class and when I run it I get an error which is:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "com.mysql.jdbc.JDBC4ResultSet@42147df5" 

The error occurs on this line:

fono = Integer.valueOf(fonoS);

This is the Code:

public Counter(){
    String query = "SELECT MAX(FONO) from forders";

 try {
    connectOrders.pst = connectOrders.con.prepareStatement(query);
    connectOrders.rs = connectOrders.pst.executeQuery(query);
    if (connectOrders.rs.next()){
        Object fonoO = connectOrders.rs;
        String fonoS= fonoO.toString();
        fono = Integer.valueOf(fonoS);
       try {
            serialisationFoNo(fono);
        } catch (IOException ex) {
            Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (connectOrders.rs.wasNull()){
        fono = 1;
        try {
            serialisationFoNo(fono);
        } catch (IOException ex) {
            Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    } catch (SQLException ex) {
     Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
     JOptionPane.showMessageDialog(null, ex);
    }
}

Why is it throwing this exception I have used the same syntax of code elsewhere in the application and it works.

NOTE: I have also tried using Integer.parseInt(); but even that throws a similar exception.

1
  • 3
    The message is pretty clear. You are trying to parse the toString representation of your object (which is com.mysql.jdbc.JDBC4ResultSet@42147df5) and that is obviously not a valid integer that you could parse. Commented Nov 28, 2014 at 11:05

6 Answers 6

1

You don't extract integer from your result set. First change your SQL to

"SELECT MAX(FONO) as m from forders"

and use

rs.getInt("m");

from resultset

Sign up to request clarification or add additional context in comments.

1 Comment

great that seems to work! Though im getting an error which says Too Many Connections.
1

The ResultSet hold all column values, which you can fetch by column index (counting from 1) or name.

Also for a PreparedStatement one does not add the query parameter, that was done in preparing.

  connectOrders.rs = connectOrders.pst.executeQuery();
  //String fonoS= rs.getString(1);
  fono = rs.getInt(1);

Close ResultSet and statement.

Comments

0

You are converting ResultSet object to String and then parsing it. Instead your code should be

int fono = connectOrders.rs.getInt(1);

Complete applicable code

public Counter(){
String query = "SELECT MAX(FONO) from forders";

 try {
    connectOrders.pst = connectOrders.con.prepareStatement(query);
    connectOrders.rs = connectOrders.pst.executeQuery(query);
    if (connectOrders.rs.next()){

//***********your code change here

        //Object fonoO = connectOrders.rs;
       // String fonoS= fonoO.toString();
      //  fono = Integer.valueOf(fonoS);

     fono = connectOrders.rs.getInt(1);


//**************

       try {
            serialisationFoNo(fono);
        } catch (IOException ex) {
            Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (connectOrders.rs.wasNull()){
        fono = 1;
        try {
            serialisationFoNo(fono);
        } catch (IOException ex) {
            Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    } catch (SQLException ex) {
     Logger.getLogger(Counter.class.getName()).log(Level.SEVERE, null, ex);
     JOptionPane.showMessageDialog(null, ex);
    }
}

Comments

0

You are trying to convert whole resultset into integer which is invalid

Object fonoO = connectOrders.rs;
String fonoS= fonoO.toString();
fono = Integer.valueOf(fonoS);

You could fetch the column from resultset which you think would be integer something on these lines using

String id = rs.getString(1);//if max(fono) in db is char but can be represented as integer or if its an int field you could do getInt("id") and store it in integer rather than string.
Integer idValue = Integer.valueOf(id);

Comments

0

I guess connectOrders.rs is a ResultSet variable

you are converting that one to string and then to integer which is wrong

the resultset holds a whole row of data (is a kind of a container), you probably want to get one of the values inside and convert that value to string/integer, like this

int x = connectOrders.rs.getInt(1); //get first field from the ResultSet

Comments

-1

Use a debugger.

Clearly the value of fonOS cannot be parsed as Integer in that instance.

From API:

The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.

1 Comment

Not the DV.. I guess the DV was expecting a more detailed answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.