-2

I am developing a web application in which I have a jsp page with table. The table is populated by values of database except for one column which is editable and empty for user to enter the data. My problem is I want to update the database table once the user enters the data into editable cells and press submit button. Can anyone help me on this. Below is my 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 * FROM employmanage WHERE    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 * FROM leavedetails WHERE 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>
Arun
  • 140
  • 3
  • 15
  • can anyone tell me how to read the values from table.so that I can update it to database. – Arun May 09 '16 at 11:17
  • any reasons for negative markings? – Arun May 09 '16 at 11:25
  • mi downvote is because your code is harming my eyes... please read [this](http://www.coderanch.com/t/659757/Wiki/Scriptlets) to know why and [this](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) to learn how to do it right. After this, rewrite your code and kindly ask a new question.... – Jordi Castilla May 09 '16 at 11:28
  • there is nothing wrong in following what i have done. different people prefer different ways of coding. – Arun May 09 '16 at 11:36
  • 1
    sorry but **yes** there is plenty of stuff wrong in your code. Have you readed the articles?? **It's not just my opinion, there are facts**. So if you want to learn, please, do it in the right way and we will be glad to help. – Jordi Castilla May 09 '16 at 11:43
  • Opening database connections inside JSPs... **twitch** – chrylis -cautiouslyoptimistic- May 09 '16 at 12:33
  • Why there are too much try catch blocks in your code. You are calling two different queries in same page!why? And how user can press submit button because there is none. Make it clear then I can help you. – Gaurav Mahindra May 09 '16 at 13:22
  • you are right there is no submit button as of now but soon i will implement that. The main thing what i want is how to read the cell data of table. user will write text in editable cell of table and press submit which will update the database table. – Arun May 09 '16 at 13:27
  • if i am able to read what the user has written in table cell i can easily update the database table with single update statement. – Arun May 09 '16 at 13:28
  • I can offer you cleaner solution. Just tell me from which table you want to fetch data in this page. – Gaurav Mahindra May 09 '16 at 14:37
  • I have a database table with one column empty i read from that table and display it on my html table. in my html table i have an empty column as well which is editable. if user writes into this and click submit i want it to update in empty column of my database. – Arun May 10 '16 at 05:23

1 Answers1

1

Ideally you should create a file to handle Database operations, So remove all the database related code from JSP page.

As soon as user hits enter make an ajax call and post edited value to the servlet or controller whichever you are using.

Then Create a Service and DAO file

you will receive changed value in your controller, pass that value to service file and create required object in that file and pass that object to DAO layer, in that file write code to handle database operation.

The way you have written the code is not the ideal way and should not be followed.

gshri1986
  • 68
  • 4