这是我的程序:
String action = request.getParameter("action");
    //System.out.println(action);
    if ("certain".equals(action)) {
        String username = request.getParameter("username");
        System.out.println(username);
        String password = request.getParameter("password");
        System.out.println(password);
        String sql = "select password from bs_users where username = '" + username + "'";
        //String sql = "select sysdate from dual";
        System.out.println(sql);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@10.11.12.75:1521:xe";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url, "legend", "19840601");
                System.out.println("deng lu cheng gong!");
            } catch (SQLException e) {
                System.err.println("login database failure!");
            }
            Statement stmt = null;
            try {
                if (conn != null) {                    stmt = conn.createStatement();
                    System.out.println("produce result cheng gong !");
                } else
                    System.err.println("produce result occur error!");
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                System.err.println("bu dui");
            }
            ResultSet rs = null;
            try {
                if (stmt != null) {                    rs = stmt.executeQuery(sql);
                    System.out.println("cha xun jie guo cheng gong !");
                } else
                    System.err.println("cuo wu");
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                System.err.println("cha xun cuo wu");
            }
            try {
                if (rs != null) {
                    System.out.println("return result!");                    while (rs.next()) {
                        String ps = rs.getString("password");                        System.out.println(ps);
                        if (password.equals(ps)) {
                            JOptionPane.showMessageDialog(null, "login success", "welcome to legend bookshop", JOptionPane.INFORMATION_MESSAGE);
                            System.out.println("welcome to legend bookshop!");
                        } else
                            JOptionPane.showMessageDialog(null, "it's error your password", "login failure", JOptionPane.ERROR_MESSAGE);
                        System.out.println("login password error!");
                    }
                } else {
                    System.err.println("cha xun shi bai");
                }            } catch (SQLException e) {
                System.err.println("it is failure to link database");
                //JOptionPane.showMessageDialog(null, "link databae failure", "error message", JOptionPane.ERROR_MESSAGE);
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println("query closing failure!");
                }
            }        }
        catch
                (ClassNotFoundException
                e
                ) {
            System.err.println("can not link the database");
            //JOptionPane.showMessageDialog(null, "fail to connect database", "error message", JOptionPane.ERROR_MESSAGE);
        }    } else if ("cancel".equals(action)) {
        response.sendRedirect("index.jsp");
        
    }

解决方案 »

  1.   

    有沒有報錯啊?還是隻是取不到資料?
    你不是有很多print嗎?核對一下
      

  2.   

    贴错误信息出来,另外你可以打个断点debug一下,一步步跟看到底哪里抛了异常
    ps:楼主你这个提问的方式不对,建议你看看提问的智慧
      

  3.   

    我在while(rs.next()){String password=rs.getString("password");System.out.println
    (password);}的时候并没有输出password后面的都没有执行.
      

  4.   

    那后面是不是报错了???
    把那后面的try catch去掉看报什么错>/
      

  5.   

    把你的sql语句放到数据库执行一下看可有数据???
      

  6.   

    参考一下,我这段代码也是一样的问题:    try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@10.11.12.75:1521:xe";
            String sql = "insert into db_users values(?,?,?,?,?)";
            try {
                Connection conn = null;
                try {
                    conn = DriverManager.getConnection(url, "legend", "19840601");
                } catch (SQLException e) {
                    System.err.println("deng lu shi bai!");
                }
                PreparedStatement pst = null;
                if (conn != null) {
                    pst = conn.prepareStatement(sql);
                    try {
                        if (pst != null) {
                            System.out.println("there is result!");
                            pst.setString(1, username);
                            pst.setString(2, password);
                            pst.setString(3, mail);
                            pst.setString(4, location);
                            pst.setString(5, sex);
                            System.out.println("add sql data success!");
                            if (pst.execute())
                                System.out.println("add users success!");
                            else
                                System.out.println("failure to add user!");
                        }
                    } catch (SQLException e) {
                        System.out.println("produce result failure!");
                    }
                }
            } catch (SQLException e) {
                System.err.println("login database failure!");
            }
        } catch (ClassNotFoundException e) {
            System.err.println("driver class can not find !");
        }
    我执行的时候会出现如下提示:
    there is result!
    add sql data success!
    failure to add user!
    但是我检查数据库已经有数据存放再里面了.
      

  7.   

    仔细看看PreparedStatement的execute方法,下面是从api文档抄来的
    execute
    boolean execute()  throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句可以是任何种类的 SQL 语句。一些特别处理过的语句返回多个结果,execute 方法处理这些复杂的语句,executeQuery 和 executeUpdate 处理形式更简单的语句。 
    execute 方法返回一个 boolean 值,以指示第一个结果的形式。必须调用 getResultSet 或 getUpdateCount 方法来检索结果,并且必须调用 getMoreResults 移动到任何后面的结果。 返回:
    如果第一个结果是 ResultSet 对象,则返回 true;如果第一个结果是更新计数或者没有结果,则返回 false 
    抛出: 
    SQLException - 如果发生数据库访问错误或者为此方法提供一个参数
    另请参见:
    Statement.execute(java.lang.String), Statement.getResultSet(), Statement.getUpdateCount(), Statement.getMoreResults()
      

  8.   

    其实对于添加数据你可以用executeUpate()方法的,look
    executeUpdate
    int executeUpdate() throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL INSERT、UPDATE 或 DELETE 语句;或者是一个什么都不返回的 SQL 语句,比如 DDL 语句。 返回:
    1、对于 INSERT、UPDATE 或 DELETE 语句,返回行数   2、或者对于什么都不返回的 SQL 语句,返回 0 
    抛出: 
    SQLException - 如果发生数据库访问错误或者 SQL 语句返回一个 ResultSet 对象
      

  9.   

    你确定那些字段名都正确吗,username、password?竟然while(rs.next()){}这个循环能执行进去说明是有记录的,你debug调了吗,有没有什么异常信息?
      

  10.   

    你要先确定你确实能查到结果,把你打印出来的sql在数据库执行一边。如果有结果的话:  while (rs.next()) {
                            String ps = rs.getString("password");                        System.out.println(ps);
    应该能打印出ps的结果。还有确保你数据库有password这个字段。
      

  11.   

    这是执行后的结果:
    legend
    19840601
    select password from bs_users where username = 'legend'
    deng lu cheng gong!
    produce result cheng gong !
    cha xun jie guo cheng gong !
    return result!