if (count > 0){ %> <a href="Admin.jsp">签到成功</a> <% } else { %> <a href="Admin.jsp">签到失败</a> <% }怎么预防rs指针为空,应该添加哪些代码
时间: 2023-12-15 19:05:31 浏览: 58
可以在执行查询语句之前,先判断记录集是否为空,可以使用rs.next()方法来判断是否有记录,如果有记录再执行相应的操作。可以使用以下代码进行修改:
```
if (rs.next()) {
if (count > 0) {
out.print("<a href='Admin.jsp'>签到成功</a>");
} else {
out.print("<a href='Admin.jsp'>签到失败</a>");
}
} else {
out.print("<a href='Admin.jsp'>查询结果为空</a>");
}
```
这样可以避免rs指针为空的情况发生。
相关问题
<%@ 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>
```
优化后的代码更加简洁、易读、易维护,并且避免了可能存在的一些问题。
<% String code = request.getParameter("YYM"); if (code == null || code.isEmpty()) { %> <a href="Admin.jsp">请输入预约码,点击返回签到</a> <% } else { Connection conn = SQLConn.openDB(); final String SELECT_SQL = "SELECT ACode FROM User_ZC JOIN YuYue ON User_ZC.UserId = YuYue.UserId WHERE Login = 1 AND State = 1 AND Arrive IS NULL"; final String UPDATE_SQL = "UPDATE YuYue SET Arrive =1 WHERE ACode = ?"; PreparedStatement pstmtSelect = conn.prepareStatement(SELECT_SQL); ResultSet rs = pstmtSelect.executeQuery(); int count = 0; while (rs.next()) { String Acode = rs.getString("ACode"); if (Acode.equals(code)) { try { PreparedStatement pstmtUpdate = conn.prepareStatement(UPDATE_SQL); pstmtUpdate.setString(1, code); count = pstmtUpdate.executeUpdate(); pstmtUpdate.close(); } catch (SQLException e) { e.printStackTrace(); } break; } } if (count > 0){ %> <a href="Admin.jsp">签到成功</a> <% } else { %> <a href="Admin.jsp">签到失败</a> <% } rs.close(); pstmtSelect.close(); conn.close(); } %>为什么总是跳转到“签到失败”页面,请找出bug
在代码中没有检查数据库连接是否成功打开。如果连接失败,会导致在执行SELECT语句时抛出SQLException异常。因此,建议在打开连接之后添加一个if语句来检查连接是否成功打开。另外,还需要检查是否有与该预约码相匹配的记录。如果没有,则也会导致跳转到“签到失败”页面。因此,建议添加一个else语句,在其中输出“该预约码无效”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书商城-图书列表</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> 0">
{{bk.bookName}} ¥{{bk.bookPrice}} <iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe> <script src="/BookShopSystem_war/layui/layui.js"></script>这是完整的html代码,请结合你给出的分页功能整合出完整的代码
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<body>
<input type="button" value="新增" onclick="add()">
ID
姓名
年龄
密码
修改 删除
<c:if test="${not empty list }">
<c:forEach items="${list }" var="user" varStatus="s">
${s.count}
${user.username}
${user.age}
${user.password}
修改
删除
</c:forEach>
</c:if>
<script >function add(){
location.href="add.jsp";
}
function updatemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能修改别人的信息");
} else {
window.location.href = "update.jsp";
}
}
function deletemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能删除别人的信息");
} else {
if(confirm("你确定要删除自己的信息吗?")) {
window.location.href = "index.jsp";
}
}
}</script>
</body>
</html>上述代码哪里报错,要如何修改
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
-
- 潮影森美 全硅胶性感甜心胶实体娃娃 159cm 169普通款 - <input type="number" id="gnum" data-id="71790817" class="gnum c_pt" min="1" value="7"> + ¥15200.00 删除
帮我做一个js代码,我点击dl标签class为car car_on时候点一次将他class属性设置为car,再次点击时恢复为car car_on,并判断如果class为car car_on时,将data-id和input的value值用post传值到t.php页面并回调显示在一个id为count的em标签里面,请写出完整的js代码和PHP代码
![](https://s.cccgx.com/t/200x200/images/qu/18976/gallery/m20211019094313_99448.jpg)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
- <input type="number" class="gnum c_pt" id ="gnumCpt" value="1"/>+ - <input type="number" class="gnum c_pt" id ="gnumCpt" value="1"/>+ 减不能小于1,并实现乘除算法输出结果在一个标签里。 ¥13500.00 全选 去结算(3) 共计:¥148000 总额:¥148000,优惠¥0 - <input type="number" class="gnum c_pt" id ="gnumCpt" value="1"/> + 全选 去结算(2) 共计:¥{{goods_price}} 总额:¥179000,优惠¥0 并将idw为gnumCpt的value值乘class为ca_jg的数,显示在b标签id为count里
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)