end-of-of-result-set in-mysql

8aqjt8rx  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(380)

我对mysql使用like语句,但不知怎么的,它的行为超出了我的预期。它给了我“java.sql.sqlexception:after end of result set in mysql”的消息,我不知道发生了什么。
我试着抬起头,人们说while(rs.next()){…}可以修复它,但对我来说不起作用。

public ArrayList<Person> lookUp(String keywordContent) throws SQLException {

        //  Four different string query to look for all fields
        String query1 = ("Select * from personinfo where FirstN like ?");
        String query2 = ("Select * from personinfo where MiddleI like ?");
        String query3 = ("Select * from personinfo where LastN like ?");
        String query4 = ("Select * from personinfo where studentId like ?");

        //  Four result set to get each query result
        pst = con.prepareStatement(query1);
        pst.setString(1, "%"+keywordContent+"%");
        ResultSet rs1 = pst.executeQuery();
        //--
        pst = con.prepareStatement(query2);
        pst.setString(1, "%"+keywordContent+"%");
        ResultSet rs2 = pst.executeQuery();
        //--
        pst = con.prepareStatement(query3);
        pst.setString(1, "%"+keywordContent+"%");
        ResultSet rs3 = pst.executeQuery();
        //--
        pst = con.prepareStatement(query4);
        pst.setString(1, "%"+keywordContent+"%");
        ResultSet rs4 = pst.executeQuery();

        ArrayList<Person> peopleAL = new ArrayList<>();

       while (rs1.next()) {
            peopleAL.add(new Person(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
        }
        while (rs2.next()) {
           peopleAL.add(new Person(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
        }
        while (rs3.next()) {
            peopleAL.add(new Person(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
        }
        while (rs4.next()) {
           peopleAL.add(new Person(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
        }

        return peopleAL;
    }

我有个例外:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.keyReleased(ButtonBehavior.java:136)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.callAction(ButtonBehavior.java:106)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$74(BehaviorBase.java:135)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352(GlassViewEventHandler.java:248)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
    at com.sun.glass.ui.View.notifyKey(View.java:966)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 49 more
Caused by: java.sql.SQLException: After end of result set
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:516)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:944)
    at bookswapakm.DBConn.lookUp(DBConn.java:322)
    at bookswapakm.FXMLDocumentController.lookUp(FXMLDocumentController.java:476)
    ... 59 more

我想让它查找包含我正在传递的关键字的所有数据。

ymzxtsji

ymzxtsji1#

您的问题是,您试图在同一个连接上同时打开四个结果集。数据库连接通常一次只能处理一个查询。你需要做的是
准备并执行第一个查询
获取并处理第一个查询中的行
准备并执行第二个查询
获取并处理第二个查询中的行
等等。

相关问题