2012-06-14 187 views
1

這似乎是一個非常基本的問題,但我找不到任何解決方案。 我有下面的代碼與我:MySQL JDBC返回0結果

package com.test.db.util; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class JDBCConnect 
{ 
private Connection conn = null; 
private final String uname = "root"; 
private final String passwd = "[email protected]"; 
private String url = "jdbc:mysql://127.0.0.1:3306/TrainDB"; 
private final String className = "com.mysql.jdbc.Driver"; 

public void initConnection() 
{ 
    try 
    { 
     if(this.conn == null || this.conn.isClosed()) 
     { 
      try 
      { 
       Class.forName (className).newInstance(); 
       this.conn = DriverManager.getConnection(url, uname, passwd); 
       System.out.println("database connection established."); 
      } 
      catch(SQLException sqe) 
      { 
       sqe.printStackTrace(); 
      } 
      catch (InstantiationException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (IllegalAccessException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (ClassNotFoundException e) 
      { 
       e.printStackTrace(); 
      } 

     } 
    } 
    catch(SQLException sqle) 
    { 
     sqle.printStackTrace(); 
    } 
    //return this.conn; 
} 

public void disconnect() 
{ 
    if (conn != null) 
     { 
      try 
      { 
       conn.close(); 
       System.out.println ("Database connection terminated"); 
      } 
      catch (Exception e) { /* ignore close errors */ } 
     } 

} 

public void insertData(String sql) 
{ 
    PreparedStatement s; 
    try 
    { 

     if(conn == null || conn.isClosed()) 
     { 
      initConnection(); 
     } 
     s = conn.prepareStatement(sql); 
     int count = s.executeUpdate(); 
     s.close(); 
     System.out.println (count + " rows were inserted"); 
    } 
    catch (SQLException e) 
    { 
     e.printStackTrace(); 
     if (conn != null) 
     { 
      try 
      { 
       conn.close(); 
       System.out.println ("Database connection terminated"); 
      } 
      catch (Exception se) { /* ignore close errors */ } 
     } 
    } 

} 

public ResultSet query(String sql) 
{ 
    Statement s = null; 
    try 
    { 
     if(this.conn == null || this.conn.isClosed()) 
     { 
      initConnection(); 
     } 

        s = conn.createStatement(); 
     s.executeQuery(sql); 
     ResultSet rs = s.getResultSet(); 
     System.out.println("lets see " + rs.getFetchSize()); 
     return rs; 
    } 
    catch(SQLException sq) 
    { 
     System.out.println("Error in query"); 
     return null; 
    } 
    finally 
    { 
     try { 
      s.close(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 
} 

我在不同的類使用JDBCConnect:

import java.sql.ResultSet; 
import java.sql.SQLException; 

public class traininfo 
{ 

public static void main(String[] args) 
{ 
    JDBCConnect jdbcConn = new JDBCConnect(); 

    String sql = "SELECT id FROM testtable"; 
    ResultSet rs = jdbcConn.query(sql); 
    try { 
     System.out.println(rs.getFetchSize()); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if(rs != null) 
    { 
     try 
     { 
      while(rs.next()) 
      { 
       System.out.println(rs.getString("id")); 
      } 
      rs.close(); 
     } 
     catch(SQLException sqe) 
     { 

     } 
    } 
    jdbcConn.disconnect(); 
} 
} 

我不使用插入併發呼叫和讀取。如果我使用的mysql-工作臺(客戶端)相同的查詢,我得到正確的結果,但使用上述代碼,我得到

database connection established. 
lets see 0 
0 
Database connection terminated 

請給我建議,我缺少的是什麼?

+0

嗯..只是一分鐘.. – Sridhar

+0

也許ID不是一個字符串?試試rs.getInt(「id」)或者其他的。 – Sridhar

+0

沒有運氣.. :-( 事實上,System.out.println(「讓我們看看」+ rs.getFetchSize()); returnign 0 – piyush

回答

2

很可能是因爲您在使用ResultSet之前正在關閉Statement。奇怪的是它沒有拋出異常,但是這是不正確的。

作爲每Statement.close method JavaDoc

當Statement對象被關閉時,它的當前ResultSet對象,如果存在的話,也被關閉。

我建議使用某種回調來檢索ResultSet結果它關閉之前,例如: -

public <T> T query(String sql, IResultSetHandler<T> resultSetHandler) throws SQLException { 
    Statement statement = null; 
    try { 
     statement = connection.createStatement(); 
     final ResultSet rs = connection.executeQuery(sql); 
     final T result = resultSetHandler.handle(rs); 
     return result; 
    } finally { 
     if(statement != null) { 
      statement.close(); 
     } 
    } 
} 

public interface IResultSetHandler<T> { 
    T handle(ResultSet rs); 
} 

public static void main(String[] args) { 
    JDBCConnect jdbcConn = new JDBCConnect(); 
    List<String> ids = jdbcConn.query(sql, new IResultSetHandler<List<String>>() { 
     public List<String> handle(ResultSet rs) { 
      List<String> ids = new ArrayList<String>(); 
      while(rs.next()) { 
       ids.add(rs.getString("id")); 
      } 
      return ids; 
     } 
    }); 
} 

或者使用公共阿帕奇dbutils庫,它不完全一樣的。

+0

我最後使用s.close(),即使我刪除結果 – piyush

+0

編輯我的答案,請檢查 – Yura

+0

同意 - 在返回結果集之前關閉語句,結果集中不會有任何內容,並且根據其他答案,getFetchSize是完全合理的返回0,即使你匹配了幾百行 – Woody

0

難道是因爲你從來沒有打電話InsertRows,因爲它從來沒有顯示,「X行被插入」

+0

表不是通過jdbc創建的。 – piyush

1

ResultSet.getFetchSize()讓你知道最大數字連接將一次獲取行。您可以使用ResultSet.setFetchSize(int)進行設置。另見the official documentation。它不會告訴你總共有多少行。如果提取大小爲零,JDBC將自行決定。

除此之外,請參閱Yura的解答您的問題的核心答案。