如何在jsp页面中编辑我的表而不打开要编辑的新页面,它应该反映在数据库中?

时间:2017-06-19 05:42:13

标签: html mysql jsp

当我编辑表并点击更新时,如果我按下查看按钮,表的内容设置为空..请帮我解决这个问题..

这是我用于从数据库中检索数据的index.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="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("id");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "test";
String userid = "root";
String password = "root";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<html>
<body>
<!-- <h1>Retrieve data from database in jsp</h1> -->
<table border="1">
<tr>
<td>id</td>
<td>first name</td>
<td>last name</td>
<td>City name</td>
<td>Email</td>
<td>update</td>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select * from users";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td  contenteditable><%=resultSet.getString("id") %></td>
<td  contenteditable><%=resultSet.getString("first_name") %></td>
<td  contenteditable><%=resultSet.getString("last_name") %></td>
<td  contenteditable><%=resultSet.getString("city_name") %></td>
<td  contenteditable><%=resultSet.getString("email") %></td>
<td><a href="update-process.jsp?id=<%=resultSet.getString("id")%>">update</a></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>

这是我的update-process.jsp,用于将数据存储回数据库。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%! String driverName = "com.mysql.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/test";%>
<%!String user = "root";%>
<%!String psw = "root";%>
<%
String id = request.getParameter("id");
String first_name=request.getParameter("first_name");
String last_name=request.getParameter("last_name");
String city_name=request.getParameter("city_name");
String email=request.getParameter("email");
if(id != null)
{
Connection con = null;
PreparedStatement ps = null;
int personID = Integer.parseInt(id);
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,user,psw);
String sql="Update users set id=?,first_name=?,last_name=?,city_name=?,email=? where id="+id;
ps = con.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2, first_name);
ps.setString(3, last_name);
ps.setString(4, city_name);
ps.setString(5, email);
int i = ps.executeUpdate();
if(i > 0)
{
out.print("Updated");
}
else
{
out.print("There is a problem in updating Record.");
} 
}
catch(SQLException sql)
{
request.setAttribute("error", sql);
out.println(sql);
}
}
%>

<html>
<body>
<form action="index.jsp">
<input type="submit" value="view">
</form>
</body></html>

1 个答案:

答案 0 :(得分:0)

首先,我建议你不要在jsp中放入任何java代码。您的代码看起来非常混乱,很难找到bug并进一步维护。我不确定,但按照下面的代码行:

Box hBox= Box.createHorizontalBox();
hBox.add(TextArea1);
hBox.add(TextArea2);
hBox.add(TextArea3);

你将永远得到null,因为你没有这些输入字段。输入字段会生成请求参数,或者您需要将这些requestParams附加到您的访问URL中,就像您为id所做的那样。

编辑:在index.jsp中更改:

String first_name=request.getParameter("first_name");
String last_name=request.getParameter("last_name");
String city_name=request.getParameter("city_name");
String email=request.getParameter("email");

<td><a href="update-process.jsp?id=<%=resultSet.getString("id")%>">update</a></td>

但这是一个非常糟糕的做法。 最好你编写一个servlet,在index.jsp中有一个包含所有输入字段的html表单。