2017-07-22 94 views
1

後無操作基本上我有:允許連不上MYSQL

String query = "SELECT * FROM table WHERE UNIQUEID=? AND DIR IS NOT NULL AND NAME IS NOT NULL AND PAGETYPE IS NOT NULL"; 
DBConnect Database = new DBConnect(); 
Connection con = null; 
PreparedStatement ps = null; 
ResultSet rs=null; 
try { 
    con = Database.getcon(); 
    ps = con.prepareStatement(query); 
    ps.setString(1, URI); 
    rs=ps.executeQuery(); 
    if(rs.next()){ 
    } 
} finally { 
    if(ps != null) 
     ps.close(); 
    if(rs != null) 
     rs.close(); 
    if(con != null) 
     con.close(); 
} 
query = "SELECT COUNTCOMMENTS FROM videosinfos WHERE UNIQUEID=?"; 
try { 
    con = Database.getcon(); 
    ps = con.prepareStatement(query); // Getting error here 
    rs=ps.executeQuery(); 
    ps.setString(1, URI); 
    rs=ps.executeQuery(); 
    if(rs.next()){ 
     comments = rs.getInt(1); 
    } 
} finally { 
    if(ps != null) 
     ps.close(); 
    if(rs != null) 
     rs.close(); 
    if(con != null) 
     con.close(); 
} 

注:行,我得到的錯誤,有它的註釋。

連接到數據庫:

public DBConnect(){ 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 

     String unicode="useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8"; 
     con = DriverManager.getConnection("jdbc:mysql://localhost:15501/duckdb?"+unicode, "root", "_PWD"); 
     st = con.createStatement(); 
    }catch(Exception ex){ 
     System.out.println(ex.getMessage()); 
     System.out.println("couldn't connect!"); 
    } 
} 
public Connection getcon(){ 
    DBConnect condb = new DBConnect(); 
    Connection connect = con; 
    return con; 
} 

但是在編譯我得到這個錯誤:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. at sun.reflect.GeneratedConstructorAccessor18.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1246) at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1241) at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4102) at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4071) at duck.reg.pack.DBConnect.getitemfull_details(DBConnect.java:686) at duck.reg.pack.index.doPost(index.java:73) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

我使用Debian與MariaDB的v10.x/MySQL的

回答

2

這是因爲兩個原因:

  • 連接是第一PreparedStatement
  • getcon執行後關閉使用代替local可變的instance變量。因此,當下次調用getcon時,相同的conn變量(先前關閉的變量)將返回。

要解決這個問題,getconDBConnect需要進行修改,以聲明局部變量conn,並返回它(實際上,你並不需要DBConnect在所有的),例如:

public Connection getcon(){ 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     String unicode="useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8"; 
     return DriverManager.getConnection("jdbc:mysql://localhost:15501/duckdb?"+unicode, "root", "_PWD"); 
    }catch(Exception ex){ 
     System.out.println(ex.getMessage()); 
     System.out.println("couldn't connect!"); 
     throw new RuntimeException(ex); 
    } 
} 
+0

謝謝,工作得很好 – Whatever

0

嘗試更改:

ps.close(); 
    rs.close(); 
    con.close(); 
} finally { 
    if(ps != null) 
     ps.close(); 
    if(rs != null) 
     rs.close(); 
    if(con != null) 
     con.close(); 
} 

到:

} finally { 
    if(ps != null) 
     ps.close(); 
    if(rs != null) 
     rs.close(); 
    if(con != null) 
     con.close(); 
} 

psrscon關閉兩次。

+0

對不起,這是一個額外的代碼。我已經刪除了它。更新我的帖子 – Whatever

-1

一好的方法是使用一個嘗試 - 最後像這樣塊

String query = "SELECT * FROM table WHERE UNIQUEID=? AND DIR IS NOT NULL AND NAME IS NOT NULL AND PAGETYPE IS NOT NULL"; 
     DBConnect Database = new DBConnect(); 
     Connection con = null; 
     PreparedStatement ps = null; 
     ResultSet rs=null; 
     try { 
      con = Database.getcon(); 
      ps = con.prepareStatement(query); 
      ps.setString(1, URI); 
      rs=ps.executeQuery(); 
      if(rs.next()){ 
      } 

      query = "SELECT COUNTCOMMENTS FROM videosinfos WHERE UNIQUEID=?"; 

      ps = con.prepareStatement(query); // Getting error here 
      rs=ps.executeQuery(); 
      ps.setString(1, URI); 
      rs=ps.executeQuery(); 
      if(rs.next()){ 
       comments = rs.getInt(1); 
      } 
     } finally { 
      if(ps != null) 
       ps.close(); 
      if(rs != null) 
       rs.close(); 
      if(con != null) 
       con.close(); 
     }