0

我正在开发一个网上银行应用程序,这是我卡住的部分。

-> 用户在 HTML 页面上显示一个表单以输入他们的详细信息,以创建一个帐户。

-> 然后将详细信息输入 MySQL 数据库,并将用户重定向到 JSP 页面,其中应显示他的帐号(使用 MySQL 自动增量功能自动生成)和一个秘密的 4 位密码(随机生成)对于每个用户,而不是通过自动增量)。

我的 servlet 的 doPost() 方法中用于在 DB 中插入值并重定向到 JSP 的代码 sinppet 在这里-

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/inb", "root", "root");

//The user's password has to be converted into a salted hash before storing in the database.
String hashed_password=CreateDigest.getPasswordDigest(request.getParameter("password"));

PreparedStatement ps=con.prepareStatement("insert into inb.users values (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        ps.setString(1, request.getParameter("name1"));
        ps.setString(2, request.getParameter("name2"));
        ps.setString(3, request.getParameter("name3"));
        ps.setString(4, request.getParameter("dob"));
        ps.setInt(5, Integer.parseInt(request.getParameter("age")));
        ps.setString(6, request.getParameter("email"));
        ps.setString(7, request.getParameter("mobnum"));
        ps.setString(8, request.getParameter("address1"));
        ps.setString(9, request.getParameter("address2"));
        ps.setString(10, request.getParameter("pannum"));
        ps.setString(11, request.getParameter("aadhar"));
        ps.setString(12, hashed_password);
        ps.setString(13, request.getParameter("acc_bal"));
        ps.setInt(14, pin);

            int i=ps.executeUpdate();


    //Check if records have been actually been inserted or not.
        if (i>0)    {
            response.sendRedirect("AccountOpened.jsp");
        }

    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

现在,这是显示帐号的 JSP 代码。和用户的 pin-(last_acc_no 和 last_pin 是我的 Servlet 类中的静态变量,我创建它是为了保存最后插入的帐户号和 pin。)

<% 
String acc_no=CreateAccount.last_acc_No;
int pin=CreateAccount.last_pin;
%>
<h3>Congratulations ! Your account has been created.</h3>.
<br/><br/>
Your Account Number Is : <b> <%= acc_no %> </b>
<br/><br/>
Your Secret PIN No. Is : <b> <%= pin %> </b>

现在,我想检索最后插入的用户的 account_no 和 pin,以便可以在 JSP 页面上显示它。而且我无法找到一种方法。

4

2 回答 2

0
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/inb", "root", "root");

//The user's password has to be converted into a salted hash before storing in the database.
String hashed_password=CreateDigest.getPasswordDigest(request.getParameter("password"));

PreparedStatement ps=con.prepareStatement("insert into inb.users values (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", , Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, request.getParameter("name1"));
        ps.setString(2, request.getParameter("name2"));
        ps.setString(3, request.getParameter("name3"));
        ps.setString(4, request.getParameter("dob"));
        ps.setInt(5, Integer.parseInt(request.getParameter("age")));
        ps.setString(6, request.getParameter("email"));
        ps.setString(7, request.getParameter("mobnum"));
        ps.setString(8, request.getParameter("address1"));
        ps.setString(9, request.getParameter("address2"));
        ps.setString(10, request.getParameter("pannum"));
        ps.setString(11, request.getParameter("aadhar"));
        ps.setString(12, hashed_password);
        ps.setString(13, request.getParameter("acc_bal"));
        ps.setInt(14, pin);

            int i=ps.executeUpdate(); 


    //Check if records have been actually been inserted or not.
            ResultSet rs = ps.getGeneratedKeys();
            if(rs.next())
            {
                int id = rs.getInt(1);
            request.setAttribute("id", id);
            request.setAttribute("pin", pin);
            request.getRequestDispatcher("AccountOpened.jsp").forward(request, response);
        }

    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在您的 jsp 页面中:

<h3>Congratulations ! Your account has been created.</h3>.
<br/><br/>
Your Account Number Is : <b> <%= request.getAttribute("id")%> </b>
<br/><br/>
Your Secret PIN No. Is : <b> <%= request.getAttribute("pin") %> </b>
于 2013-07-27T17:12:26.323 回答
0

你有没有考虑Return SCOPE_IDENTITY()在你的t-sql?

正在int i返回帐号 - 它应该是。这样您就不必担心它是数据库中最顶层的条目。严格地说您只想要创建的最后一条记录是危险的。用户 A 和用户 B 可能在同一时间创建一个帐户,并获得纠结/交叉的帐户信息。一旦你有了“int i”的accountNumber,那么从数据库中重新获取这些信息应该不会有问题吗?

这有帮助吗?

于 2013-07-27T17:12:33.477 回答