0

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.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • This is a duplicate of the linked question https://stackoverflow.com/questions/9923623/how-to-return-int-value-from-select-query-in-function – LearningPhase May 13 '19 at 22:49
  • why you're not using-> if(rs.next()){int val = (rs.next()) ? rs.getInt("idvenda") : 0;} and you're getting only one values or multiple values for idvenda ? – Onkar Musale May 17 '19 at 07:22

0 Answers0