Load JLabel from MySQL

0

I am working in Java and I need to place a jLabel that obtains information from the Database, in this case it is a "Registration Number" every time that the Form will be done, you must bring the name and upload it in said Jlabel. I have tried several ways and I still can not do it.

private void numeros() throws SQLException{
    String SQL= ("select max(nregistro) from muestras");
    Statement pst = cn.prepareStatement(SQL);
    ResultSet ResultSet =pst.executeQuery();
    try {   
        while(ResultSet.next()){
            String c =ResultSet.getString(1);
            System.out.println(c);
            this.txt_registro.setText(String.valueOf(c));
        } 
    } catch (SQLException ex) {
        Logger.getLogger(LoqNuevo.class.getName()).log(Level.SEVERE, null, ex);
    }
}
    
asked by Juan Pablo 10.08.2018 в 19:30
source

2 answers

0

I found the answer: It was a more limited answer and without so many laps

void numeros(){
    String sql ="SELECT LAST_INSERT_ID(nregistro)+1 as nregistro FROM muestras ORDER BY nregistro DESC LIMIT 1";
    conectar cc = new conectar();
//    Connection cn = (Connection) cc.conexion();
    try {
        java.sql.Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while(rs.next()){
            //Aca le digo que muestre el valor en un JtextFiel

            lb_registro.setText(rs.getString("nregistro"));

        }

        } catch (SQLException e) {
        // NOTA: So hubo error muestra el error
        JOptionPane.showMessageDialog(null, e);
    }
}
    
answered by 16.08.2018 в 16:43
-1

Assuming that the query works and that you are calling the numeros() method from the constructor (or a post-constructor), you should do a repaint() to see the changes on the screen.

It's all I can see without having the way you call the method and the error it shows you (if it shows you an error).

    
answered by 10.08.2018 в 19:41