2012-06-14 192 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的解答您的问题的核心答案。