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:

here is servlet:

here is class named user:

here is jsp named dp:

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

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:

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