【问题标题】:Jdbcrowset bug ? return nullpointer exception!Jdbcrowset 错误?返回空指针异常!
【发布时间】:2011-05-01 13:44:55
【问题描述】:
package CrimeFile;

import com.sun.rowset.JdbcRowSetImpl;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.JdbcRowSet;

/**
 *
 * @author singgum3b
 */
public class test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
                    JdbcRowSet jrsi=new JdbcRowSetImpl();
                    jrsi.setUrl("jdbc:sqlserver://localhost:1433;databaseName=CrimeFile");                    
                    jrsi.setUsername("sa");
                    jrsi.setPassword("hellokitty");
                    jrsi.setCommand("select * from dbo.Target");
                    jrsi.execute();
        }              
        catch (SQLException ex) {            
            Logger.getLogger(test.class.getName()).log(Level.ALL, null, ex);
        } 
    }
}

例外:

Exception in thread "main" java.lang.NullPointerException
    at com.sun.rowset.JdbcRowSetImpl.prepare(JdbcRowSetImpl.java:666)
    at com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:553)
    at CrimeFile.test.main(test.java:30)
Java Result: 1

(第 30 行是 crsi.excute();
我使用的是 sql server 2008 和 ms jdbc 3.0。我四处搜索,发现这段代码与 Sun 的示例 link 中的代码相同。我错了吗?

【问题讨论】:

  • 您是否尝试过另一个非常简单的表格?因为虽然我没有 MS SQL,但我尝试了与 MySQL 驱动程序基本相同的代码,没有问题。另外,您正在运行哪个 JRE?
  • 我在只有 1 列的测试表上尝试过,但这似乎没有区别。我使用的是 jre 6。此外,它似乎是 jdbc 驱动程序问题,因为这里是link跨度>
  • 可以试试开源jtDS驱动吗?
  • 非常感谢!这显然是一个 MS JDBC 错误,我切换到 jtDS 驱动程序,事情迅速发展。请做出答案,因为我自己无法发布答案。

标签: java sql-server jdbc


【解决方案1】:

我遇到了同样的问题,并得出结论,问题是微软驱动程序不支持conn.prepareStatemen(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); 的组合,如所述 at microsoft website 导致 prepare() 方法异常。 我从here 获取源代码创建了我自己的MyJdbcRowSetImpl 并将prepareStatement 的参数更改为ResultSet.TYPE_SCROLL_SENSITIVE

Jtds 驱动程序不是解决方案,因为它不支持行集。

【讨论】:

    【解决方案2】:

    好的,答案是切换到JtDS驱动,可以找到here

    MS JDBC 驱动程序中显然有一些东西。

    【讨论】:

      【解决方案3】:

      我记得这个 NullPointerException 发生在我身上,但只有当我不应该这样做时调用 execute(),即使用 JdbcRowSetImpl 和 ResultSet 作为参数时

      private JdbcRowSet executeWithResultSet(Connection conn, String sqlQuery)
              throws SQLException {
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery(sqlQuery);
          JdbcRowSet jdbcRs = new JdbcRowSetImpl(rs);
          jdbcRs.execute(); //<-- results to the error as reported
      
          return jdbcRs;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多