java - insert data into database through MVC model using setter getter class -
i want insert data database in organised way, have jsp
page takes input id
name
salary
user , send database table employee. table has 3 columns eid
, ename
,esalary
.
in coding part have 3 packages: com.mvc.javaclassbeans
,com.mvc.javaclasses
,com.mvc.servlets
.
myservlet
class in com.mvc.servlets
package takes parameter (eid,ename,esalary)from jsp page , process set parameters creating object of employee
class , insert eid
ename
esalary
values using new insertclass().insertfunction(obj);
my code not showing error not inserting values. not getting problem have parted codes different packages make work mvc.
my com.mvc.javaclasses pakage consists of
- dbconnector
- insertclass
com.mvc.javaclassbeans consists of
- employeeclass
com.mvc.servlets consists of
- myservlet class
below code:
insert.jsp
<%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <form action="myservlet" method="post"> id:<input type="text" name="eid"> name:<input type="text" name="ename"> salary:<input type="text" name="esalary"> <button type="submit" name="submit" value="submit" >sign in</button> </form> </body> </html>
mysevlet
package com.mvc.servlets; import com.mvc.javaclassbeans.employee; import com.mvc.javaclasses.dbconnector; import com.mvc.javaclasses.insertclass; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet(name = "myservlet", urlpatterns = {"/myservlet"}) public class myservlet extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { employee obj = new employee(); obj.setid(request.getparameter("eid")); obj.setname(request.getparameter("ename")); obj.setsalary(request.getparameter("esalary")); try { new insertclass().insertfunction(obj); } catch (classnotfoundexception ex) { logger.getlogger(myservlet.class.getname()).log(level.severe, null, ex); } catch (sqlexception ex) { logger.getlogger(myservlet.class.getname()).log(level.severe, null, ex); } out.print("sucess"); } catch (exception ex) { logger.getlogger(myservlet.class.getname()).log(level.severe, null, ex); } } }
please me find problem missing.
this code worked me. sake of testing configuration is:
main code (just put code in test
class):
package sarah; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.io.printwriter; import java.sql.*; import java.util.logging.level; import java.util.logging.logger; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; class dbconnector { //string user = "system"; //string pass = "system"; //string driver = "oracle.jdbc.oracledriver"; //string dburl = "jdbc:oracle:thin:@localhost:1521:xe"; resultset rs; connection connection = null; statement stmt = null; public connection open() { system.out.println("-------- mysql jdbc connection testing ------------"); try { class.forname("org.postgresql.driver"); connection = drivermanager.getconnection("jdbc:postgresql://localhost:5432/test", "postgres", "root"); system.out.println("succeed!"); // return connection; } catch (exception e) { system.out.println("you have stucked error!!!!!!"); } system.out.println("mysql jdbc driver registered!"); if (connection != null) { system.out.println("you made it, take control database now!"); } else { system.out.println("failed make connection!"); } return connection; } public void db_close(connection con) throws sqlexception { con.close(); return; } } class employee { string eid; string ename; string esalary; // public employee(string eid, string ename, string esalary) { // this.eid = eid; // this.ename = ename; // this.esalary = esalary; //} public string getid() { return eid; } public void setid(string eid) { this.eid = eid; } public string getname() { return ename; } public void setname(string ename) { this.ename = ename; } public string getsalary() { return esalary; } public void setsalary(string esalary) { this.esalary = esalary; } } class insertclass { // connection connection = null; preparedstatement ps; public string query; public boolean insertfunction(employee ob) throws classnotfoundexception, sqlexception { try { dbconnector dbc = new dbconnector(); connection connection = dbc.open(); query = "insert employee (eid,ename,esalary) values(?,?,?)"; java.sql.preparedstatement ps = connection.preparestatement(query); ps.setstring(1, ob.getid()); ps.setstring(2, ob.getname()); ps.setstring(3, ob.getsalary()); ps.executeupdate(); return true; } catch (exception ex) { ex.printstacktrace(); return false; } } } @webservlet(name = "test", urlpatterns = {"/test"}) public class test extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { employee obj = new employee(); obj.setid(request.getparameter("eid")); obj.setname(request.getparameter("ename")); obj.setsalary(request.getparameter("esalary")); try { new insertclass().insertfunction(obj); } catch (classnotfoundexception ex) { logger.getlogger(test.class.getname()).log(level.severe, null, ex); } catch (sqlexception ex) { logger.getlogger(test.class.getname()).log(level.severe, null, ex); } out.print("sucess"); } catch (exception ex) { logger.getlogger(test.class.getname()).log(level.severe, null, ex); } } }
table definition:
create table employee ( eid text, ename text, esalary text ) ( oids=false ); alter table employee owner postgres;
and test.jsp
file:
<%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <form action="test" method="post"> id:<input type="text" name="eid"> name:<input type="text" name="ename"> salary:<input type="text" name="esalary"> <button type="submit" name="submit" value="submit" >sign in</button> </form> </body> </html>
Comments
Post a Comment