这个问题在这里已有答案:
所以我有一个JSF页面,其中包含一个带有<h:inputtext>
和一个<h:commandbutton>
的panelgrid。当我按下按钮时,我希望inputtexts中引入的数据存储在数据库中。执行操作,问题是存储在数据库中的值为空。
这是豆:
package accountsPackage;
public class Accounts {
private String username;
private String password;
private String CustomerName;
private String CustomerFirstName;
private String CustomerMiddleName;
private String CustomerPhone;
private String CustomerMobilePhone;
private String repassword;
private String email;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCustomerName() {
return CustomerName;
}
public void setCustomerName(String customerName) {
CustomerName = customerName;
}
public String getCustomerFirstName() {
return CustomerFirstName;
}
public void setCustomerFirstName(String customerFirstName) {
CustomerFirstName = customerFirstName;
}
public String getCustomerMiddleName() {
return CustomerMiddleName;
}
public void setCustomerMiddleName(String customerMiddleName) {
CustomerMiddleName = customerMiddleName;
}
public String getCustomerPhone() {
return CustomerPhone;
}
public void setCustomerPhone(String customerPhone) {
CustomerPhone = customerPhone;
}
public String getCustomerMobilePhone() {
return CustomerMobilePhone;
}
public void setCustomerMobilePhone(String customerMobilePhone) {
CustomerMobilePhone = customerMobilePhone;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这是连接到MySQL数据库并发送值的类:
package databaseConnection;
import java.sql.*;
import accountsPackage.Accounts;
public class ConnectToDatabase {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/crochet_onlineshop";
static final String USER = "root";
static final String PASS = "root";
Accounts acc=new Accounts();
PreparedStatement ps=null;
public String addUser() throws Exception,SQLException,ClassNotFoundException{
String username=acc.getUsername();
String password=acc.getPassword();
String CustomerName=acc.getCustomerName();
String CustomerFirstName=acc.getCustomerFirstName();
String CustomerMiddleName=acc.getCustomerMiddleName();
String CustomerPhone=acc.getCustomerPhone();
String CustomerMobilePhone=acc.getCustomerMobilePhone();
String repassword=acc.getRepassword();
String email=acc.getEmail();
String address=acc.getAddress();
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sqlAcc= "INSERT INTO Customer (username, password, repassword, CustomerName, CustomerFirstName, CustomerMiddleName, CustomerMobilePhone, CustomerPhone, email, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps=conn.prepareStatement(sqlAcc);
ps.setNString(1, username);
ps.setNString(2, password);
ps.setNString(3, repassword);
ps.setNString(4, CustomerName);
ps.setNString(5, CustomerFirstName);
ps.setNString(6, CustomerMiddleName);
ps.setNString(7, CustomerMobilePhone);
ps.setNString(8, CustomerPhone);
ps.setNString(9, email);
ps.setNString(10, address);
if( ps.executeUpdate()==1)
return "success";
else
return "fail";
}
}
这是JSF页面(jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Please fill in the necessary fields in order to complete your account</h4>
<f:view>
<h:panelGrid columns="1">
<h:outputLabel value="username"></h:outputLabel>
<h:inputText value="#{accounts.username}"></h:inputText>
<h:outputLabel value="password"></h:outputLabel>
<h:inputSecret value="#{accounts.password}"></h:inputSecret>
<h:outputLabel value="re-enter password"></h:outputLabel>
<h:inputSecret value="#{accounts.repassword}"></h:inputSecret>
<h:outputLabel value="name"></h:outputLabel>
<h:inputText value="#{accounts.customerName}"></h:inputText>
<h:outputLabel value="firstname"></h:outputLabel>
<h:inputText value="#{accounts.customerFirstName}"></h:inputText>
<h:outputLabel value="middle name"></h:outputLabel>
<h:inputText value="#{accounts.customerMiddleName}"></h:inputText>
<h:outputLabel value="mobile phone"></h:outputLabel>
<h:inputText value="#{accounts.customerMobilePhone}"></h:inputText>
<h:outputLabel value="phone"></h:outputLabel>
<h:inputText value="#{accounts.customerPhone}"></h:inputText>
<h:outputLabel value="email"></h:outputLabel>
<h:inputText value="#{accounts.email }"></h:inputText>
<h:outputLabel value="address"></h:outputLabel>
<h:inputText value="#{accounts.address }"></h:inputText>
<h:form>
<h:commandButton action="#{connectToDatabase.addUser}"></h:commandButton>
</h:form>
</h:panelGrid>
</f:view>
</body>
</html>
有人可以帮我找出存储在db中的值为NULL的原因吗?