使用JSP中的postgresql数据库验证用户输入

时间:2014-04-27 22:39:45

标签: postgresql jsp

如何在JSP中验证数据库中是否已存在用户登录名?我在PreparedStatement statement = conn.prepareStatement(“SELECT * FORM UserAccount”)中不断出现异常错误。

<td><input type = "text" name = "user_name" value = "" /></td>
                        <%
                            String thatname = request.getParameter("user_name");
                            session.setAttribute("theName", thatname);

                            ResultSet rs = null;
                            // Verify if the username already exists in the database.
                            try {
                                //String userValidate = "SELECT * FORM UserAccount";
                                Connection conn = null;
                                PreparedStatement statement = conn.prepareStatement("SELECT * FORM UserAccount");
                                rs = statement.executeQuery();
                                while(rs.next()) {
                                    if(thatname == rs.getString("name")) {
                                        // do something
                                    }
                                }
                                rs.close();
                            }catch(SQLException e ) {
                                throw new RuntimeException(e);
                            } finally {
                                if (rs != null) {
                                    try {
                                        rs.close();
                                    } catch (SQLException e) { } // Ignore
                                    rs = null;
                                }
                            }

2 个答案:

答案 0 :(得分:0)

乍一看,你有:

Connection conn = null;
PreparedStatement statement = conn.prepareStatement("SELECT * FORM UserAccount");

尝试使用正确的连接。

答案 1 :(得分:0)

preparedStatement中存在语法错误,您在select语句中编写了FORM,它应该是FROM。确切的语法是

Connection conn=null;
PreparedStatement statement=conn.prepareStatement("SELECT * FROM UserAccount");