jsp error question (Illegal operation on empty result set)

Asked 2 years ago, Updated 2 years ago, 86 views

When you run jsp,

The error "Illegal operation on empty result set." I don't know what the problem is.

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>

<%@page import="java.io.PrintWriter" %>

<%@page import="user.UserDAO" %>

<%@page import="java.sql.Timestamp" %>

<%@ page import ="java.sql.DriverManager"%>

<%@ page import ="java.sql.SQLException"%>

<%@ page import ="java.sql.ResultSet"%>

<%@ page import ="java.sql.PreparedStatement"%>

<%@ page import ="java.sql.Connection"%>

<!DOCTYPE html> Web site <!-- Add bootstrap css -->

 <!-- Add custom css-->
    <link rel="stylesheet" href="./css/custom.css">
    </head>

    <body>
    <nav class = "navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="index.jsp">Webpage for DB integration</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbar">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="index.jsp">main</a>
                    </li>

                    <li class="nav-item active">
                        <a class="nav-link" href="boardList.jsp"> Bulletin</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" id="dropdown" data-toggle="dropdown">
                             Member management
                        </a>
                        <div class="dropdown-menu" aria-labelledby="dropdown">
                        <a class="dropdown-item"href="userLogin.jsp">Login</a>
                        <a class="dropdown-item"href="userRegister.jsp">Membership</a>
                        <a class="dropdown-item"href="userLogout.jsp">Logout</a>

Search

 <!-- Add J-Query JavaScript-->
        <script src="./js/jquery.min.js"></script>
        <!--Add Popper JavaScript -->
        <script src="./js/popper.min.js"></script>
        <!-- Add bootstrap JavaScript-->
        <script src="./js/bootstrap.min.js"></script>



<%
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
        String dbURL = "jdbc:mysql://localhost:3308/test01";
        String dbID ="root";
        String dbPassword = "jyyoo";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(dbURL, dbID, dbPassword);
        String sql = "select * from test where name =?";

        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, name);
        rs = pstmt.executeQuery();

        String story = rs.getString("story");
    %>

    <form name ='frm1' method ='post' action='updateOk.jsp'>
    <table>
        <tr>
            <td>File Name</td>
            <td><%=name %><input type = "hidden" name="name" value="<%=name%>"/></td>
        </tr>
        <tr>
            <td>Content</td>
            <td><input type = "text" name="story" value="<%=story%>"/></td>
        </tr>

        <tr>
            <td colspan="2" align="center">
            <input type="button" name="btn1" value="Save" onclick="javascipt:frm1.submit();"/>
            <input type="button" name="btn2" value="list" onclick="javascipt:location.href='boardList.jsp';"/>
            </td>
        </tr>   
    </table>
    </form>
    <%
        }catch(SQLException se) {
            System.out.println(se.getMessage());
            }finally{
                try{
                    if(rs!=null) rs.close();
                    if(pstmt!=null) pstmt.close();
                    if(con!=null) con.close();
                }catch(SQLException se) {
                    System.out.println(se.getMessage());
                }
            }
    %>

    <script type ="text/javascript">
        function update() {
            document.frm1.submit();
        }
        function list(){
            location.href="boardList.jsp";
        }
    </script>

jsp web

2022-09-22 13:52

1 Answers

The result set must be empty. Take a picture of the rss.

        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, name);
        rs = pstmt.executeQuery();

        //result set verification - This is how you normally validate it.
        if(rs.next()) {
            String story = rs.getString("story");
        }

        Or

        while(rs.next()) {
            String story = rs.getString("story");
        }


2022-09-22 13:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.