简体   繁体   English

Java:MySQL对象在关闭连接时引发错误

[英]Java: MySQL object throws error on closing connection

I'm relativly new to java and i've written an object (with help from http://www.tutorialspoint.com/jdbc/ ) to handle a MySQL db connection like this: 我是java的新手,并且已经编写了一个对象(在http://www.tutorialspoint.com/jdbc/的帮助下),用于处理这样的MySQL数据库连接:

package rekening.db;
import java.sql.*;

public class dbConnection1 {
private final String dbUrl;
private final String dbUser;
private final String dbPass;
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;

/**
 * @param args the command line arguments
 */
//Constructor
public dbConnection1(String dbUrl, String dbUser, String dbPass) {
    this.dbUrl = dbUrl;
    this.dbUser = dbUser;
    this.dbPass = dbPass;

}

public boolean initiateConnection() throws SQLException {
try{
   //STEP 2: Register JDBC driver
   Class.forName("com.mysql.jdbc.Driver");

   //STEP 3: Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);
   return true;
}catch(SQLException se){
   //Handle errors for JDBC
   se.printStackTrace(); 
}catch(Exception e){
   //Handle errors for Class.forName
   e.printStackTrace();
}
return false;
}

public ResultSet executeQuery(String dbStatement) throws SQLException {
try{
   //STEP 4: Execute a query
   System.out.println("Creating statement...");
   stmt = conn.createStatement();
   String sql;
   sql = dbStatement;
   ResultSet rs = stmt.executeQuery(sql);

   //STEP 5: Extract data from result set
   while(rs.next()){
      //Retrieve by column name
      String DESC_ORIGINAL = rs.getString("DESC_ORIGINAL");
      String DESC_TRIMMED = rs.getString("DESC_TRIMMED");
      String DESC_EDITED = rs.getString("DESC_EDITED");
      String DESC_CATEGORY_EDITED = rs.getString("DESC_CATEGORY_EDITED");
      String DESC_SUBCATEGORY = rs.getString("DESC_SUBCATEGORY");


      //Display values
      System.out.print("Orignal: " + DESC_ORIGINAL);
      System.out.print(", Trimmed: " + DESC_TRIMMED);
      System.out.print(", Edited: " + DESC_EDITED);
      System.out.println(", Category: " + DESC_CATEGORY_EDITED);
      System.out.println(", Subcategory: " + DESC_SUBCATEGORY);
   }
   return rs;
   }catch(SQLException se){
   //Handle errors for JDBC
   se.printStackTrace(); 
   }

return rs;
}

public void closeConnection() throws SQLException {
   //STEP 6: Clean-up environment
try{
   rs.close();
   stmt.close();
   conn.close();
}
catch (SQLException se){
   //Handle errors for JDBC
   se.printStackTrace(); 
} 
finally{
   //finally block used to close resources
   try{
      if(stmt!=null)
         stmt.close();
   }catch(SQLException se2){
   }// nothing we can do
   if(conn!=null)
       conn.close();
}//end try        
}
}

from the main i call the dbConnection1 object: 从主要的角度来说,我叫dbConnection1对象:

try {
    dbConnection1 dbConn = new dbConnection1("jdbc:mysql://192.168.1.550/Finance","test","");
    dbConn.initiateConnection();
    dbConn.executeQuery("SELECT DESC_ORIGINAL, DESC_TRIMMED, DESC_EDITED, DESC_CATEGORY_EDITED, DESC_SUBCATEGORY FROM `TOOL.DESCRIPTION`");
    dbConn.closeConnection(); 
} catch (SQLException ex) {
    Logger.getLogger(Rekening.class.getName()).log(Level.SEVERE, null, ex);
}

The output returns the errors below. 输出返回以下错误。 But the strange thing is that it returns the rows from the db. 但是奇怪的是,它从数据库返回行。 The errors are in between the resultset. 错误在结果集中。 And in the end it says the build is succesfull. 最后,它说构建成功。 Can anybody help my please? 有人可以帮我吗? Thanks!! 谢谢!!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at rekening.db.dbConnection1.closeConnection(dbConnection1.java:105)
at rekening.ui.Rekening$1.run(Rekening.java:85)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 1 second)

First thing to notice is, that you close your stmt and connection twice in any case. 首先要注意的是,无论如何都要关闭stmt和连接两次。 First in the try block and then again in the finally block... Hard to say more, since your code does not have a line 105 ;-) 首先在try块中,然后在finally块中...很难说更多,因为您的代码没有第105行;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM