java - How to insert value from table of jsp page into database -
i developing web application in have jsp page table. table populated values of database except 1 column editable , empty user enter data. problem want update database table once user enters data editable cells , press submit button. can me on this. below code of jsp page.
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <%@ page import="com.employee.com.logindetails" %> <%@ page import="java.sql.*" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> <link rel="stylesheet" href="css/newfile2.css"> </head> <body> <img src="images/crevavi_plain.jpg" background-color="white" width="300" height="70" style="float:right; margin-right:200px;"/></br> <h1 style="margin-left:20px; margin-top:10px;"><font size="8" color="black"> notification </font></h1> <fieldset style="float: center; width:1680px; height:900px; background-color:white; border-color:black; margin-top:10px;"></br></br> <link rel="stylesheet" href="css/table.css"> <fieldset style="float: center; width:250px; height: 450px; background-color:white; border-color:black; margin-top:10px;margin-left:10px;"> <fieldset style="float: center; width:900px; height:600px; background-color:; border-color:grey; margin-left:275px; margin-top:-450px;"></br> <link rel="stylesheet" href="css/table.css"> <% final string username,password; string name="",employeeid="",projectmanager=""; username = logindetails.username; password = logindetails.password; string employname="",employid="",fromdate="",todate="",reason=""; resultset resultset = null; try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } connection connection = null; try { connection = drivermanager.getconnection("jdbc:mysql://localhost:3306/students","root","root"); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } statement statement = null; try { statement = connection.createstatement(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } try { resultset = statement.executequery("select * employmanage username = '"+username+"'"); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } try { while(resultset.next()){ projectmanager = resultset.getstring(1); } }catch(exception e){ e.printstacktrace(); } try { resultset = statement.executequery("select * leavedetails projectmanager = '"+projectmanager+"'"); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } try {%> <table> <tr> <th>employee name</th> <th>employee id</th> <th>from date</th> <th>to date</th> <th>reason</th> <th>status</th> </tr> <% while(resultset.next()){ %> <tr> <td> <%= resultset.getstring(1) %></td> <td> <%= resultset.getstring(2) %></td> <td> <%= resultset.getstring(4) %></td> <td> <%= resultset.getstring(5) %></td> <td> <%= resultset.getstring(6) %></td> <td contenteditable='true' %></td> <% } }catch(exception e){ e.printstacktrace(); } %> </fieldset> </body></html>
ideally should create file handle database operations, remove database related code jsp page.
as user hits enter make ajax call , post edited value servlet or controller whichever using.
then create service , dao file
you receive changed value in controller, pass value service file , create required object in file , pass object dao layer, in file write code handle database operation.
the way have written code not ideal way , should not followed.
Comments
Post a Comment