我试图在 postgres 中显示一个带有“日期”列的表格。我想这与迄今为止解析字符串有关。我尝试使用简单的日期格式,但没有成功。
我实际上之前发布了类似的问题并尝试了给出的建议,但我仍然没有成功。如果有人可以使用下面的代码给我一个示例,我将不胜感激。
我无法解析字符串并显示数据......下面是我的代码。
我的DAO
package source;
import java.sql.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
public class parseDAO {
private Database database;
public parseDAO(Database database) {
this.database = database;
}
public List<parseBean> results() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
String querystring = "select * from dateparse";
List<parseBean> result_array = new ArrayList<parseBean>();
try {
connection = database.getConnection();
statement = connection.prepareStatement(querystring);
resultSet = statement.executeQuery();
while (resultSet.next()) {
parseBean resultsarray = new parseBean();
resultsarray.setDate(resultSet.getDate("date"));
resultsarray.setName(resultSet.getString("name"));
resultsarray.setAddress(resultSet.getString("address"));
result_array.add(resultsarray);
}
} finally {
try { resultSet.close(); } catch (SQLException logOrIgnore) {}
try { statement.close(); } catch (SQLException logOrIgnore) {}
try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return result_array;
}
}
我的Servlet
package source;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class parseServlet extends HttpServlet {
private parseDAO parseDAO;
@Override
public void init() throws ServletException {
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost/myDB";
String username = "postgres";
String password = "secret";
Database database = new Database(driver, url, username, password);
this.parseDAO = new parseDAO(database);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<parseBean> result_array = parseDAO.results();
request.setAttribute("result_array", result_array);
request.getRequestDispatcher("results.jsp").forward(request, response);
}
catch (SQLException e) {
throw new ServletException("Cannot retrieve areas", e);
}
}
}