2016-06-25 5 views
-2

を使用して、テーブルにデータを表示することはできません。私はこんにちは、私はプログラミングで初心者だと私はこのようにJSTLを使用して、データベースのデータを表示しようとしているサーブレットとJSP

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()); 
} 

Account.javaを}

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> 

Koneksi.Java

public class Koneksi { 

private static Connection conn; 

public static Connection getConn() { 
    if (conn == null) { 
     MysqlDataSource data = new MysqlDataSource(); 
     data.setDatabaseName("bimbel_online"); 
     data.setUser("root"); 
     data.setPassword(""); 

     try { 
      conn = data.getConnection(); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
    return conn; 
} 

}

しかし、そのコードはテーブルにデータを表示することはできませんが、そこに私を助けるのですか?

答えて

0

1/whereはデータベースに接続するためのコードですか?私は上記のようにKoneksi.Javaでデータベースを接続するためのコードを入れて、私が試した

DaoAccount daoAccount = new DaoAccount(); 
+0

:あなたがあなたのクラス DaoAccount daoAccount 変更をinstansiateなかったDisplayTableAkunManagerサーブレットで

2/instansiateに私は動作しませんでした – surachman

関連する問題