This question already has an answer here:
hello i'm newbie in programming and I am trying to display database data using JSTL like this :
Account.java
public class Account implements Comparable<Account>{
private String accountID;
private String name;
private String username;
private String password;
private String email;
private String alamat;
private String telp;
private Timestamp dateCreated;
private Type type;
public Account() {
}
public String getAccountID() {
return accountID;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getTelp() {
return telp;
}
public void setTelp(String telp) {
this.telp = telp;
}
public Timestamp getDateCreated() {
return dateCreated;
}
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
@Override
public int compareTo(Account o) {
return accountID.compareToIgnoreCase(o.getAccountID());
}
}
DaoAccount.java
public class DaoAccount implements ImplementDao.ImplementAccount {
List<Account> listAccounts;
Connection koneksi;
String insert = "insert into Account(nama,username,password,email,alamat,telp,typeNo) "+
"values(?,?,?,?,?,?,?)";
String update = "update Account set nama = ?, username = ?, password = ?, email = ?,"+
"alamat = ?, telp = ?, typeNo = ? where accountID = ?";
String delete = "delete from Account where accountID = ?";
String getAll = "select a.*, t.* from Account a inner join Type t on a.typeNo = t.typeNo ";
String getName = "select a.*, t.* from Account a inner join Type t on a.typeNo = t.typeNo "+
"where a.nama like ?";
String getType = "select a.*, t.* from Account a inner join Type t on a.typeNo = t.typeNo "+
"where a.typeNo like ?";
public DaoAccount() {
koneksi = Koneksi.getConn();
}
@Override
public List<Account> getall() {
List<Account> list = null;
try{
list = new ArrayList<>();
Statement statement = koneksi.createStatement();
ResultSet rs = statement.executeQuery(getAll);
while(rs.next()){
Account account = new Account();
Type type = new Type();
account.setAccountID(rs.getString("accountID"));
account.setName(rs.getString("nama"));
account.setUsername(rs.getString("username"));
account.setPassword(rs.getString("password"));
account.setEmail(rs.getString("email"));
account.setTelp(rs.getString("telp"));
account.setAlamat(rs.getString("alamat"));
account.setDateCreated(rs.getTimestamp("date_craeated"));
type.setTypeNo(rs.getString("typeNo"));
type.setTypeName(rs.getString("typeName"));
account.setType(type);
list.add(account);
}
}catch(SQLException e){
e.printStackTrace();
}
return list;
}
DisplayTableManager.java
public class DisplayTableAkunManager extends HttpServlet {
List<Account> accounts;
DaoAccount daoAccount;
RequestDispatcher dispatcher;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
accounts = daoAccount.getall();
Collections.sort(accounts);
req.setAttribute("listAccounts", accounts);
dispatcher = req.getRequestDispatcher("/Manage_Akun.jsp");
dispatcher.forward(req, resp);
}
}
Manage_Akun.jsp
<tbody>
<c:forEach items="${listAccounts}" var="list">
<tr>
<td>${list.name}</td>
<td>${list.username}</td>
<td>${list.password}</td>
<td>${list.email}</td>
<td>${list.alamat}</td>
<td>${list.telp}</td>
<td>${list.type.typeName}</td>
</tr>
</c:forEach>
but that's codes can't display data to table, is there to help me ?
Aucun commentaire:
Enregistrer un commentaire