Queria uma ajuda, a horas estou tentando ver uma solução e não conseguir.
<html>
<body>
<table>
<%
ContactDao dao = new ContactDao();
List<Contact> contacts = dao.getContacts();
for (Contact contact : contacts) {
%>
<tr>
<td><%=contact.getName() %></td>
<td><%=contact.getEmail() %></td>
<td><%=contact.getAddress() %></td>
<td><%=contact.getBirthDate().getTime() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
Classe ContactDao
public class ContactDao {
private Connection connection;
public ContactDao() {
this.connection = new ConnectionFactory().getConnection();
}
public List<Contact> getContacts() {
try {
List<Contact> contactList = new ArrayList<>();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM contacts");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Contact contact = new Contact();
contact.setName(resultSet.getString("firstName"));
contact.setEmail(resultSet.getString("email"));
contact.setAddress(resultSet.getString("address"));
Calendar date = Calendar.getInstance();
date.setTime(resultSet.getDate("birthDate"));
contact.setBirthDate(date);
contactList.add(contact);
}
resultSet.close();
statement.close();
return contactList;
} catch (SQLException e) {
throw new DaoException();
}
}
public void add(Contact contact) {
try {
PreparedStatement add = connection.prepareStatement(
"INSERT INTO contacts (firstName, email, address, birthDate) VALUES (?, ?, ?, ?)");
add.setString(1, contact.getName());
add.setString(2, contact.getEmail());
add.setString(3, contact.getAddress());
add.setDate(4, new Date(contact.getBirthDate().getTimeInMillis()));
add.executeUpdate();
add.close();
} catch (SQLException e) {
throw new DaoException();
}
}
public void update(Contact contact) {
try {
PreparedStatement update = connection.prepareStatement(
"UPDATE contacts SET firstName = ?, email = ?, address = ?, birthDate = ? WHERE id = ?");
update.setString(1, contact.getName());
update.setString(2, contact.getEmail());
update.setString(3, contact.getAddress());
update.setDate(4, new Date(contact.getBirthDate().getTimeInMillis()));
update.setLong(5, contact.getId());
update.executeUpdate();
update.close();
} catch (SQLException e) {
throw new DaoException(e.getMessage());
}
}
public void delete(Contact contact) {
try {
PreparedStatement delete = connection.prepareStatement("DELETE FROM contacts WHERE id = ?");
delete.setLong(1, contact.getId());
delete.executeUpdate();
delete.close();
} catch (SQLException e) {
throw new DaoException();
}
}
}
Root
java.lang.RuntimeException
jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:12)
jdbc.dao.ContactDao.<init>(ContactDao.java:16)
org.apache.jsp.list_002dcontacts_002dscriptle_jsp._jspService(list_002dcontacts_002dscriptle_jsp.java:124)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.jasper.JasperException: An exception occurred processing [/list-contacts-scriptle.jsp] at line [9]
6: <body>
7: <table>
8: <%
9: ContactDao dao = new ContactDao();
10: List<Contact> contacts = dao.getContacts();
11:
12: for (Contact contact : contacts) {
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)