column not found

1

I have a problem - I get an error message:

  

"java.sql.SQLException: Column 'cod_pro'nod found."

Why does this "column not found" error come out?

This is my code:

void generarNumeroFactura(){
String sql ="SELECT LAST_INSERT_ID(cod_pro)+1 FROM tickets";
Conectar cc= new Conectar();
Connection cn = 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
        txtcod1.setText(rs.getString("cod_pro"));

    }

    } catch (Exception e) {
    // NOTA: So hubo error muestra el error
    JOptionPane.showMessageDialog(null, e);
}
    
asked by user83962 03.01.2019 в 19:08
source

1 answer

1

The error can be given in two parts of your code. As a good decision, the problem is in your query. However, because your try is structured, it is difficult to say in which part.

In the comments, you specified that the query works well. Therefore, the problem is not the query itself, but the result.

By doing this:

SELECT LAST_INSERT_ID(cod_pro)+1 FROM tickets

The column resulting from that query is not called cod_pro. The name of that column, can be whatever the database puts you. That's because you're using a function to calculate that column. The same would happen, if you added two columns or did anything else with your columns.

To solve this, you need to name your column using the AS.

SELECT LAST_INSERT_ID(cod_pro)+1 AS cod_pro FROM tickets
    
answered by 03.01.2019 / 22:38
source