<%@ 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 17:21:55 浏览: 147
这是一个 JSP 页面,用于修改学生信息。它包含了一个 JavaBean 类 LinkMysql,用于连接数据库。在页面中,它通过 request.getParameter() 方法获取表单提交的学生信息,然后执行 SQL 语句,将修改后的信息更新到数据库中。最后,根据执行结果,将页面重定向到 linkMysql-2.jsp 页面。其中,注释部分的代码是在修改成功或失败后弹出提示框。
相关问题
<%@ 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="java.net.HttpURLConnection"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!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 stime = request.getParameter("stime"); String people = request.getParameter("people"); %> <%-- 定义SQLConn类和INSERT_SQL常量 --%> <% Connection conn = SQLConn.openDB(); //创建Statement , 执行 SQL 查询 Statement sm=ct.createStatement(); //查询用户名登陆状态 String sql; sql ="select Login from User_ZC where UserId='"+usernum+"' and Login='"+1+"' "; // 执行查询 ResultSet rs = sm.executeQuery(sql); if(rs.next()) { //用户在登陆状态 int dbpass = rs.getInt(1); //System.out.println(dbpass); if(dbpass>0){ //用户合法,可以进行预约}第 31 行。错误信息是 "ct cannot be resolved"。
在代码中,你创建了一个名为 "conn" 的 Connection 对象,但在第 31 行中使用了一个名为 "ct" 的 Statement 对象,这导致了编译错误 "ct cannot be resolved"。你需要将第 31 行中的 "ct" 改为 "conn",或者在代码中声明并初始化一个名为 "ct" 的 Statement 对象。
<%@ 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>
```
阅读全文