java.sql.SQLException:[Microsoft] [ODBC Driver Manager]游标状态无效 [英] java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

查看:98
本文介绍了java.sql.SQLException:[Microsoft] [ODBC Driver Manager]游标状态无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的应用程序,将数据库值与textfield值进行比较,但是当我执行项目时,它会给我以下异常


java.sql.SQLException :[Microsoft] [ODBC Driver Manager]无效的游标状态


这是我使用的代码:

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
String dataSource =testDb;
String U =jdbc:odbc:+ dataSource;
try {
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
Connection con = DriverManager.getConnection(U,,);

PreparedStatement s = con.prepareStatement(Select * from student);
ResultSet s1 = s.executeQuery();


String textField;
textField = jTextField1.getText();
String database = s1.getString(1);
if(textField.equals(database)){
System.out.println(ok);
} else {
System.out.println(Not ok);
}
} catch(Exception ex){
System.out.println(EXCEPTION IS+ ex);
}
}

c>异常 c>

解决方案 ,即的初始位置 ResultSet BEFORE 第一行。



rs.next()将其推进到第一行。



处理 if(s1.next()){} 阻止


I created a simple application that compares a database value with a textfield value, but when I execute the project it gives me the following Exception:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

This is the code I'm using:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
   String dataSource ="testDb";
   String U="jdbc:odbc:"+dataSource;
   try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con=DriverManager.getConnection(U,"","");

      PreparedStatement s=con.prepareStatement("Select * from student");
      ResultSet s1=s.executeQuery();


      String textField;
      textField=jTextField1.getText();
      String database =s1.getString(1);
      if(textField.equals(database)) { 
         System.out.println("ok");
      } else {
         System.out.println("Not ok");
      }
   } catch(Exception ex) {
      System.out.println("THE EXCEPTION IS"+ex);
   }
}

What could be the cause if this Exception?

解决方案

When you do s.executeQuery(), the initial position of the ResultSet is BEFORE the first row.

You have to do rs.next() to advance it to the first row.

Enclose your ResultSet processing in a if ( s1.next() ) {} block

这篇关于java.sql.SQLException:[Microsoft] [ODBC Driver Manager]游标状态无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆