So I have a JSF page which contains a panelgrid with <h:inputtext>
and one <h:commandbutton>
. When I press the button I want the data that is introduced in the inputtexts to be stored in the database. The action is performed,the problem is that the values stored in the database are null.
This is the the Bean :
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;
}
}
This is the class which connects to MySQL database and sends the values:
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";
}
}
And this is the JSF page(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>
Can someone help me to find out why the values stored in the db are NULL?