Reputation: 226
I am learning jsf through developing crud App and have been totally lost in a silly problem, that is I cant add data into database, while my sql command and database both are correct. with my meager knowledge what i assume is my xhtml page approach different object to set data and jdbc class uses different to get. Please some one help.
Following is my index.xhtml code which shows all data of student database:
<h:head>
<title>MY CRUD Application</title>
</h:head>
<h:body>
<center>
<h:form>
<h:dataTable var="list" value="#{dBQueries.allStudents}" cellpadding="2" cellspacing="2" border="1">
<h:column>
<f:facet name="header">
Student Id
</f:facet>
<h:inputText value="#{list.id}"/>
</h:column>
<h:column>
<f:facet name="header">
Student Name
</f:facet>
<h:inputText value="#{list.name}"/>
</h:column>
</h:dataTable>
<h:commandLink value="Add New Student" action="add"/>
</h:form>
</center>
</h:body>
</html>
and below is my add.xhtml page
<h:head>
<title>Add New Student</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h:panelGrid border="1" cellpadding="20" cellspacing="20" columns="3">
<h:outputLabel value="student Id" />
<h:inputText value="#{studentManagedBean.id}" required="true" requiredMessage="Student id is Required" id="eId">
</h:inputText><h:message for="eId"/>
<h:outputLabel value="student Name" />
<h:inputText value="#{studentManagedBean.name}" required="true" requiredMessage="Student Name is Required" id="eName">
</h:inputText><h:message for="eName"/>
<h:commandLink value="saveAdd" action="index" actionListener="#{dBQueries.add()}" onclick="confirm('Are you Sure')" />
</h:panelGrid>
</h:form>
</f:view>
</h:body>
Below is my java Managed Bean Class
@ManagedBean
@RequestScoped
public class StudentManagedBean {
private int id;
private String name;
public StudentManagedBean() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
my last java class for DBQueries performs two methods, 1) get all students data and 2) add new student data. the code is as below:
@ManagedBean
@RequestScoped
public class DBQueries {
String query = null;
public PreparedStatement ps = null;
public ResultSet resultSet = null;
StudentManagedBean bean = new StudentManagedBean();
DBConnectivity connectivity = new DBConnectivity();
public DBQueries() {
}
public ArrayList<StudentManagedBean> getAllStudents() {
ArrayList<StudentManagedBean> arrayList = new ArrayList();
Connection con = connectivity.connect();
query = "SELECT id, name FROM student_record";
try {
ps = con.prepareStatement(query);
resultSet = ps.executeQuery();
while (resultSet.next()) {
bean.setId(resultSet.getInt("id"));
bean.setName(resultSet.getString("name"));
arrayList.add(bean);
}
} catch (SQLException ex) {
Logger.getLogger(DBQueries.class.getName()).log(Level.SEVERE, null, ex);
} finally {
connectivity.closeConnection(con, resultSet, ps);
}
return arrayList;
}
public void add() {
Connection con1 = connectivity.connect();
query = "INSERT INTO student_record(id, name) VALUES(?, ?)";
try {
ps = con1.prepareStatement(query);
ps.setInt(1, bean.getId());
ps.setString(2, bean.getName());
ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(DBQueries.class.getName()).log(Level.SEVERE, null, ex);
} finally {
connectivity.closeConnection(con1, resultSet, ps);
}
}
}
when i click add new student and give student id and name on add.xhtml it doesnt saves the data into database, neither getAllStudent() called in index.xhtml shows that data. Where am i wrong? please help
Upvotes: 0
Views: 227
Reputation: 1440
The Student instance you're trying to insert into the database is empty because the StudentManagedBean
is defined in DBQueries
as a classic java class, not as an other managed bean, that's why the methods bean.getId()
and bean.getName()
return nothing.
I assume the problem consists that the DBQueries
couldn't recognize the other managed bean you're using through the view. To do so, you should inject it in the bean that needs its services, add this annotation just before the Student bean class in DBQueries
bean.
@ManagedProperty
StudentManagedBean bean = new StudentManagedBean();
Also, for injection without problems, you should render the Student bean @SessionScoped
.
Upvotes: 2