使用JSP更新MySQL数据库

时间:2018-03-22 03:47:20

标签: mysql jsp

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

profiletest.jsp



componentWillUnmount() {
  clearInterval(this.apiCall);
}




updateprofile.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> 
&#13;
&#13;
&#13;

和updateprocess.jsp

&#13;
&#13;
<%@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> 
&#13;
&#13;
&#13;

你能帮帮我吗?我真的无法找到错误的位置而且我不太了解JSP那么多,因为我们的教授只是把这个炸弹放在我们身上而几乎没有知识。

1 个答案:

答案 0 :(得分:0)

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

在同一年龄段,您无法更新MySQL数据库,因为java代码未传输到浏览器,而是在网络容器中已编译 在服务器上运行,然后转换 转换为HTML代码,然后转移到浏览器。

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