java - Can't insert item in database using servlet -
i trying insert values table of database using servlet , jsp , mysql workbench database. when fill in form , submit, shows blank screen, no output displayed , in database values not updated.these following details:
1) controller.java
public class controller extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception, sqlexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { string dburl = getservletcontext().getinitparameter("dburl"); string dbuser = getservletcontext().getinitparameter("dbuser"); string dbpassword = getservletcontext().getinitparameter("dbpassword"); string dbdriver = getservletcontext().getinitparameter("dbdriver"); connection conn = dao.getconnectionjdbc(dburl, dbuser, dbpassword, dbdriver); string title = request.getparameter("title"); string actor = request.getparameter("actor"); string actress = request.getparameter("actress"); string genre = request.getparameter("genre"); string year = string.valueof("year"); string querymessage = "insert movies (title, actor, actress,genre,year)" + "values (" + title + ",'" + actor + "','" + actress + "','" + genre + "','" + year + "')"; statement statement = conn.createstatement(); int result = statement.executeupdate(querymessage); conn.close(); if (result > 0) { requestdispatcher rd = request.getrequestdispatcher("/added.jsp"); rd.forward(request, response); } } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { processrequest(request, response); } catch (sqlexception ex) { logger.getlogger(controller.class.getname()).log(level.severe, null, ex); } }
2) dao.java
public class dao { public static connection getconnectionjdbc(string dburl, string dbuser,string dbpassword, string dbdriver) throws ioexception { connection connection = null; try { class.forname(dbdriver); } catch (classnotfoundexception e) { system.out.println("where mysql jdbc driver?"); } try { connection = drivermanager.getconnection(dburl,dbuser,dbpassword); } catch (sqlexception e) { system.out.println("connection failed! check output console"); } if (connection != null) { system.out.println("connection established"); } return connection; }
}
3) addmovie.jsp
<form action="controller" method="post"> code insert in text box </form>
4) web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>controller</servlet-name> <servlet-class>com.myapp.controller.controller</servlet-class> </servlet> <servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>/controller</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <context-param> <param-name>dbuser</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>dbpassword</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>dbdriver</param-name> <param-value>com.mysql.jdbc.driver</param-value> </context-param> <context-param> <param-name>dburl</param-name> <param-value>jdbc:mysql://localhost:3306/moviedb</param-value> </context-param>
Comments
Post a Comment