i have a jsf page that update employee table with this code....
i can get the data,but i can't update it with primefaces.and when i debug it,it doesn't do anything. i need help to update the data. thanks
this is my page controller
#ManagedBean(name = "homebean")
#ViewScoped
public class HomeController implements Serializable {
private Employee employee;
private List<Employee> employees;
private EmployeeeDAO employeeeDAO;
public HomeController() {
employeeeDAO = new EmployeeDAOImpl();
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public void editEmployee() throws SQLException {
Employee e = this.getEmployee();
employeeeDAO.update(e);
}
#PostConstruct
private void getListEmployees() {
employees = employeeeDAO.employees(20, 10);
}
}
and this is my DAO
public class EmployeeDAOImpl implements EmployeeeDAO, Serializable {
private PreparedStatement ps;
private ResultSet rs;
#Override
public void add(Employee emp) {
try (Connection c = ConnectionHelper.getConnection()) {
String sql = "UPDATE EMPLOYEES"
+ " SET FIRST_NAME = ?,"
+ " LAST_NAME = ?,"
+ " EMAIL = ? WHERE EMPLOYEE_ID=?";
ps = c.prepareStatement(sql);
ps.setString(1, emp.getFirstname());
ps.setString(2, emp.getLastname());
ps.setString(3, emp.getEmail());
ps.setInt(4, emp.getEmployeeId());
ps.executeUpdate();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} catch (SQLException ex) {
Logger.getLogger(EmployeeDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void update(Employee emp) throws SQLException {
try (Connection c = ConnectionHelper.getConnection()) {
String sql = "UPDATE EMPLOYEES "
+ " SET FIRST_NAME = ?,"
+ " LAST_NAME = ?,"
+ " EMAIL = ? WHERE EMPLOYEE_ID=?";
ps = c.prepareStatement(sql);
ps.setString(1, emp.getFirstname());
ps.setString(2, emp.getLastname());
ps.setString(3, emp.getEmail());
ps.setInt(4, emp.getEmployeeId());
ps.execute();
} catch (SQLException ex) {
Logger.getLogger(EmployeeDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public List<Employee> employees(int size, int lastRow) {
List< Employee> list = null;
try (Connection c = ConnectionHelper.getConnection()) {
String sql = "SELECT ROWNUM NUM,EMP.* FROM EMPLOYEES EMP ORDER BY ROWNUM";
ps = c.prepareStatement(sql);
rs = ps.executeQuery();
list = new ArrayList<Employee>();
while (rs.next()) {
Employee e = new Employee();
e.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
e.setFirstname(rs.getString("FIRST_NAME"));
e.setLastname(rs.getString("LAST_NAME"));
e.setEmail(rs.getString("EMAIL"));
e.setHireDate(rs.getDate("hire_date"));
e.setSalary(rs.getBigDecimal("salary"));
list.add(e);
}
} catch (Exception e) {
}
return list;
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public Employee getById(String id) {
return null;
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
and this is the jsf page..
<p:dataTable id="employeetable" value="#{homebean.employees}" var="emp" rows="10" paginator="true"
selection="#{homebean.employee}">
<p:column headerText="First Name">#{emp.firstname}</p:column>
<p:column headerText="First Name">#{emp.lastname}</p:column>
<p:column headerText="First Name"><h:outputText value="#{emp.salary}">
<f:convertNumber type="currency" currencySymbol="$ "/>
</h:outputText></p:column>
<p:column headerText="First Name">#{emp.email}</p:column>
<p:column width="50">
<p:commandButton value="Edit" oncomplete="editdialog.show();"
partialSubmit="true" update="#([id$=display])">
<f:setPropertyActionListener value="#{emp}" target="#{homebean.employee}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form>
<p:dialog widgetVar="editdialog" id="edit" modal="true" appendToBody="true" resizable="false">
<h:panelGrid id="display" columns="2" cellspacing="4">
<h:outputText value="ID"/>
<p:inputText value="#{homebean.employee.employeeId}" disabled="true">
<f:convertNumber type="number"/></p:inputText>
<p:spacer/><p:spacer/>
<h:outputText value="First Name"/>
<h:outputText value="Last Name"/>
<p:inputText value="#{homebean.employee.firstname}"/>
<p:inputText value="#{homebean.employee.lastname}"/>
<h:outputText value="Email"/>
<h:outputText value="Salary"/>
<p:inputText value="#{homebean.employee.email}"/>
</h:panelGrid>
<p:separator/>
<p:commandButton value="save" action="#{homebean.editEmployee()}" partialSubmit="true"
update="#([id$=employeetable])" oncomplete="editdialog.hide();"/>
</p:dialog>
</h:form>
</ui:define>
finally i get the answer....we need to make sure that the property of Employee is correct. and have match datatype with our database...thanks all
Related
I need your help in enabling and disabling inputText based on rowSelectCheckbox and rowUnselectCheckbox if the checkbox is ticked or unticked. If it is ticked, then I need to enable the inputText otherwise it should be disabled on page load and on untick. By default the inputText is disabled on the page load. Here is the code for the jsf:
<h:form id="request">
<p:dataTable value="#{dataTableView.employeeList}" id="Employee" var="emp"
selection="#{dataTableView.selectedEmployees}" rowKey="#{emp.id}">
<p:ajax event="rowSelectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:ajax event="rowUnselectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:columnGroup type="header">
<p:row>
<p:column/>
<p:column headerText="ID"/>
<p:column headerText="Name"/>
<p:column headerText="Location"/>
<p:column headerText="Remarks"/>
</p:row>
</p:columnGroup>
<p:column selectionMode="multiple" style="width:2%;text-align:center"/>
<p:column headerText="ID">
<h:outputText value="#{emp.id}"/>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{emp.name}"/>
</p:column>
<p:column headerText="Location">
<h:outputText value="#{emp.location}"/>
</p:column>
<p:column headerText="Remarks">
<h:inputText id="inputT1" value="#{emp.remarks}" disabled="#{emp.disable}"/>
</p:column>
</p:dataTable>
</h:form>
And here is the code in the bean:
private List<Student> employeeList = new ArrayList<Student>();
private List<Student> selectedEmployees;
private boolean disable;
#PostConstruct
public void init() {
//add Employees
disable=true;
Student w1 = new Student(111, "AAAA", "ZZZZ", "", disable);
Student w2 = new Student(222, "CCCCC", "ZZZZZ", "OUT", disable);
Student w3 = new Student(222, "BBBBBB", "YYYYYYY", "IN", disable);
employeeList.add(w1);
employeeList.add(w2);
employeeList.add(w3);
}
public void EnableInputText(SelectEvent event) {
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=employeeList.size();j++){
if(selectedEmployees.get(i).getId().equals(employeeList.get(j).getId()))
{
employeeList.get(j).setDisable(false);
break;
}
}
}
}
The Student Bean:
public class Student {
private Integer id;
private String name;
private String location;
private String remarks;
private boolean disable;
public Student(Integer id, String name, String location, String remarks, boolean disable){
this.id = id;
this.name = name;
this.location = location;
this.remarks=remarks;
this.disable=disable;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getRemarks() {
return remarks;
}
public void setDisable(boolean disable) {
this.disable = disable;
}
public boolean isDisable() {
return disable;
}
And in the Bean, I am facing difficulties in enabling the inputText for entry if the row is ticked. So could you please help.
Now I got the error :
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 if I tick and checkbox
First thing you are using selectionMode="multiple" it means there will be multiple rows with textField enabled next
instead of this :
<h:inputText value="#{emp.remarks}" disabled="#{empBean.enable}" />
write
<h:inputText value="#{emp.remarks}" disabled="#{emp.enable}" />
means declare one variable enable in the bean itself after that:
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=empList.size();j++){
if(selectedEmployees.get(i).getId().equals(empList.get(j).getId()){
empList.get(j).setEnable(false);
}
}
}
before to this you can write one for loop and disable all the textField for list for that will work for rowUnselect
I'm using datatable to retrieve records from database. Datatable is showing all the items on rows as expected. What I want is how to choose only a couple or single one of them, could it be by specifying the index of the item in datatable? or by the SQL query?
bean :
public JcalendarController getSelectedUser() {
return selectedday;
}
public void setSelectedUser(JcalendarController selectedday) {
this.selectedday = selectedday;
}
List<String> user_spinner_list = new ArrayList<String>();
List<JcalendarController> calendarlist = new ArrayList<JcalendarController>();
public void delete() {
System.out.println("JadminBeans >> delete() ---------- id= ");
JcalendarDAO.deleteDay(selectedday);
}
public List<JcalendarController> getMessages() {
System.out.println("List<JcalendarController> getMessages()");
calendarlist = JcalendarDAO.getdays();
return calendarlist;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
//--------------------------------------------------------------------------
//---------------------------- user_spinner() ----------------------------//
public List<String> user_spinner() {
System.out.println("List<JcalendarBeans> user_spinner()");
user_spinner_list = JcalendarDAO.AllUsarname_spinner();
return user_spinner_list;
}
//---------------------------- ImageUpload() ----------------------------//
public void ImageUpload() {
JcalendarController CC = new JcalendarController(this.username, this.day, this.file);
System.out.println(this.username + " " + this.day + " " + this.file);
calendarlist.add(CC);
JcalendarDAO.add_image_DAO(CC);
}
//---------------------------- TextUpload() ----------------------------//
public void TextUpload() {
JcalendarController CC = new JcalendarController(this.username, this.day, this.text);
System.out.println(this.username + " " + this.day + " " + this.text);
calendarlist.add(CC);
JcalendarDAO.add_text_DAO(CC);
}
DAO :
public static List<JcalendarController> getdays() {
List<JcalendarController> ccs = new ArrayList<JcalendarController>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from calendar");
while (rs.next()) {
JcalendarController cc = new JcalendarController();
cc.setId(rs.getLong("id"));
cc.setDay(rs.getInt("day"));
cc.setText(rs.getString("text"));
cc.setUsername(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return ccs;
}
XHTML
<p:dataTable value="#{Jcalendar.messages}" var="o" paginator="true" selection="#{Jcalendar.selectedUser}"
rowKey="#{o.id}" style="margin-bottom:20px"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="24,48,144">
<f:facet name="header">
<h:outputText value="Data showing from database" />
</f:facet>
<p:column selectionMode="single" />
<p:column>
<f:facet name="header">
<h:outputText value=" Id" />
</f:facet>
<h:outputText value="#{o.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Day" />
</f:facet>
<h:outputText value="#{o.day}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Text" />
</f:facet>
<h:outputText value="#{o.text}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Username" />
</f:facet>
<h:outputText value="#{o.username}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Image" />
</f:facet>
<h:outputLink value="DisplayImage?id=#{o.id}" target="_blank">
<h:graphicImage value="DisplayImage?id=#{o.id}" width="50" height="50"></h:graphicImage>
</h:outputLink>
</p:column>
<f:facet name="footer">
<p:commandButton value="Delete" action="#{Jcalendar.delete()}" ajax="false" update=":form:msgs"/>
</f:facet>
</p:dataTable>
The datatable will show the values of the messages property, so whatever is in the list returned by the getMessages() will be rendered inside the table.
For what concerns you question, it is best to make a DAO method and return from the DB only the needed records, putting it in your words, use the SQL.
One optimization hint, you should avoid using any logic inside your getter methods as during the life-cycle of a single request, it can be called multiple times. In your case it would contact a DB upon every call. You can check this post to learn more, and find a better way to initialize your list.
Can I use radio button to select a single row then edit some of data on that row then use
commandButton to submit what I edit it in that row. I'm trying to edit username cell for now as test.
this a snap of my code:
Xadmin.xhtml
<h:form id="form" enctype="multipart/form-data">
<p:growl id="msgs" showDetail="true" />
<p:dataTable id="DT" value="#{Jadmin.messages}"
var="o"
selection="#{Jadmin.selectedUser}"
rowKey="#{o.id}"
style="margin-bottom:20px">
<f:facet name="header">
Users List
</f:facet>
<p:column selectionMode="single" />
<p:column>
<f:facet name="header">
<h:outputText value="id" />
</f:facet>
<h:outputText value="#{o.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="username" />
</f:facet>
<p:inputText value="#{o.username}" />
</p:column>
<f:facet name="footer">
<h:commandButton value="Update" action="#{Jadmin.update}" />
<p:commandButton value="Delete"
action="#{Jadmin.delete}"
ajax="false"
update=":form:msgs"/>
</f:facet>
</p:dataTable>
JadminBeans.java
#ManagedBean(name = "Jadmin")
#SessionScoped
public class JadminBeans implements Serializable {
private static final long serialVersionUID = 1L;
private JadminController selectedUser;
List<JadminController> userslist = new ArrayList<JadminController>();
public List<JadminController> getMessages() {
System.out.println("List<JadminController> getMessages()");
userslist = JadminDAO.getAllUsers();
return userslist;
}
public void delete() {
//System.out.println(usr);
//System.out.println(itemList.remove(item)+"!!");
System.out.println("JadminBeans >> delete() ---------- id= ");
JadminDAO.deleteUser(selectedUser);
}
public JadminController getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(JadminController selectedUser) {
this.selectedUser = selectedUser;
}
public void update() {
//o=(JadminBeans) objct;
JadminDAO.updateUser(selectedUser);
}
}
JadminDAO.java
public static void deleteUser(JadminController user) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("delete from users where id=?");
// Parameters start with 1
preparedStatement.setLong(1, user.getId());
preparedStatement.executeUpdate();
System.out.println("JadminDAO >> deleteUser ----------");
} catch (SQLException e) {
System.out.println("JadminDAO >> deleteUser----------- SQLException :(");
e.printStackTrace();
}
}
public static void updateUser(JadminController user) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("update users username=?, password=?, permission=? where username=?");
// Parameters start with 1
//System.out.println(new java.sql.Date(user.getRegisteredon().getTime()));
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getPermission());
//preparedStatement.setDate(3, new java.sql.Date(user.getRegisteredon().getTime()));
preparedStatement.setString(4, user.getUsername());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
JadminController.java
public class JadminController implements Serializable {
private static final long serialVersionUID = 1L;
private String username, password, permission;
private long id;
// Getters and setters.
}
If you use Editable Datatable you don't need any command button to submit what you've edited.
As shown in the same link, inside in datatable is usually used a selectOneMenu instead of radioButton for making choices.
To use input element inside your datatable don't forget to put <f:facet name="output"></f:facet> and <f:facet name="input"></f:facet>
I hope it helps.
How would it be possible to retrieve an inputText value foreach row in JSF dataTable, since this data is not an attribute in the object on which we iterate in the loop.
(here 0 as default value). I can't change the Class properties (here Product)
<h:form>
<p:dataTable var="product" value="#{bean.products}">
<p:column headerText="Id">
<h:outputText value="#{product.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{product.name}" />
</p:column>
<p:column headerText="Quantity">
<h:inputText size="3" value="0" />
</p:column>
</p:dataTable>
<h:commandButton value="Submit" action="#{bean.submit}"/>
</h:form>
Bean.class
#ManagedBean
...
public void submit() {
List<Product> products = this.getProducts();
for(Product product : list) {
System.out.println("Product.name : "+ Product.name );
System.out.println("Quantity : "+ ?? );
}
}
Bind it to a Map with Product (or its ID) as key.
E.g.
private List<Product> products;
private Map<Product, Long> quantities;
#EJB
private ProductService productService;
#PostConstruct
public void init() {
products = productService.list();
quantities = new HashMap<>();
}
public void submit() {
for (Product product : products) {
Long quantity = quantities.get(product);
System.out.println("Quantity: " + quantity);
}
}
// Getters (no setters necessary for those two properties)
with
<h:inputText size="3" value="#{bean.quantities[product]}" />
You can use the visitor pattern to get the values. Something like this:
table.visitTree(VisitContext.createVisitContext(faces),
new VisitCallback() {
#Override public VisitResult visit(VisitContext vc, UIComponent component) {
if (component.equals(quantity) && quantity.getValue() != null) {
cart.addItem(
(Product) table.getRowData(),
(Integer) quantity.getValue());
}
return VisitResult.ACCEPT;
}
});
I m trying to display data in jsf datable data from the database,ans in a same row i m displaying link for Edit. Now when user will clicked on edit button then it should be inputText from outputText. Here i have done coding for that but i can't change the textbox can u please help me? Thanks in advance.
ShowData.xhtml
<h:dataTable value="#{customerdata.customerList}" var="c"
styleClass="order-table" binding="#{customerdata.dataTable}"
headerClass="order-table-header" border="1" width="100%"
rowClasses="order-table-odd-row,order-table-even-row" rows="3">
<h:column>
<h:selectBooleanCheckbox></h:selectBooleanCheckbox>
</h:column>
<h:column>
<f:facet name="heder">User ID</f:facet>
<h:inputText value="#{c.cust_id}" size="10" rendered="#{c.editable}"/>
<h:outputLabel value="#{c.cust_id}" rendered="#{not c.editable}" />
</h:column>
<h:column>
<f:facet name="heder">User Name</f:facet>
<h:inputText value="#{c.cust_name}" size="10" rendered="#{c.editable}"/>
<h:outputLabel value="#{c.cust_name}" rendered="#{not c.editable}" />
</h:column>
<h:column>
<h:commandLink value="Update" rendered="#{c.editable}" action="#{customerdata.editAction(c)}" />
<h:commandLink value="Edit" action="#{customerdata.editAction(c)}" rendered="#{not c.editable}"/>
</h:column>
<h:column>
<h:commandLink value="Delete" action="#{customerdata.deleteAction(c)}" />
</h:column>
<!-- Footer Setting -->
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="prev" action="#{customerdata.pagePrevious}"
disabled="#{customerdata.dataTable.first == 0}" />
<h:commandButton value="next" action="#{customerdata.pageNext}"
disabled="#{customerdata.dataTable.first + customerdata.dataTable.rows
>= customerdata.dataTable.rowCount}" />
</h:panelGroup>
</f:facet>
</h:dataTable>
CustomerData.java
package model;
#ManagedBean(name="customerdata")
public class CustomerData implements Serializable {
private static final long serialVersionUID = 1L;
Connection con;
Statement smt;
HtmlDataTable dataTable;
//Customer cust=new Customer();
public List<Customer> getcustomerList() throws SQLException{
//System.out.println("in getcustomerlist");
List<Customer> list= new ArrayList<Customer>();
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/openid","root","root");
smt = con.createStatement();
String query="select * from jsftable";
ResultSet rs= smt.executeQuery(query);
while(rs.next()){
Customer cust = new Customer();
cust.setCust_id(rs.getInt("cust_id"));
cust.setCust_name(rs.getString("cust_name"));
cust.setHas_attachment(rs.getBoolean("has_attachment"));
System.out.println("in cusotomer data"+cust.isEditable());
//store all data into a List
list.add(cust);
}
}
catch(Exception e){
e.printStackTrace();
}
return list;
}
public void pageFirst() {
dataTable.setFirst(0);
}
public void pagePrevious() {
dataTable.setFirst(dataTable.getFirst() - dataTable.getRows());
System.out.println("Prevoius"+dataTable.getFirst());
}
public void pageNext() {
dataTable.setFirst(dataTable.getFirst() + dataTable.getRows());
System.out.println("Next"+dataTable.getFirst());
}
public void pageLast() {
int count = dataTable.getRowCount();
int rows = dataTable.getRows();
dataTable.setFirst(count - ((count % rows != 0) ? count % rows : rows));
}
public HtmlDataTable getdataTable() {
return dataTable;
}
public void setdataTable(HtmlDataTable dataTable) {
this.dataTable = dataTable;
}
public void showRow(){
System.out.println(dataTable.getRows());
}
public String deleteAction(Customer customer) throws SQLException{
//list.remove(customer);
//System.out.println(customer.cust_id);
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/openid","root","root");
smt = con.createStatement();
String query="delete from jsftable where cust_id="+customer.cust_id+"";
//ResultSet rs= smt.executeQuery(query);
smt.executeUpdate(query);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
public String editAction(Customer customer) {
//list.remove(customer);
System.out.println(customer.cust_id);
customer.setEditable(true);
return null;
}
}
Customer.java
public class Customer {
int cust_id;
String cust_name;
boolean has_attachment;
boolean editable;
String edit_value;
public String getEdit_value() {
System.out.println("in getter");
return edit_value;
}
public void setEdit_value(String editvalue) {
this.edit_value = editvalue;
}
public boolean isHas_attachment() {
System.out.println("in getter of has_attach");
return has_attachment;
}
public void setHas_attachment(boolean hasAttachment) {
has_attachment = hasAttachment;
}
public int getCust_id() {
System.out.println("in getter of cust_id");
return cust_id;
}
public void setCust_id(int custId) {
cust_id = custId;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String custName) {
cust_name = custName;
}
public boolean isEditable() {
//System.out.println("in isEditable"+editable);
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
//System.out.println("in set editable"+editable);
}
}
To be honest, I wonder how your code works at all. It has several major flaws.
You should start with
giving your managed bean a scope, the #ViewScoped seems the most appropriate (see here)
removing the database access from the getter method. Put it inside an #PostConstruct annotated method in your bean. Getter methods can be called several times during JSF lifecycle. The #PostConstruct method only after bean construction.
closing sql statement and connection when you are done (stmt.close() and con.close())
following bean field and method naming conventions: A private field xxx has a getter getXxx and a setter setXxx (the capitalization is important)
removing datatable binding. It is not necessary here.
I recommend to go through this simple CRUD example by BalusC and adapting it for your functional requirements.
Add id tag for datatable:
<h:dataTable value="#{customerdata.customerList}" var="c" id="customerDT"
styleClass="order-table" binding="#{customerdata.dataTable}"
headerClass="order-table-header" border="1" width="100%"
rowClasses="order-table-odd-row,order-table-even-row" rows="3">
Add in your edit CommandButton update tag:
But why you don't use the inplace component using primefaces? http://www.primefaces.org/showcase-labs/ui/inplace.jsf (need a save button)