BEE
BEE

Reputation: 107

Java String to Integer No Working?

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.

Upvotes: 0

Views: 311

Answers (6)

J-W
J-W

Reputation: 418

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

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109623

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.

Upvotes: 1

SMA
SMA

Reputation: 37103

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);

Upvotes: 0

sidgate
sidgate

Reputation: 15254

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);
    }
}

Upvotes: 0

DontRelaX
DontRelaX

Reputation: 754

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

Upvotes: 1

Mena
Mena

Reputation: 48444

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.

Upvotes: -1

Related Questions