当前位置: 首页 > 知识库问答 >
问题:

MySQL语句关闭后不允许操作

连正信
2023-03-14

我目前正在使用MySQL for DB connection开发一个Struts2 Web应用程序,并且在实际工作中遇到了代码部分的问题。恒定误差:

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after statement closed.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Statement.checkClosed(Statement.java:385)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1107)
at com.jpalenci.action.RequestAction.getUserData(RequestAction.java:75)
at com.jpalenci.action.RequestAction.execute(RequestAction.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:871)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1294)
at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1370)
at ognl.ASTMethod.getValueBody(ASTMethod.java:90)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.Ognl.getValue(Ognl.java:494)
at ognl.Ognl.getValue(Ognl.java:458)
at com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:315)
at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:346)
at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:313)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:430)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:290)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:251)
at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:168)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:140)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:567)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

我目前拥有的RequestAction类基本上是我的一个工作项目的完整副本,但由于某些原因,我无法使其用于此应用程序

请求操作。Java:

public String execute() throws SQLException{
    String ret ="";

    try{
        Class.forName(myDriver);

        try {
            getDBConn();
            callInsertUserData();
            getUserData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ret = SUCCESS;  
    } catch(Exception e){
        e.printStackTrace();
        ret = ERROR;
    } finally {
        if(dbConn != null){
            dbConn.close();
        }
    }

    return ret;
}

public static void getUserData() throws SQLException{

    getDBConn();

    try{        
        String selectQuery = "SELECT FullName, PhoneNumber, EmailAddress, CheckOutDate, DeviceUsage, DeviceSelection " 
        + "FROM devices_requested";

        rs = callStmt.executeQuery(selectQuery);

        rs.last();

        out_FullName = rs.getString("FullName");
        out_PhoneNumber = rs.getString("PhoneNumber");
        out_EmailAddress = rs.getString("EmailAddress");
        out_CheckOutDate = rs.getString("CheckOutDate");
        out_DeviceUseage = rs.getString("DeviceUsage");
        out_DeviceSelection = rs.getString("DeviceSelection");

        callStmt.executeUpdate();

        rs.close();

    } catch(SQLException e){
        e.printStackTrace();
    } finally {
        if(callStmt != null){
            callStmt.close();
        }
    }
}

/*
 * Method Which Calls To MySQL Stored Procedure That Inserts Information
 * That Comes From The Register Form,Which Then Is Updated Into The
 * DataBase.
 */
public static void callInsertUserData() throws SQLException{
    String insertUserData = "{CALL insertUserData(?,?,?,?,?,?)}";

    try{
        dbConn = getDBConn();
        callStmt = dbConn.prepareCall(insertUserData);

        callStmt.setString(1, in_FullName);
        callStmt.setString(2, in_PhoneNumber);
        callStmt.setString(3, in_EmailAddress);
        callStmt.setString(4, in_CheckOutDate);
        callStmt.setString(5, in_DeviceUsage);
        callStmt.setString(6, in_DeviceSelection);

        callStmt.executeUpdate();

    } catch (SQLException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if (callStmt != null) {
            callStmt.close();
        }
    }
}

/*
 * MySQL DataBase Connection, Attempts To Connect To Specified DB URL With
 * UserName & Password.
 */
private static Connection getDBConn() {
    Connection dbConn = null;

    try {
        Class.forName(myDriver);
    } catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    }
    try {
        dbConn = DriverManager.getConnection(myURL, dbUser, dbPass);
        return dbConn;
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return dbConn;
}

不知道为什么连接不工作,我试图乱搞,看看从我的其他项目,看看有什么差异,使连接和无法找到它,除非调用Stmt.executeUpdate();是给问题。

编辑:<代码>更新的callInUserData方法:

public static void callInUserData() throws SQLException {
    // CallableStatement callStmt = null;
    Connection dbConn = getDBConn();

    String inUserData = "{CALL inUserData(?,?,?,?,?,?)}";
    CallableStatement callStmt = dbConn.prepareCall(inUserData);

    try {
        // System.out.println("I am here" + FullName.toString());
        // System.exit(1);
        callStmt.setString("in_FullName", FullName);
        callStmt.setString("in_PhoneNumber", PhoneNumber);
        callStmt.setString("in_EmailAddress", EmailAddress);
        callStmt.setString("in_CheckOutDate", CheckOutDate);
        callStmt.setString("in_DeviceUsage", DeviceUsage);
        callStmt.setString("in_DeviceSelection", DeviceSelection);

        callStmt.execute();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
}

使用@Roman C推荐阅读的内容更新了我的插入方法,我的代码的其他部分也进行了更新,以适应关闭和打开MySQL正确连接的实际目的。

共有1个答案

谢俊悟
2023-03-14

您在下面的代码中使用了相同的语句

rs = callStmt.executeQuery(selectQuery);

此语句已被前面的方法关闭。您需要创建一个新的语句对象以用于查询。

Statement st = dbConn.createStatement();
rs = st.executeQuery(selectQuery);

Struts2通常用于web应用程序的表示层,而JDBC用于持久层。因此,不鼓励在操作中弄乱JDBC代码。从Struts2操作的视图端口来看,它不知道如何以及在何处存储对象,为了使代码更易于维护和测试,建议将操作代码与数据库操作代码分开。阅读更多关于Java EE用来构建web应用程序的DAO/DTO模式的信息。

参考文献:

  • 使用JDBC处理SQL语句
  • 核心J2EE模式-数据访问对象
 类似资料:
  • 我正在将MySQL与Java结合使用,试图制作shoppingcartDB,并试图删除从订购到现在已经过了30天的元组。 但是编译器说: 我如何解决这个问题? 代码: 抛出异常的代码行是: stmt。executeUpdate(“从篮子中删除,其中orderdate=““Odate”;”);

  • 问题内容: 我被错误卡住了,这里的第42行是,请帮我解决这个问题,我在这个问题上待了几个小时。 这是我的代码: 问题答案: 一个对象只能有一个active对象,因此在执行时,第一个ResultSet()被关闭。 创建两个对象,一个用于,另一个用于。 引用以下内容的javadoc : 默认情况下,每个对象只能同时打开一个对象。因此,如果一个对象的读取与另一对象的读取交错,则每个对象必须已由不同的对象

  • 问题内容: 我被错误卡住了,这里的第42行是,请帮我解决这个问题,我在这里待了几个小时。 这是我的代码: 问题答案: 一个对象只能具有一个active对象,因此在执行时,第一个ResultSet()被关闭。 创建两个对象,一个用于,另一个用于。 引用以下内容的javadoc : 默认情况下,每个对象只能同时打开一个对象。因此,如果一个对象的读取与另一个对象的读取是交错的,则每个对象必须已由不同的对

  • 问题内容: 我正在尝试执行getPendingSalesOrderIDs()方法,该方法调用方法selectInAsending(…)。 但这显示了一个SQLException,它说java.sql.SQLException:在ResultSet关闭后不允许进行该操作 此处db.endSelect()将关闭所有连接。 我认为问题就在于此。 问题答案: 如果关闭ResultSet,为什么不删除它(在

  • 我收到这个错误,我不知道为什么: SQLException: ResultSet关闭后不允许操作。错误出现在同时(rs1.next()){ 和consultaPortada方法:

  • 我试图为通过html形式的文本框检索的查询编写一个JUnit测试。文本检索已经过测试并有效,但我的单元测试失败了。我使用了两个相关的类:QueryController和QueryControlllerTest。我一直在玩弄我在这两个类中关闭的时间和内容,并不断得到错误:结果集关闭后不允许操作。 QueryController测试。Java语言 QueryController。Java语言