Return a value from a MYSQL/JAVA column

Asked

Viewed 80 times

0

I have the following code to store the maximum value of a column in a variable integer codemax:

cf = new ConnectionFactory().getConexao();
int codigomax = 0;
try {
    stmt1 = cf.prepareStatement("SELECT max(id_endereço) as max_id_endereço from endereço");
    rs1 = stmt1.executeQuery("SELECT max(id_endereço) as max_id_endereço from endereço");
    if (rs1.next()) {
        codigomax = rs1.getInt("max_id_endereço");
    }
}
catch (SQLException ex){
}

But when I check the variable, it always has the value that was initialized PS: The column in question has data!

1 answer

1

Oops, I think there’s something strange in your code.

stmt1 = cf.prepareStatement("SELECT max(id_endereço) as max_id_endereço from endereço");
rs1 = stmt1.executeQuery();

Another thing that can give errors are the non-ascii characters. I know most things nowadays work with them, but it’s worth trying without them.

then stay like this:

cf = new ConnectionFactory().getConexao();
int codigomax = 0;
try {
    stmt1 = cf.prepareStatement("SELECT max(id_endereço) as max_id_endereco from endereço"); //sem ç
    rs1 = stmt1.executeQuery();
    if (rs1.next()) {
        codigomax = rs1.getInt("max_id_endereco"); //sem ç
    }            
} catch (SQLException ex) {}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.