-1

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?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Flaviu
  • 1
  • 2
  • In the current code snippets, no classes are managed beans i.e. they are not managed by the JSF framework, the container or something else you may be interested in - they are merely plain Java classes. Are they managed beans in your real code? You have not enclosed input fields within an `` tag. Thus, they would be submitted, when the given `` is clicked. Please adjust the code accordingly. – Tiny Jan 18 '15 at 15:07
  • Account is a managed bean -session and ConnectTo Database is set as request – Flaviu Jan 18 '15 at 15:25
  • I did not play much with XML configurations. Therefore, I did not guess it. :) – Tiny Jan 18 '15 at 15:27
  • I have enclosed the input fields within a form,but stii it passes NULL values to the mysql database. – Flaviu Jan 18 '15 at 15:33

1 Answers1

1

Even if you are using XML, you are doing it completely wrong. You are not passing Accounts to your ManagedBean ConnectToDatabase. Instead you are creating a new Accounts() in ConnectToDatabase and adding it to database.

EDIT1:

You should add getter and setter for accounts in class ConnectToDatabase. Then use #{connectToDatabase.accounts.username}, ... for the fields.

EDIT2:

ManagedBean

public class ConnectToDatabase {
// all your code is here
// For simplicity rename your accounts
// Accounts accounts = new Accounts();
// add the following lines

    public Accounts getAccounts(){
        return accounts
    }

    public void setAccounts(accounts){
        this.accounts = accounts;
    }

}

View

// your code
<h:panelGrid columns="1">
        <h:outputLabel value="username"></h:outputLabel>
        <h:inputText value="#{connectToDatabase.accounts.username}"></h:inputText>
......
//your code
Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
  • Could you be more specific? How should I do? Thanks – Flaviu Jan 18 '15 at 15:39
  • I have seen but i just can't understand because accounts object is accessed with getters in ConnectToDatabase. Could you provide me a short code snipped? – Flaviu Jan 18 '15 at 15:52