首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

查询结果如何不能得到

2011-11-24 
查询结果怎么不能得到这是我的程序:Stringactionrequest.getParameter( action )//System.out.println

查询结果怎么不能得到
这是我的程序:
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 ");
               
        }


[解决办法]
仔细看看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()
[解决办法]
其实对于添加数据你可以用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 对象
[解决办法]
你确定那些字段名都正确吗,username、password?竟然while(rs.next()){}这个循环能执行进去说明是有记录的,你debug调了吗,有没有什么异常信息?

热点排行