gpt4 book ai didi

mysql - 使用JSP更新MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 17:50:20 25 4
gpt4 key购买 nike

嗯,大家好。我正在尝试创建更新 MySQL 数据库的 JSP 文件,它“没有错误”地工作,但只显示白屏并且不更新数据库。以下是我使用的 JSP:

profiletest.jsp

<!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 = "sais2";
String userid = "root";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<html>
<head>
<title>Profile</title>
</head>
<jsp:include page="head.jsp"></jsp:include>
<body onload="startTime()" style="background-image:url(assets/img/bg.jpg);
background-attachment:fixed;background-size:cover">
<jsp:include page="header1.jsp"></jsp:include>
<jsp:include page="settings-nav.jsp"></jsp:include>
<table border="1" width="75%" align="center">
<tr>
<td align="center">ID</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">E-Mail</td>
<td align="center">Update</td>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl + database, userid, password);
statement=connection.createStatement();
String sql ="SELECT * FROM login";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td align="center"><%=resultSet.getString("id") %></td>
<td align="center"><%=resultSet.getString("fname") %></td>
<td align="center"><%=resultSet.getString("lname") %></td>
<td align="center"><%=resultSet.getString("email") %></td>
<td align="center"><a href="updateprofile.jsp?id=<%=resultSet.getString("id")%>">UPDATE</a></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>

更新配置文件.jsp

<%@page import="java.sql.*"%>
<%
String id = request.getParameter("id");
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/sais2";
String userid = "root";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<%
try{
connection = DriverManager.getConnection(url, userid, password);
statement = connection.createStatement();
String sql ="SELECT * FROM login WHERE id ="+id;
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<html>
<head>
<title>Profile</title>
</head>
<jsp:include page="head.jsp"></jsp:include>
<body onload="startTime()" style="background-image:url(assets/img/bg.jpg);
background-attachment:fixed;background-size:cover">
<jsp:include page="header1.jsp"></jsp:include>
<jsp:include page="settings-nav.jsp"></jsp:include>
<form method="post" action="update-process.jsp">
<div class="form-group">
<label class="control-label col-sm-2" >First Name</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="fname" name="fname" value="<%=resultSet.getString("fname")%>" required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" >Last Name</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="lname" name="lname" value="<%=resultSet.getString("lname")%>" required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" >E-Mail</label>
<div class="col-sm-3">
<input type="email" class="form-control" id="email" name="email" value="<%=resultSet.getString("email")%>" required/>
</div>
</div>
<br>
<input class="loginb" type="submit" value="Update"/>
</form>
<%
}
connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>

和updateprocess.jsp

<%@page import="java.sql.*"%>
<%!String driverName = "com.mysql.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/sais2";%>
<%!String userid = "root";%>
<%!String password = "";%>
<%
String id = request.getParameter("id");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
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, userid, password);
String sql = "UPDATE login SET lname = ?, fname = ?, email = ? WHERE id = " + id;
ps = con.prepareStatement(sql);
ps.setString(1, lname);
ps.setString(2, fname);
ps.setString(3, email);
int i = ps.executeUpdate();
if(i != 0){
out.print("Record Updated Successfully");
String redirectURL = "http://localhost:8080/SalesandInventory/System/profiletest.jsp";
response.sendRedirect(redirectURL);
}
else{
out.print("There is a problem in updating Record.");
}
}
catch(SQLException sql){
request.setAttribute("error", sql);
out.println(sql);
}
}
%>

你能帮我一下吗?我真的找不到错误在哪里,而且我不太了解 JSP,因为我们的教授只是在几乎没有知识的情况下向我们扔了这个炸弹。

最佳答案

再包含一个 jsp 页面,并通过将第一页中的所有值转发到第二页来在第二个 jsp 页面中执行数据库操作。或者使用 servlet 执行数据库更新。

在同一时代,您无法更新 MySQL 数据库,因为 java 代码不会传输到浏览器,而是编译web-container 在服务器上运行,然后转换 为html代码,然后传输到浏览器。 p>

其他资源:JSP Architecture并转到 JSP 处理

关于mysql - 使用JSP更新MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49420186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com