找不到空指针异常和JDBC.sql异常用户

时间:2019-01-09 04:44:49

标签: java mysql jsf java-ee javaloader

每当我使用户注册时,我都会使我的用户登录并注销,这会导致错误。

问题清单是

INFO [stdout](默认任务6)org.h2.jdbc.JdbcSQLException:未找到表“ USER”;

SQL语句: stdout](默认任务6)插入用户(名字,密码,姓氏,电子邮件)VALUES(?,?,?,?)[42102-173]

错误[stderr](默认任务6)java.lang.NullPointerException

com.ark.beans.User.add(User.java:113)出现[stderr]错误(默认任务6)

sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处出现错误[stderr](默认任务6)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)出现[stderr]错误(默认任务6)

我检查了每个名称和联系,但对我不起作用

这是我的User类

public User() {
    try {
        Context ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/database");
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

public String add() {
    int i = 0;
    if (firstName != null) {
        PreparedStatement ps = null;
        Connection con = null;
        try {
            if (ds != null) {
                con = ds.getConnection();
                if (con != null) {
                    String sql = "INSERT INTO user(firstname, password, lastname, email) VALUES(?,?,?,?)";
                    ps = con.prepareStatement(sql);
                    ps.setString(1, firstName);
                    ps.setString(2, password);
                    ps.setString(3, lastName);
                    ps.setString(4, email);
                    i = ps.executeUpdate();
                    System.out.println("Data Added Successfully");
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                con.close();
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    if (i > 0) {
        return "success";
    } else
        return "unsuccess";
}

public void dbData(String uName) {
    if (uName != null) {
        PreparedStatement ps = null;
        Connection con = null;
        ResultSet rs = null;

        if (ds != null) {
            try {
                con = ds.getConnection();
                if (con != null) {
                    String sql = "select firstname,password from user where firstname = '"
                            + uName + "'";
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    rs.next();
                    dbName = rs.getString("firstname");
                    dbPassword = rs.getString("password");
                }
            } catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }
}

public String login() {
    dbData(firstName);
    if (firstName.equals(dbName) && password.equals(dbPassword)) {
        return "output";
    } else
        return "invalid";
}

public void logout() {
    FacesContext.getCurrentInstance().getExternalContext();
    FacesContext.getCurrentInstance()
            .getApplication().getNavigationHandler()
            .handleNavigation(FacesContext.getCurrentInstance(), null, "/login.xhtml");
}
}

这是context.xml

<Resource name="jdbc/database" auth="Container" type="javax.sql.DataSource"
          username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/appdb" />

获取用户登录的表格

<h:form id="registerForm">
    <table>
        <tr>
            <td><h:outputText value="Enter Your First Name:" /></td>
            <td><h:inputText id="fname" value="#{user.firstName}"
                             required="true" requiredMessage="Please enter your first name" /></td>
            <td><h:message for="fname" style="color:red" /></td>
        </tr>
        <tr>
            <td><h:outputText value="Enter Your Last Name:" /></td>
            <td><h:inputText id="lname" value="#{user.lastName}"
                             required="true" requiredMessage="Please enter your last name" /></td>
            <td><h:message for="lname" style="color:red" /></td>
        </tr>
        <tr>
            <td><h:outputText value="Enter Your email ID:" /></td>
            <td><h:inputText id="email" value="#{user.email}"
                             required="true" requiredMessage="Please enter your email id" /></td>
            <td><h:message for="email" style="color:red" /></td>
        </tr>
        <tr>
            <td><h:outputText value="Enter Password :" /></td>
            <td><h:inputSecret id="psw" value="#{user.password}"
                               required="true" requiredMessage="Please enter your password" /></td>
            <td><h:message for="psw" style="color:red" /></td>
        </tr>
        <tr>
            <td />
            <td><h:commandButton value="Register" action="#{user.add}" /></td>
        </tr>
        <tr>
            <td><h:outputLink value="home.xhtml">Home</h:outputLink></td>
        </tr>
    </table>
</h:form>

0 个答案:

没有答案