JDBC和sqlserver连接,查询的异常
小弟以前是用mysql数据库的 ,现在的系统要求使用sqlserver 2000,
sql=select * from tablename
Statement stmt = cn.createStatement();
stmt.executeQuery(sql);
数据库操作就大概上面的,然后执行的话,就出现下面异常,不知道是什么原因,在mysql下就没有这样问题;
java.sql.SQLException: [TDS Driver]Illegal method 's call for result set of type TYPE_FORWARD_ONLY.
------解决方案--------------------贴段代码给你参考下,用的驱动是jtds的驱动
Connection con;
Statement stmt;
// ResultSet rs;
try{
Class.forName( "net.sourceforge.jtds.jdbc.Driver ");
DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
String dbUrl = "jdbc:jtds:sqlserver: " +
"//localhost:1433/test;user=sa;password=root ";
con = java.sql.DriverManager.getConnection(dbUrl);
String sql= " ";
stmt = con.createStatement();
for(int i=0;i <100;i++){
sql= "insert into Test1(id) values( ' "+i+ " ') ";
stmt.executeUpdate(sql);
}
/**//*rs = stmt.executeQuery( "SELECT * FROM Test1 ");
while(rs.next()){
System.out.println(rs.getString(1));
}*/
// rs.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println( "error ");
}
System.out.println( "OK2 ");
------解决方案--------------------记录取数据只能向前,不能回头,楼主回头了
------解决方案--------------------可以这样:stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
来获得可以回头的记录结果集
------解决方案--------------------字段也只能顺序取,比如字段顺序是 A,B,C
取了B,A就不能取了