简体   繁体   English

如何将jsp页面表中的值插入到数据库中

[英]How to insert value from table of jsp page into database

I am developing a web application in which I have a jsp page with table.我正在开发一个 Web 应用程序,其中有一个带表格的 jsp 页面。 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.下面是我的jsp页面代码。

<%@ 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>

Ideally you should create a file to handle Database operations, So remove all the database related code from JSP page.理想情况下,您应该创建一个文件来处理数据库操作,因此从 JSP 页面中删除所有与数据库相关的代码。

As soon as user hits enter make an ajax call and post edited value to the servlet or controller whichever you are using.只要用户点击进入,就进行 ajax 调用并将编辑后的值发布到 servlet 或控制器,无论您使用的是哪个。

Then Create a Service and DAO file然后创建一个Service和DAO文件

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.您将在控制器中收到更改的值,将该值传递给服务文件并在该文件中创建所需的对象并将该对象传递给 DAO 层,在该文件中编写代码来处理数据库操作。

The way you have written the code is not the ideal way and should not be followed.您编写代码的方式不是理想的方式,不应遵循。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM