每当我让我的用户注册时,我都会让我的用户登录并注销它会提供错误。
问题清单是
INFO [stdout](默认任务 6)org.h2.jdbc.JdbcSQLException:找不到表“USER”;
SQL 语句:stdout](默认任务 6)INSERT INTO 用户(名字、密码、姓氏、电子邮件)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)
我检查了每个名称和连接,但不适合我
这是我的用户类
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>