gpt4 book ai didi

java - MySQL语句关闭后不允许进行任何操作

转载 作者:行者123 更新时间:2023-11-29 02:17:56 25 4
gpt4 key购买 nike

我目前正在用 MySQL 做一个 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 类基本上是我的一个工作项目的完整副本,但由于某种原因我无法让它为这个应用程序工作。

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;
}

不知道为什么 Connection 不工作,我试着四处乱逛,看看我的其他项目,看看有什么不同之处建立了连接,但找不到它,除非 callStmt.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 关闭和打开正确连接的实际目的。

最佳答案

您在下面的代码中使用了相同的Statement 对象

rs = callStmt.executeQuery(selectQuery);

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

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

关于 JDBC 在操作中的使用的几点注意事项:

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

引用资料:

关于java - MySQL语句关闭后不允许进行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36511787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com