补充一点,vbb.user中有1条记录,但输出为0。

解决方案 »

  1.   

    String ls_sql = "select count(*) as no from user";
    ...
    li_count = rs.getInt("no");这样呢?
      

  2.   

    你的exception就这些吗?把所有输出信息贴出来。
    我怀疑你的数据库连接建立没有成功,cuz如果成功,应该有输出“数据库连接成功!”,否则应该有SQLException。另外,请在Statement Stmt = con.createStatement();和ResultSet rs = Stmt.executeQuery(ls_sql);两行语句前加上assert判断:
    if( con != null ){
        Statement Stmt = con.createStatement();
    }else{
       // out put invalid connection
    }
    if( Stmt != null ){
        ResultSet rs = Stmt.executeQuery(ls_sql);
    }else{
        // out put invalid statement
    }因为是jsp代码,我不便本地测试,你给出详细输出会更有帮助。
      

  3.   

    TO: chenyuan_tongji(codeguru)
    输出内容就这些,没有其它的了。我在resin中用:
    <resource-ref>
      <res-ref-name>jdbc/vbb</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <init-param driver-name="com.caucho.jdbc.mysql.Driver"/>
      <init-param url="jdbc:mysql_caucho://localhost:3306/vbb"/>
      <init-param user="root"/>
      <init-param password=""/>
      <init-param max-connections="20"/>
      <init-param max-idle-time="30"/>
    </resource-ref>然后用以下servlet,可以正常运行,输出结果也是正确的:
    import java.io.*;import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.naming.*;
    import javax.sql.*;public class TestDatabase extends HttpServlet {
      DataSource pool;  public void init()
        throws ServletException
      {
        try {
          Context env = (Context) new InitialContext().lookup("java:comp/env");      pool = (DataSource) env.lookup("jdbc/vbb");      if (pool == null)
            throw new ServletException("`jdbc/vbb' is an unknown DataSource");
        } catch (NamingException e) {
          throw new ServletException(e);
        }
      }  public void doGet(HttpServletRequest req,
                        HttpServletResponse res)
        throws IOException, ServletException
      {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();    Connection conn = null;
        try {
          conn = pool.getConnection();      Statement stmt = conn.createStatement();      ResultSet rs = stmt.executeQuery("select username, password from user");      out.println("Users:<br>");
          while (rs.next()) {
            out.print(rs.getString("username"));
            out.print(" ");
            out.print(rs.getString(2));
            out.println("<br>");
          }      rs.close();
          stmt.close();
        } catch (SQLException e) {
          throw new ServletException(e);
        } finally {
          try {
            if (conn != null)
              conn.close();
          } catch (SQLException e) {
          }
        }
      }
    }
      

  4.   

    TO: chenyuan_tongji(codeguru)
    我按你说的加了assert的判断,结果输出信息中说明:
    Undefined variable or class name: Stmt................好象是数据库没有连接成功,怎么回事?
      

  5.   

    我一点一点跟踪的结果,发现在
    Class.forName("org.gjt.mm.mysql.Driver");
    的时候就出现异常了,奇怪的是这个异常为什么没有catch好象是org.gjt.mm.mysql.Driver这样一个class没找到........
    在java.sun.com/jdbc上,有很明显的说明:
    jdbc api已经完全地包含在j2se1.4中............
    可为什么找不到呢??
      

  6.   

    你是用的mysql jdbc driver是由第三方提供的,你必须将它加入classpath中才可以。sun不提供某一数据库的jdbc driver,你所说的是jdbc api的统一接口,jdbc driver提供商必须实现这些api规范你需要找本书看看
      

  7.   

    谢谢chenyuan_tongji(codeguru),那么,你可以告诉我该看什么样的书吗?
    还有,这些JDBC Driver我该在哪里找?是不是MYSQL的,我就该到mysql去找?
    还有,我用jdbc-odbc桥也出同样的错,我又该到哪里去找呢?
      

  8.   

    jdbc-odbc是由sun提供的,自带在jdk中的,你需要通过
    Class.forName("jdbc.odbc.JdbcOdbcDriver")加载,另外你要为你的mysql数据库配置一个odbc源mm mysql jdbc是一个常用的mysql jdbc driver
    参考以下链接:
    http://www.mysql.com/downloads/api-jdbc.html至于参考图书,你可以先看看网上的教程,wrox的书不错,推荐