import java.sql.*; 
import java.io.*; 
public class DBColumn 
{ public static  void main(String args[]) 
{      
                          Connection con=null; 
                          Statement sm=null; 
                          String command=null; 
                          ResultSet rs=null; 
                          String result; 
            try 
              { 
                          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
                          System.out.println("驱动程序已加载");                                            
                   con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=model"); 
                              
                          System.out.println("OK,成功连接到数据库"); 
              }  catch(ClassNotFoundException classnotfound) {  } 
                catch(SQLException sql) { } 
      
      try 
            {    sm=con.createStatement(); 
                  command="select * from sysobjects"; 
                  rs=sm.executeQuery(command); 
                  System.out.println("查询结果为:"); 
                  while(rs.next()); 
                        {   System.out.println("查询结果为1010:");
                            result=rs.getString(1); 
                            System.out.println(result); 
                        } 
                        
            } 
      catch(Exception ex)    { ex.printStackTrace();  } 
      
      
      
    } } 
运行后:
驱动程序已加载
OK,成功连接到数据库
查询结果为1010:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.validateCursorPosition(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at DBColumn.main(DBColumn.java:40)为什么会这样呢?

解决方案 »

  1.   

    没有关闭connection,stmt,和结果集。
      

  2.   


    ....
           try 
                {    sm=con.createStatement(); 
                      command="select * from sysobjects"; 
                      rs=sm.executeQuery(command); 
                      System.out.println("查询结果为:"); 
                      while(rs.next()); 
                            {  System.out.println("查询结果为1010:"); 
                                result=rs.getString(1); 
                                System.out.println(result); 
                            } 
                            
                } 
          catch(Exception ex)    { 
                      ex.printStackTrace();  
               }
          finally{
              if(rs!=null){
                  try{
                     rs.close();
                  }catch(Exception e){
                     e.printStatckTrace();
                  }
               }
              if(sm!=null){
                  try{
                     sm.close();
                  }catch(Exception e){
                     e.printStatckTrace();
                  }
               }
              if(con!=null){
                  try{
                     con.close();
                  }catch(Exception e){
                     e.printStatckTrace();
                  }
               }
          } 
          
          ....
      

  3.   

    谢谢两位了,一:没加关闭;二:while语句后不知道什么时候多了个分号
    怪我粗心。。