How can I insert and delete value in a database in derby in JSP?

前端 未结 2 1094
鱼传尺愫
鱼传尺愫 2021-01-26 17:50

The history: history

The problem is I cannot delete customer from the database. I can create a new customer, but if I leave blank the fields it create an empty record in

相关标签:
2条回答
  • 2021-01-26 18:25

    I understand from the previous question where you defined ID as integer, so you should use setInt instead of setString, try as

    public void deleteClient(Integer id){
    
        try {
    
            createConnection();
            String delete="DELETE FROM CUSTOMER WHERE ID=?";
            PreparedStatement ps=conn.prepareStatement(delete);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ps.close();
            closeConnection();
        }
    }
    

    Id is an integer in the database field where you defined it has a primary key.

    Update 1

    Use the following in your index.jsp to convert String to int, pass idNum to your method.

     Integer idNum = null;
     String id = request.getParameter("ID");
        if(id != null) {
            try {
                System.out.println("String Id value --> "+id.trim());
                idNum = Integer.parseInt(id.trim());
            }
            catch(NumberFormatException ex) {
                ex.printStackTrace();
            }
        }
    
    0 讨论(0)
  • 2021-01-26 18:45

    Looks like you are not setting the ID in the list

    while(rs.next()){
            list.add(rs.getString("ID")); // Add this
            list.add(rs.getString("CNAME"));
            list.add(rs.getString("ADDRESS"));
            list.add(rs.getString("PHONENUMBER"));
        }
    

    Without ID in the list , you wont be able to access the same from the jsp and the same wont be available when you call the delete method.

    Another thing is make sure that the ID is string and not int . Usually we keep the key (i.e ID here ) as integer.

    0 讨论(0)
提交回复
热议问题