Monday, May 07, 2007

Simple JSF

Here my snippet that working successfully...
The page is where user can view ,add,delete and edit the data in the same page.

My Managed bean
public class MyBean {

private List myDataList;
private HtmlDataTable myDataTable;//bind
private MyData myDataItem=new MyData();
private boolean editMode=false;
private String status="";
private MyDataAccess da=new MyDataAccess();
/** Creates a new instance of MyBean */

public MyBean() {
loadMyDataList();
}
public void loadMyDataList(){
//my dao thing
try {
myDataList=da.retrieveNationalityList();
} catch (Exception ex) {
System.out.println("Something error while loadMyDataList...");
ex.printStackTrace();
}

}
public List getMyDataList() {
if(FacesContext.getCurrentInstance().getRenderResponse()){
loadMyDataList();
}
return myDataList;
}

public void setMyDataList(List myDataList) {
this.myDataList = myDataList;
}

public String deleteMyData(){
//get selected MyData
setMyDataItem((MyData)getMyDataTable().getRowData());
status=da.deleteNationality( getMyDataItem().getId());
setMyDataItem(null);//clean textfield
System.out.println("Str inside delete-->"+ status);
return "delete"; //navigation case
}

public HtmlDataTable getMyDataTable() {
return myDataTable;
}

public void setMyDataTable(HtmlDataTable myDataTable) {
this.myDataTable = myDataTable;
}

//addition
public String insertData(){
try {
MyDataAccess da=new MyDataAccess();
System.out.println("Tgk jap date ko tulih camnaa? --->" + getMyDataItem().getSetupDate());
status=da.insertData(getMyDataItem());

} catch (Exception ex) {
ex.printStackTrace();
}
if(status=="success"){
System.out.println("status--->"+ status);
setMyDataItem(null);
return "success";
}
else{
return "fail";
}
}

public MyData getMyDataItem() {
return myDataItem;
}

public void setMyDataItem(MyData myDataItem) {
this.myDataItem = myDataItem;
}

public String editData(){
editMode=true;
System.out.println("editMode --> "+ editMode);
setMyDataItem((MyData)getMyDataTable().getRowData());
return "edit";
}

public String saveData(){ //in edit mode
System.out.println("editMode---> "+ editMode);
status=da.editNationality(getMyDataItem());
setMyDataItem(null);//clean textfield
System.out.println("status inside saveData-->"+ status);
return "save";
}
}
My data object
public class MyData {
private String id;
private String descr;
private String setupDate;
private String changeDate;

/** Creates a new instance of MyData */
public MyData() {
}

//setter
public void setId(String id) {
this.id = id;
}

public void setChangeDate(String changeDate) {
this.changeDate = changeDate;
}

public void setSetupDate(String setupDate) {
// setupDate.toString();
this.setupDate = setupDate;
}

public void setDescr(String descr) {
this.descr = descr;
}

//getter
public String getDescr() {
return descr;
}

public String getId() {
return id;
}
public String getSetupDate() {
return setupDate;
}

public String getChangeDate() {
return changeDate;
}

}
My data access

public class MyDataAccess {
private String str="";
Connection con = null;
PreparedStatement ps = null;
/** Creates a new instance of MyDataAccess */
public MyDataAccess() {
}

public List retrieveNationalityList() throws Exception{
ArrayList ls=new ArrayList();
ResultSet rsReturn = null;

try {
con = DBManager.getDBConnection();
String sql="select id,descr,to_char(setup_date,'MM/dd/yyyy, hh:mm:ss'),to_char(change_date,'MM/dd/yyyy, hh:mm:ss') from nationality order by id asc" ;
System.out.println("sql-->"+ sql);
ps = con.prepareStatement(sql);
System.out.println("Nilai ps---->"+ ps);
rsReturn = ps.executeQuery();
System.out.println("rsReturn-->"+rsReturn);
while(rsReturn.next()){
MyData n=new MyData();
n.setId(rsReturn.getString(1));
System.out.println("rsReturn.getString(1)"+ rsReturn.getString(1));
n.setDescr(rsReturn.getString(2));
System.out.println("rsReturn.getString(2)"+ rsReturn.getString(2));
n.setSetupDate(rsReturn.getString(3));
n.setChangeDate(rsReturn.getString(4));
System.out.println("rsReturn.getString(4)"+ rsReturn.getString(4));
ls.add(n);
}
System.out.println("list size---->"+ ls.size());
} catch(SQLException sqlex) {
System.out.println("Inside sqlexception ...");
sqlex.printStackTrace();
} finally {
con.close();
ps.close();
}

return ls;
}


public String deleteNationality(String id){
try {
try {
con = DBManager.getDBConnection();
} catch (Exception ex) {
System.out.println("Inside deleteNationality ");
ex.printStackTrace();
}
int count=0;

String sql="delete nationality where id='"+id+"'";
System.out.println("sql-->"+ sql);
ps = con.prepareStatement(sql);
System.out.println("Nilai ps---->"+ ps);
count= ps.executeUpdate();
if(count<0 data-blogger-escaped-str="success">"+str);

} catch(SQLException sqlex) {
sqlex.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return str;
}

//for addition
public String insertData(MyData nationality) throws Exception {
try {
con = DBManager.getDBConnection();
int count=0;

String sql="insert into nationality(id,descr,setup_date,change_date) values('"+nationality.getId()+
"','"+nationality.getDescr()+"',current_timestamp,current_timestamp)";
System.out.println("sql--&gt;"+ sql);
ps = con.prepareStatement(sql);
System.out.println("Nilai ps----&gt;"+ ps);
count= ps.executeUpdate();
if(count<0 data-blogger-escaped-str="success">"+str);

} catch(SQLException sqlex) {
System.out.println("Inside sqlexception ...");
sqlex.printStackTrace();
} finally {
con.close();
ps.close();
}
return str;
}

public String editNationality(MyData data){
try {
try {
con = DBManager.getDBConnection();
} catch (Exception ex) {
System.out.println("Inside editNationality ");
ex.printStackTrace();
}
int count=0;
String sql="UPDATE nationality SET descr='"+data.getDescr()
+"',change_date=current_timestamp WHERE id ='"+data.getId()+"'";
System.out.println("sql--&gt;"+ sql);
ps = con.prepareStatement(sql);
System.out.println("Nilai ps----&gt;"+ ps);
count= ps.executeUpdate();
if(count<0 data-blogger-escaped-str="success">"+str);

} catch(SQLException sqlex) {
sqlex.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return str;
}
}