Skip to content
Advertisement

How to insert into table in java sql? and how to solve http 500 error [closed]

im trying to create a simple code where it get name from the user and insert it into the table , it first goes to a servlet to check if user left textbox empty or not then to goes to the jsp if correct and then i created a class named user to get input from user and insert it into the table my problem is its doesnt insert it into the table after thee run is finished

here is index.html:

<html>
    <head>
        <title>TODO supply a title</title>
        
    </head>
    <body>
        <form action="NewServlet" method="POST">
            Name:<input type="text" name="name">
            <input type="submit">
        </form>
    </body>
</html>

here is servlet:

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name=request.getParameter("name");
        if(name.equals(""))
        {
            PrintWriter pw=response.getWriter();
            pw.print("NO");
            
        }
        else
        {
            request.getRequestDispatcher("db.jsp").forward(request, response);
        }
    }

here is class named user:

public class user {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public int num(){
      int num=0;
      Connection con=null;
        try {
             Class.forName("org.apache.derby.jdbc.ClientDriver");
      con=DriverManager.getConnection("jdbc:derby://localhost:1527/m","m","m");
      PreparedStatement stmt=con.prepareStatement("INSERT INTO Tables "+"(NAME) values"+"(?)");
      stmt.setString(1, getName());
      num=stmt.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();;
        }
        finally{
           
          try {
              con.close();
          } catch (SQLException ex) {
              Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
          }
            
        }
     return num;
    }
}

here is jsp named dp:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        
    </head>
    <body>
        <jsp:useBean id="bean1" class="user.user">
            <jsp:setProperty property="name" name="bean1"/>
        </jsp:useBean>
        <%
            int a=bean1.num();
            if(a==1)
            {
                out.print("yes");
            }
            else
            {
                out.print("no");
            }
        %>
    </body>
</html>

the type of error it prints out:

enter image description here

Advertisement

Answer

It’s generally not a good practice to have database access code in your JSP layer (in your User class which your JSP is accessing with useBean). If your HTML form is posting to your Servlet, move the logic there instead.

The exception is telling you that conn is null when you called conn.close(). The only place where you have this call is in your finally block, so there was another exception that your catch didn’t catch, then the conn.close() call failed.

Change your catch to

catch (Exception e) 

so you can see what Exception is occurring. Note that you can’t call close on conn if the attempt to get the connection already failed, so guard against that by checking for null first:

try {
    if(conn != null){        
        con.close();
    }
    else{
        // conn was null, failed to get a connection!
    }

It might also help to check if you successfully get a connection in your main block rather than assuming you do.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement