我是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
}
}
从main调用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);
}
输出返回以下错误。但奇怪的是它从数据库中返回行。错误位于结果集之间。最后它表示构建是成功的。有人可以帮助我吗?谢谢!
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)
答案 0 :(得分:0)
首先要注意的是,在任何情况下都要关闭stmt和连接两次。首先在try块中然后再在finally块中...很难说更多,因为你的代码没有第105行; - )