未找到空指针异常和jdbc.sql异常用户

mw3dktmi  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(178)

我让我的用户登录和注销每当我得到我的用户注册它提供了一个错误。
问题列表为
info[stdout](默认task-6)org.h2.jdbc.jdbcsqlexception:未找到表“user”;
sql语句:stdout](默认任务-6)插入到用户(firstname、password、lastname、email)值(?,?,?)[42102-173]
错误[stderr](默认任务-6)java.lang.nullpointerexception
com.ark.beans.user.add(user。java:113)
sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)处出现错误[stderr](默认任务-6)
sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)处出现错误[stderr](默认任务-6)。java:62)
我已经检查了每个名字和连接,但不是为我工作
这是我的用户类

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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题