<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生管理系统</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <jsp:useBean id="link" class="jdbcHomework.LinkMysql"></jsp:useBean> <% request.setCharacterEncoding("utf-8"); String uid = request.getParameter("sid"); String uname = request.getParameter("sname"); String sex = request.getParameter("ssex"); String banji = request.getParameter("banji"); String yuanxi = request.getParameter("yuanxi"); System.out.println(uid+uname+sex+banji+yuanxi); Connection con=null; PreparedStatement ps=null; con=link.getConnection(); String sql="update student set Sname=?,Ssex=?,Sclass=?,Syuanxi=? where Sid=?"; ps=con.prepareStatement(sql); ps.setString(1,uname); ps.setString(2, sex); ps.setString(3,banji); ps.setString(4,yuanxi); ps.setString(5,uid); int i=ps.executeUpdate(); if(i>0){ System.out.println("修改成功!"); response.sendRedirect("linkMysql-2.jsp"); //out.print("<script type='javascript'> alert('学生信息修改成功!');window.location='linkMysql.jsp';</script>"); } else{ System.out.println("修改失败!"); response.sendRedirect("linkMysql-2.jsp"); //out.print("<script type='javascript'> alert('学生信息修改失败!');window.location='login.jsp';</script>"); } ps.close(); con.close(); %> </body> </html>
时间: 2024-04-28 08:21:55 浏览: 9
这是一个 JSP 页面,用于修改学生信息。它包含了一个 JavaBean 类 LinkMysql,用于连接数据库。在页面中,它通过 request.getParameter() 方法获取表单提交的学生信息,然后执行 SQL 语句,将修改后的信息更新到数据库中。最后,根据执行结果,将页面重定向到 linkMysql-2.jsp 页面。其中,注释部分的代码是在修改成功或失败后弹出提示框。
相关问题
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="java.sql.*" %> <%@page import="java.sql.Statement" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆跳转servlet</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%-- 获取前端数据 --%> <% String usernum = request.getParameter("usernum"); String userpwd = request.getParameter("userpwd"); %> <%-- 定义SQLConn类和INSERT_SQL常量 --%> <% SQLConn sqlConn = new SQLConn(); //final String INSERT_SQL = "INSERT INTO User_ZC(UserID, Name, Code, Power) VALUES (?, ?, ?, ?)"; sqlConn.openDB();//打开数据库 String sql = "select UserId,Code from User_ZC"; ResultSet rs = sqlConn.execQuery(sql); boolean v = false; while(rs.next()){ if(rs.getString("usernum").equals(usernum)&&rs.getString("userpwd").compareTo(userpwd)==0){ v = true; } } rs.close(); sqlConn.closeDB(); if(v){ request.getRequestDispatcher("ZCS.jsp").forward(request, response); }else{ request.getRequestDispatcher("ZCB.jsp").forward(request, response); } %> </body> </html> 找bug
在代码中,有两处问题:
1. 在获取前端数据时,需要使用参数名 `usernum` 和 `userpwd` 而不是 `UserID` 和 `Code`。
```
String usernum = request.getParameter("usernum");
String userpwd = request.getParameter("userpwd");
```
2. 在比较密码时,应该使用 `equals()` 方法而不是 `compareTo()` 方法。
```
if(rs.getString("usernum").equals(usernum) && rs.getString("userpwd").equals(userpwd)) {
v = true;
}
```
修改后的代码如下所示:
```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.util.SQLConn"%>
<%@page import="java.sql.*" %>
<%@page import="java.sql.Statement" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登陆跳转servlet</title>
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body>
<%-- 获取前端数据 --%>
<%
String usernum = request.getParameter("usernum");
String userpwd = request.getParameter("userpwd");
%>
<%-- 定义SQLConn类和INSERT_SQL常量 --%>
<%
SQLConn sqlConn = new SQLConn();
sqlConn.openDB(); //打开数据库
String sql = "select UserId,Code from User_ZC";
ResultSet rs = sqlConn.execQuery(sql);
boolean v = false;
while(rs.next()){
if(rs.getString("UserId").equals(usernum) && rs.getString("Code").equals(userpwd)) {
v = true;
}
}
rs.close();
sqlConn.closeDB();
if(v){
request.getRequestDispatcher("ZCS.jsp").forward(request, response);
} else {
request.getRequestDispatcher("ZCB.jsp").forward(request, response);
}
%>
</body>
</html>
```
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="org.apache.commons.lang3.*" %> <%@page import="com.util.LOGS" %> <html> <body> <%-- 获取前端数据 --%> <% String username = request.getParameter("username"); String usernum = request.getParameter("usernum"); String name=request.getParameter("ytime"); String mon=request.getParameter("mtime"); String num=request.getParameter("rtime"); String people = request.getParameter("people"); %> <%-- 定义SQLConn类和INSERT_SQL常量 --%> <% final String INSERT_SQL = "INSERT INTO YuYue(UserID, Time, People) VALUES (?, ?, ?)"; %> <%-- 执行数据库操作 --%> <% int count = 0; try {Connection conn = SQLConn.openDB(); PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL); { pstmt.setString(1, usernum); pstmt.setString(2, name+mon+num); pstmt.setString(3, people); count = pstmt.executeUpdate(); }} catch (SQLException e) { e.printStackTrace(); } %> <%-- 根据执行结果进行相应的操作 --%> <c:choose> <c:when test="${count > 0}"> <%-- 成功跳转到ZCS.jsp --%> <c:redirect url="ZCS.jsp"/> </c:when> <c:otherwise> <%-- 失败跳转到ZCB.jsp --%> <c:redirect url="../ZCB.jsp"/> </c:otherwise> </c:choose> </body> </html>优化代码
以下是对代码进行优化的建议:
1. 在第二行中,导入语句中的星号通配符应该避免使用,应该明确导入需要使用的类或接口。
2. 在第十行中,可以将 INSERT_SQL 常量定义在 SQLConn 类中并进行调用,以提高代码的可维护性和复用性。
3. 在第十二行中,PreparedStatement 对象应该使用 try-with-resources 语句,以自动释放资源并避免资源泄露问题。
4. 在第十六至二十二行中,可以使用 EL 表达式代替 JSTL 标签,以简化代码并提高可读性。
基于上述建议,优化后的代码如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.sql.*"%>
<%@ page import="com.util.SQLConn"%>
<%@ page import="com.util.LOGS"%>
<%@ page import="org.apache.commons.lang3.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Data</title>
</head>
<body>
<%-- 获取前端数据 --%>
<% String username = request.getParameter("username");
String usernum = request.getParameter("usernum");
String name = request.getParameter("ytime");
String mon = request.getParameter("mtime");
String num = request.getParameter("rtime");
String people = request.getParameter("people"); %>
<%-- 执行数据库操作 --%>
<% int count = 0;
try (Connection conn = SQLConn.openDB();
PreparedStatement pstmt = conn.prepareStatement(SQLConn.INSERT_SQL)) {
pstmt.setString(1, usernum);
pstmt.setString(2, name + mon + num);
pstmt.setString(3, people);
count = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} %>
<%-- 根据执行结果进行相应的操作 --%>
<c:if test="${count > 0}">
<%-- 成功跳转到 ZCS.jsp --%>
<jsp:forward page="ZCS.jsp"></jsp:forward>
</c:if>
<c:if test="${count <= 0}">
<%-- 失败跳转到 ZCB.jsp --%>
<jsp:forward page="../ZCB.jsp"></jsp:forward>
</c:if>
</body>
</html>
```
优化后的代码更加简洁、易读、易维护,并且避免了可能存在的一些问题。