I am new to java ee and dont have much idea about jsf and all. I am making a simple java web application that fetches data from database and shows in dataTable. I need to edit data selected by user from dataTable for which i need to get value of the row selected/clicked. But i havent been able to do it. Can any one please help me with my code ? I hope someone would tell me how can i do it with my following codes.
showRecords.xhtml
h:dataTable value="#{studentList.studentL()}" var="student" styleClass="studentTable"
columnClasses=",,,fixedWidth">
<h:column>
<f:facet name="header">Student ID</f:facet>
<h:outputText value="#{student.studentId}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{student.fname}"></h:outputText>
</h:column>
Student.java
@ManagedBean(name="student")
public class student {
@Id private String StudentId;
private String Fname, Lname, Mname="noname";
/*******getters and setters** and database transaction****/
}
studentList.java
@ManagedBean(name="studentList")
@SessionScoped
public class studentList {
public List<student> studentL(){
List<student> list = new ArrayList<student>();
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/tourManager","administrator","pass");
String sql = "Select * from student";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
student student1 = new student();
student1.setFname(rs.getString("FNAME"));
student1.setLname(rs.getString("LNAME"));
student1.setStudentId(rs.getString("STUDENTID"));
list.add(student1);
// return list;
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
public void editStudent() throws IOException{
int index = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("index").toString());
System.out.println(" the selected row is "+index);
}
}