MySQL does not return data to JAVA

0

I am trying to make a query to my DB but it does not return anything, no error, or some other way of doing it

public class test {

    public static void main(String[] args) throws SQLException {
        coneccion con = new coneccion();
        Connection c = con.conectar();
        Statement s = c.createStatement();
        String sql = String.format("select * from administrador where PERSONA_CodiPers='10020000' and ContrAdmi='123456'");
        ResultSet res = s.executeQuery(sql);
        JOptionPane.showMessageDialog(null, "op3");
        //System.out.println(sql);
        int rows = res.getRow();
//            res.last();
//            rows = res.getRow();
//            res.beforeFirst();

        JOptionPane.showMessageDialog(null, String.valueOf(rows));

    }
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class coneccion {

    public Connection conectar = null;

    public Connection conectar() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conectar = DriverManager.getConnection("jdbc:mysql://localhost:3306/colegio1", "root", "123456");

        } catch (ClassNotFoundException | SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return conectar;
    }

    public void desconectar() {
        try {
            conectar.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

}

    
asked by user75463 15.12.2018 в 23:50
source

2 answers

0

I want to think that what you want to print in the JOption is that it is "active", what you have to do is:

res.next();
JOptionPane.showMessageDialog(null, res.getString("EstaAdmi"));

since what you are printing is the Row number where the reading begins. "res.next ();" what it does is move to the next row that is where your result is, and "res.getString (" ThisAdmi ");" is the name of the column along with the type of data you want to print.

    
answered by 16.12.2018 в 01:20
0

the problem is not the select but the method you are using

  

res.getRow (): Retrieve the current row number. The first row is number 1, the second number 2, and so on.   Note: Support for the getRow method is optional for ResultSets with a type of result set TYPE_FORWARD_ONLY   Returns:   the current row number; 0 if there is no current row   Shots

Saying does not return the number of rows affected, you could use this method to count the rows.

public class test {

 public static void main(String[] args) throws SQLException {
    coneccion con = new coneccion();
    Connection c = con.conectar();
    Statement s = c.createStatement();
    String sql = String.format("select * from administrador where PERSONA_CodiPers='10020000' and ContrAdmi='123456'");
    ResultSet res = s.executeQuery(sql);

    int total = 0;
    while (res.next()){
    //Obtienes la data que necesitas...Ejemplo
    System.err.println("EstaAdmi:"+res.getString("EstaAdmi"));
    total++;
    }

    JOptionPane.showMessageDialog(null,"Filas:"+total);

}

}
    
answered by 17.12.2018 в 15:03