0

This is my database

CREATE TABLE `Animal` ( `name` varchar(128) NOT NULL, `breed` varchar(128) NOT NULL, `age` varchar(128) NOT NULL )    

register.html to fill in the data

<html>
<body>
<form action="servlet/Register" method="post">

name <input type="text" name="name"/><br/><br/>
breed <input type="password" name="breed"/><br/><br/>
age <input type="password" name="age"/><br/><br/>
<br/><br/>
<input type="submit" value="register"/>
</form>
</body>
</html>    

My servlet

package animals;


import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Register extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String name = request.getParameter("name");
    String breed = request.getParameter("breed");
    String age = request.getParameter("age");


    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
        PreparedStatement ps = con.prepareStatement(
                "INSERT INTO Animal (name,breed,age) VALUES(?,?,?)");

        ps.setString(1, name);
        ps.setString(2, breed);
        ps.setString(3, age);


        int i = ps.executeUpdate();
        if (i > 0) {
            out.print("Data successfully registered...");
        }

    } catch (Exception e2) {
        System.out.println(e2);
    }

    out.close();
}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
    <servlet-name>Register</servlet-name>
    <servlet-class>animals.Register</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/servlet/Register</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>register.html</welcome-file>
</welcome-file-list>
</web-app>

When I fill in the form and submit, it send me to /register but it's blank and it doesn't add data in my database.

I'm 100% sure that my connection to the java database is working, because I have another project with a login with the same connection to the database and that one is working.

Any tips / comments are welcome

8
  • 2
    So what diagnostics have you performed? Are you sure the request is getting through to your servlet? Have you logged anything? Is your driver set to auto-commit? (If not, maybe that's the problem.) Commented Apr 25, 2015 at 17:49
  • Are you getting any error in Console? Commented Apr 25, 2015 at 17:50
  • I'm guessing it's @JonSkeet 's suggestion for the auto-commit. Commented Apr 25, 2015 at 17:51
  • @Dilip No errors in the console Commented Apr 25, 2015 at 17:51
  • What do you mean with auto-commit? @JonSkeet Commented Apr 25, 2015 at 17:51

1 Answer 1

1

Few things you can check :
1. Debug the code & check if values are captured by the servlet.
2. Use commit() after the executeUpdate(). Maybe your config is set to auto-commit off due to some reasons.
3. Is this string printed? "Data successfully registered..."
4. Lastly, Any exceptions?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.