I want to create a Registration Servlet allowing a user to type in their details and save it to a mySQL database. Below are all the classes i have used. the Registration Servelt is throwing an SQlException shown below.
} catch (SQLException se) {
out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
out.println("<p>SQL Error executing query='"+query+"'");
}
My question is how can I fix it to make my servlet work, ive tried everything i could think of.
/*
* RegistrationServlet.java
*
* Created on 2 September 2005, 20:04
*/
//Get parameters for new user record
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String username = request.getParameter("uname");
String password = request.getParameter("password");
String query = "INSERT INTO Users VALUES ('"+username+"','"+password+"','"+firstname+"','"+lastname+"','"+email+"')";
// write the user to the selected database
try {
Connection conn = DataManager.getInstance().getConnection();
Statement stmt = conn.createStatement();
stmt.execute(query);
User newUser = new User(username, firstname, lastname, email);
// add the user to the session and redirect to the menu
request.getSession().setAttribute("user", newUser);
// response.sendRedirect("tipping");
} catch (SQLException se) {
out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
out.println("<p>SQL Error executing query='"+query+"'");
}
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
/*
* User.java
*
* Created on 2 September 2005, 20:33
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
* @author Trixxma
*/
public class User {
private String userName, firstName, lastName, email;
/** Creates a new instance of User */
public User(String userName, String firstName, String lastName, String email) {
this.setFirstName(firstName);
this.setLastName(lastName);
this.setUserName(userName);
this.setEmail(email);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
}
/*
* DataManager.java
*
* Created on 28 August 2005, 18:30
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
try {
Connection conn = DataManager.getInstance().getConnection();
Statement stmt = conn.createStatement();
stmt.execute(query);
User newUser = new User(username, firstname, lastname, email);
// add the user to the session and redirect to the menu
request.getSession().setAttribute("user", newUser);
// response.sendRedirect("tipping");
} catch (SQLException se) {
out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
out.println("<p>SQL Error executing query='"+query+"'");
}
currently I am only getting what is printed in the catch block which is
There has been a problem creating your account. Please contact the site administrator via email this address
SQL Error executing query="trixma","123","Bob","brown","apple@gmail.com")'
So my query is throwing an SQLException and i want to stop this. The query is meant to store the results into mySQL and currently it is not but throwing an exception, I want help in fixing my code so it no longers throws the exception but stores the data into the database Users.
09-04-2005, 10:40 AM
Norm
It would help if you would show exactly what the SQLException was and what line it was on.
You can do that by adding a line of code within the catch block:
ex.printStackTrace();
Quote:
There has been a problem
doesn't help much.
Perhaps once you see what the error is you can correct it.
Also it would be better if you copy and paste the EXACT contents of the error message and not type it in manually.
09-04-2005, 11:10 PM
trixma
I did copy it, the message is exactly how i saw it in html view, i ran getMessage() and found that I needed to fix up some file permissions, and once i did this i fixed the problem. Thanks for your help Norm, its most appriciated