2011-05-01 87 views
1
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); 
     } 
    } 
} 

例外:Jdbcrowset错误?返回nullpointer异常!

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.I谷歌上搜索了一圈,发现了这段代码和Sun的例子一样link。我错了吗?

+0

你试过另一个非常简单的表吗?因为虽然我没有MS SQL,但我用MySQL驱动程序尝试的代码基本相同,并没有任何问题。另外,你正在运行哪个JRE? – MJB 2011-05-01 18:53:08

+0

我在一个只有1列的测试表上试过了,但是这看起来没有什么区别。我使用jre 6.此外,它似乎是jdbc驱动程序的问题,因为这里[链接](http://grepcode.com/ file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/rowset/JdbcRowSetImpl.java#JdbcRowSetImpl.execute%28%29) – Singgum3b 2011-05-01 19:00:29

+0

是否可以尝试开源jtDS驱动程序? – MJB 2011-05-01 20:03:54

回答

1

好了,答案是切换到JTDS驱动器,它可以发现here

有清楚的东西在MS JDBC驱动程序bollixed起来。

0

我记得这个NullPointerException异常发生在我身上,但只有当我调用execute()时的ResultSet使用一个JdbcRowSetImpl作为参数

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; 
} 
2

我有同样的问题,来的时候我不应该这样做,即得出结论,问题是Microsoft驱动程序不支持conn.prepareStatemen(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);的组合 at microsoft website 导致prepare()方法异常。 我把源代码here创建了自己的MyJdbcRowSetImpl和改变prepareStatement参数ResultSet.TYPE_SCROLL_SENSITIVE

JTDS司机wasnt解决方案,它不支持行集。