日期:2014-05-18  浏览次数:20840 次

为什么无法利用RESULTSET进行赋值?
数据库查询结果保存到RESULTSET sqlRst里面,
用<%=sqlRst.getString(1)%>能将打印出来,可是用 String str=sqlRst.getString(1);却无法对str赋值,str一直是null。
这是怎么回事????


数据库的javabean:
package Bean;

import java.sql.*;

public class MyDbBean {
  private Statement stmt = null;
  ResultSet rs = null;
  private Connection conn = null;

//构造函数
  public MyDbBean() {
  }

//根据dsn参数,加载驱动程序,建立连接
  public void OpenConn(String dsn, String uid, String pwd) throws Exception {
try {
  dsn = "jdbc:odbc:" + dsn;
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  conn = DriverManager.getConnection(dsn, uid, pwd);
  } 
  catch (Exception ex) {
  System.err.println("aq.executeQuery: " + ex.getMessage());
  }
  }

//执行查询类的SQL语句,有返回集
  public ResultSet executeQuery(String sql) {
  rs = null;
  try {
  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

  rs = stmt.executeQuery(sql);
  } 
  catch(SQLException ex) { 
  System.err.println("aq.executeQuery: " + ex.getMessage());
  }
  return rs;
  }
  
//执行更新类的SQL语句,无返回集
  public void executeUpdate(String sql) {
  stmt = null;
  rs=null;
  try {
  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  stmt.executeQuery(sql);
  stmt.close();
  conn.close();
  } 
  catch(SQLException ex) { 
  System.err.println("aq.executeQuery: " + ex.getMessage());
  }
  }

//关闭对象
  public void closeStmt(){
  try{
  stmt.close();
  }
  catch(SQLException ex) { 
  System.err.println("aq.executeQuery: " + ex.getMessage());
  }
  }

  public void closeConn(){
  try{
  conn.close();
  }
  catch(SQLException ex) { 
  System.err.println("aq.executeQuery: " + ex.getMessage());
  }
  }
}

查询代码:
  <jsp:useBean id="MyDataBean" scope="page" class="Bean.MyDbBean"></jsp:useBean>
  <%
  MyDataBean.OpenConn("hotel","sa","");
  sql="select * from room where Sex='"+sex+"' or Sex is NULL";
  sqlRst=MyDataBean.executeQuery(sql);
  %>


------解决方案--------------------
rs.next();没写~
把这个加你程序里就可以了~
然后最好在你关闭方法里写上个 关闭RS 的