java - Adding data to mysql with servlet not working -
this database
create table `animal` ( `name` varchar(128) not null, `breed` varchar(128) not null, `age` varchar(128) not null )
register.html fill in 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 animal (name,breed,age) values(?,?,?)"); ps.setstring(1, name); ps.setstring(2, breed); ps.setstring(3, age); int = ps.executeupdate(); if (i > 0) { out.print("data 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 fill in form , submit, send me /register it's blank , doesn't add data in database.
i'm 100% sure connection java database working, because have project login same connection database , 1 working.
any tips / comments welcome
few things can check :
1. debug code & check if values captured servlet.
2. use commit() after executeupdate(). maybe config set auto-commit off due reasons.
3. string printed? "data registered..."
4. lastly, exceptions?
Comments
Post a Comment