Problem with mysql connection in java

1

I have this code to connect to a bd but the problem is that it does not enter the while loop

public static void conf(){
    System.out.println("Ingrese serial");
    String numSerial = sc.nextLine();
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/seriales","root","");
        PreparedStatement ps = con.prepareStatement("SELECT usado FROM serial WHERE serial = ?");
        ps.setString(1, numSerial);
        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            if(rs.equals(0)){
                System.out.println("No usado");
            }else if(rs.equals(1)){
                System.out.println("Usado");
            }
        }
        con.close();
    }catch(Exception ex){
        System.out.println(ex);
    }
}

}

What can be the fault.

    
asked by bruno Diaz martin 20.07.2016 в 13:46
source

2 answers

2

First, does the program throw an exception? In this case, I recommend that, in addition to System.out.println(ex); you add ex.printStackTrace(System.out); to block catch . This will allow you to identify the exact place where the exception is thrown.

On the other hand, you are not recovering any value of ResultSet ; To read values from a ResultSet you must use one of the getXXX() methods. Assuming that the value of the column usado is integer, you can use:

if(rs.getInt(1) == 1) {  // También puede ser: rs.getInt("usado");
    //...
} else {
    //...
}

Remember: When you use JDBC, the columns of a ResultSet are numbered starting from 1.

Now, to make sure that ResultSet has records, I suggest the following corrections:

PreparedStatement ps = con.prepareStatement(
                           "SELECT usado FROM serial WHERE serial = ?"
                           , ResultSet.TYPE_SCROLL_SENSITIVE    // Esto asegura que el ResultSet pueda ser recorrido en cualquier dirección (adelante o atrás)
                           , ResultSet.CONCUR_READ_ONLY    // Esto asegura que el ResultSet sea de sólo lectura
                       );
ps.setString(1, numSerial);
ResultSet rs = ps.executeQuery();
rs.beforeFirst();    // Coloca el cursor ANTES de la primera fila del ResultSet
                     // (para esto es que se requiere la opción TYPE_SCROLL_SENSITIVE)
/* Si esperas que el ResultSet tenga una sola fila, puedes hacer esto: */
if(rs.first()) {
    if(rs.getInt("usado") == 1) {
        System.out.println("Usado");
    } else {
        System.out.println("No usado");
    }
} else {
    System.out.println("No hay registros coincidentes");
}
    
answered by 20.07.2016 / 20:23
source
0

The ruling I think can be for 2 things:

1 - The Select query is not returning anything.

2 - The comparison you make with equals if(rs.equals(0)) may not do what you expect.

Remember that equals only returns true if the two objects are of the same type and equal, for example:

if("1".equals(1)) -> FALSO

if("1".equals("1")) -> TRUE

To make conversions:

int numero = Integer.parseInt("1"); //Convierte un String en int
String texto = String.valueOf(2); //Convierte un int en String
    
answered by 20.07.2016 в 16:25