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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?