iteye_5102 2010-09-24 17:07
浏览 195
已采纳

我这个ResultSet哪里出了问题

[code="java"][size=large]import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import java.io.*;
import javax.servlet.http.*;

public class Registration extends HttpServlet {

private PreparedStatement pstmt;
private PreparedStatement stmtQuery;
private ResultSet rset;

/**
 * Constructor of the object.
 */
public Registration() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String ID = request.getParameter("ID");
    String password = request.getParameter("password");

    HttpSession httpSession = request.getSession();
    IDInformation IDInf = new IDInformation(ID, password);

    httpSession.setAttribute("IDInf", IDInf);

    out.println("<form method = \"post\" action = "
            + "\"http://localhost:8080/RegistrationLogInSystem/servlet/Registration\">");
    out.println("<br>Your ID and Password are : ");
    out.println("<br>ID: " + ID);
    out.println("<br>Password: " + password);
    out.println("<br><input type = \"submit\" value = \"Confirm\">");
    out.println("</form>");

    out.close();
}

/**
 * The doPost method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    HttpSession session = request.getSession();
    IDInformation IDInf = (IDInformation) session.getAttribute("IDInf");
    String ID = IDInf.ID;
    String password = IDInf.password;

    try {
        [color=red]while (rset.next()) {
            if (ID == rset.getString(1)) {
                out.println("The ID " + rset.getString(1)
                        + "has already existed");
                return;
            }
            out.println(rset.getString(1));
        }[/color]

        if (ID.length() == 0 || ID.length() > 20) {
            out.println("ID's length must between 0~20");
            return;
        }

        if (password.length() == 0 || password.length() > 20) {
            out.println("password's length must between 0~20");
            return;
        }

        pstmt.setString(1, ID);
        pstmt.setString(2, password);
        pstmt.executeUpdate();
        out.println("Registration Successed");
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        out.close();
    }
}

/**
 * Initialization of the servlet. <br>
 * 
 * @throws ServletException
 *             if an error occurs
 */
public void init() throws ServletException {
    initJdbc();
}

private void initJdbc() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/test", "root", "179401");

        pstmt = conn
                .prepareStatement("insert into RegistrationTable (ID, password) values (?, ?)");
        [color=red]stmtQuery = conn.prepareStatement("select ID from RegistrationTable");
        rset = stmtQuery.executeQuery();[/color]
        System.out.println("Init Successfully!");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public class IDInformation {
    private String ID = "";
    private String password = "";

    public IDInformation(String ID, String password) {
        this.ID = ID;
        this.password = password;
    }
}

}[/size]
[/code]

这是一个我自己写的注册页面
我从一个HTML页面获取ID和password,然后到数据库获取一个ResultSet查看注册页面填写的ID是否已经注册.
红色部分是有关结果集的代码,但是每一次confirm以后都是Registration Successfully.
即使我输入了数据库中已有的ID也会成功,也就说判断语句不起作用,帮我看看为什么

  • 写回答

1条回答 默认 最新

  • weixin_42525443 2010-09-25 08:45
    关注

    请在用到rs的时候进行创建
    [code="java"]
    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
    
        HttpSession session = request.getSession();  
        IDInformation IDInf = (IDInformation) session.getAttribute("IDInf");  
        String ID = IDInf.ID;  
        String password = IDInf.password;  
    
        try {
    

    Connection conn = DriverManager.getConnection(

    "jdbc:mysql://localhost/test", "root", "179401");
    PreparedStatement stmtQuery=conn.prepareStatement("select 1 from RegistrationTable where ID=?");
    stmtQuery.setString(1,ID);

    ResultSet rset = stmtQuery.executeQuery();
    if (rset.next()) {

    out.println("The ID " + rset.getString(1)

    + "has already existed");

    return;

    out.println(rset.getString(1));

    }

            if (ID.length() == 0 || ID.length() > 20) {  
                out.println("ID's length must between 0~20");  
                return;  
            }  
    
            if (password.length() == 0 || password.length() > 20) {  
                out.println("password's length must between 0~20");  
                return;  
            }
    

    PreparedStatement pstmt= conn.prepareStatement("insert into RegistrationTable (ID, password) values (?, ?)");

    pstmt.setString(1, ID);

    pstmt.setString(2, password);

    pstmt.executeUpdate();

    out.println("Registration Successed");

    } catch (Exception ex) {

    ex.printStackTrace();

    } finally {

    out.close();

    }

    }
    private void initJdbc() {

    try {
    Class.forName("com.mysql.jdbc.Driver");

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    [/code]
    然后代码对Connection ,PreparedStatement ,ResultSet 三个对象都要保证资源的关闭
    [code="java"]
    Connection conn=...;
    try{
    PreparedStatement pstmt=conn.prepareStatement(...);
    try{
    ResultSet rset = pstmt.executeQuery();
    try{
    while(rset.next()){
    ...
    }
    }finally{
    rset .close();
    }
    }finally{
    pstmt.close();
    }
    }finally{
    conn.close();
    }
    [/code]
    Good Luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch