Im doing a project for my class in highschool, so i need to build a billing system but i found a problem, i need to get the ID of a bill but the method im trying to use isnt returning an integer and everytime a try to convert it says "unreported exception SQLException; must be caught or declared to be thrown" and when i try to return it as an integer already it says i didnt declare the variable(it must happen bc the variable is inside the try catch).
I tried to convert the result set to int inside the method and a i tried to use a public int to return the resultset as an integer already.
public int lastid(){
Connection con = ConnectionFactory.getConnection();
PreparedStatement st = null;
ResultSet rs = null;
List<VendaProduto> vd = new ArrayList<>();
try {
st = con.prepareStatement("SELECT idvenda FROM vendas ORDER BY idvenda DESC LIMIT 1;");
rs = st.executeQuery();
int val = ((Number) rs.getObject(1)).intValue();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Código da venda perdido.");
}
return val;
}
I need this function to return the resultset(Sell ID) as an integer so i can insert into the mysql table.