Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot insert data to MySQL with Servlet
    text
    copied!<p>I 'm working on a web applicaton with Netbeans JAVA (JSPs and Servlets) and MySQL and I am trying to make a "Sign up" feature.</p> <p>So, from the JSP I call the SignUp Servlet, which calls the PostUserData. The Userbean just keeps the data, so that I can show them again in the JSP easily.</p> <p>I get error "java.lang.NullPointerException - model.DataManager.PostUserData(DataManager.java:191)", which points where the resultsets close.</p> <pre><code>public UserBean PostUserData(String username, String password, String first_name, String last_name, String email) { UserBean userBean = new UserBean(); Connection conn = getConnection(); if (conn != null) { ResultSet rs1 = null; ResultSet rs2 = null; PreparedStatement pst1 = null; PreparedStatement pst2 = null; try { String strQuery1 = "select * from user where uid=?"; pst1 = conn.prepareStatement(strQuery1); pst1.setString(1, username); rs1 = pst1.executeQuery(); boolean usernameExists = rs1.next(); if (usernameExists == false) { String student = "student"; String strQuery2 = "INSERT INTO user VALUES (?, ?, ?, ?, ?, ?)"; pst2 = conn.prepareStatement(strQuery2); pst2.setString(1, username); pst2.setString(2, password); pst2.setString(3, first_name); pst2.setString(4, last_name); pst2.setString(5, email); pst2.setString(6, student); rs2 = pst2.executeQuery(); } else { userBean.setUidExists(true); } }//end of try catch(SQLException ex) { ex.printStackTrace(); } finally { try { rs1.close(); rs2.close(); pst1.close(); pst2.close(); } catch (SQLException e) {e.printStackTrace();} putConnection(conn); }//end of finally }//end of if return userBean; } </code></pre> <p>I already did a login feature in the same way and it works perfect with my database, so there is not a problem with MySQL. I also tested the Insert Script in MySQL workbench and it worked. </p> <p>Finally, when I remove the code </p> <pre><code>&gt; rs1.close(); &gt; rs2.close(); &gt; pst1.close(); &gt; pst2.close(); </code></pre> <p>outside the try, it checks if the username exists, so the problem is definitely in the INSERT script. Thanks for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload