我无法使用servlet和jsp向表中显示数据

时间:2016-06-25 21:28:47

标签: java mysql jsp servlets jstl

你好,我是编程中的新手,我正在尝试使用JSTL显示数据库数据,如下所示:

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>

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

}

但是这些代码无法向表格显示数据,是否可以帮助我?

1 个答案:

答案 0 :(得分:0)

1 /连接数据库的代码在哪里?

2 /在您的servlet DisplayTableAkunManager中,您没有实现您的类 DaoAccount daoAccount 改为:

DaoAccount daoAccount = new DaoAccount();