你试试把rs=stmt.executeQuery("sql");这一句 改为:rs=stmt.executeQuery(sql);其他都应该是正确的呀

解决方案 »

  1.   

    to zgmg2003():
    不好意思,是我在CSDN上打错了.rs=stmt.executeQuery(sql);我的程序本来就是这个语句,还是一样的.
      

  2.   

    不知SQL Server里有没有Schema这样的东西,如果有,试试用select * from schema.table进行查询
      

  3.   

    !!你把"sql"作为查询语句传进去拉。。按照zgmg2003的改法就可以拉
      

  4.   

    你为什么不在bean中连接数据库呀
    我给你一段连接数据库的源码吧
     你调试一下行不行  
    (把String DBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
        String ConnStr="jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=mis";
        String MyUser="USAuser";
        String MyPassword="sh960112#";
      写到属性文件里 提高了jsp的快数快发

    源码如下import java.util.*;
    import java.sql.*;
    import java.io.*;/*******************************************************************************
     * <p>Title: 连接数据库</p>
     * <p>Description: 管理数据库的连接</p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author 王太平
     * @version 1.0
     ******************************************************************************/
    public class  dbConnBean
    {
      Connection conn =null;
      Statement stmt=null;
      ResultSet rs=null;
      //默认构造器
      public dbConnBean(){  }
      /*******************************************************************************
       * 方法名称:openConnection
       * 参数:无
       * 返回值类型:boolean
       * 说明:打开数据库Connection的方法
       *  为了打开数据库,读取位于该类路径下的Db.properties文件
       *******************************************************************************/
      public boolean openConnection(){
        //载入Property文件
        Properties prop=new Properties();
        try{
         InputStream is=getClass().getResourceAsStream("/db.properties");
          prop.load(is);
          if(is!=null) is.close();
        }
        catch(IOException e){
          System.out.println("读取属性文件出错");
        }
        //从Propery文件读取数据库连接信息
        String jdbc=prop.getProperty("drivers");
        String url=prop.getProperty("url");
        String user=prop.getProperty("user");
        String password=prop.getProperty("password");
        //加载JDBC驱动
        try{
          Class.forName(jdbc);
        }
        catch(ClassNotFoundException e){
          e.printStackTrace();
          return false;
        }
        //打开数据库
        try{
          conn=DriverManager.getConnection(url,user,password);
        }
        catch(SQLException e){    }
        return true;
      }
      /*******************************************************************************
       * 方法名称:executeQuery
       * 参数:query (SQL询问语句)
       * 返回值类型:java.sql.ResultSet
       * 说明:查询数据库的方法(SELECT)
       *******************************************************************************/
      public ResultSet executeQuery(String query) throws SQLException
      {
        stmt=conn.createStatement();
        rs=stmt.executeQuery(query);
        return rs;
      }
      /*******************************************************************************
       * 方法名称:executeUpdate
       * 参数:query (SQL询问语句)
       * 返回值类型:void
       * 说明:修改数据库的方法(update,delete,insert)
       *******************************************************************************/
      public void executeUpdate(String query) throws SQLException
      {
        this.stmt=conn.createStatement();
        stmt.executeUpdate(query);
        if (stmt!=null) stmt.close();
      }
      /*******************************************************************************
       * 方法名称:close
       * 参数:无
       * 返回值类型:void
       * 说明:安全的关闭数据库
       *******************************************************************************/
      public void close() throws SQLException
      {
        if (rs!=null) rs.close();
        if (stmt!=null) stmt.close();
        if (conn!=null) conn.close();
      }
      /*******************************************************************************
       * 方法名称:finalize
       * 参数:无
       * 返回值类型:void
       * 说明:从服务器回收资源
       *******************************************************************************/
      public void finalize() throws SQLException
      {
        this.close();
      }
    }
      

  5.   

    就是rs=stmt.executeQuery("sql");的问题,改为rs=stmt.executeQuery(sql);即可。
    仔细看,原因不用多说吧。
      

  6.   

    to jkit(郁闷之堕落的程序员):
    我不是已经说了嘛,rs=stmt.executeQuery("sql");是我在CSDN上敲错了.不是这个原因!!!!各位兄弟:
    后来我发现了一个问题,就是我把数据库名改为任意不存的数据库,它也是提示某一个表对象无效.所以我怀疑并不是这个查询语句的问题,而有可能是连接语句的问题.
    但是我实在找不出这个连接语句的哪个字母错了,请高手们指点啊.
      

  7.   

    换一下
    catch(SQLException ex)
               {
                System.err.println("aq.executeQuery:"+ex.getMessage());
               }用
    catch(SQLException ex)
               {
                ex.printStackTrack();
               }把错误给大家看一下。
      

  8.   

    String ConnStr="jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=mis";
     这一句DatabaseName=mis前面有空格,将空格去掉试一下,另外把rs=stmt.executeQuery("sql");中的引号去掉。