Title.java
package cn.ac.radi.example;
import java.sql.Date;
public class Title {
private int id;
private Date createTime;
private int rainfall;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getRainfall() {
return rainfall;
}
public void setRainfall(int rainfall) {
this.rainfall = rainfall;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Title(int id,Date createTime,int rainfall) {
super();
this.id = id;
this.createTime = createTime;
this.rainfall = rainfall;
}
}
------------------------------------------------------------------------------------------------------------------------------------
TitleDAO.java
package cn.ac.radi.example;
import cn.ac.radi.example.Title;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TitleDAO {
public List<Title> readFirstTitle(){
//调用list接口下的ArrayList类,再此需要使用泛型,因为当数据存入集合中,类型默认转换为object类型所以加入泛型,就不用后续进行强制转换了
List<Title> list =new ArrayList<Title>();
Connection con=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:postgresql://localhost:5432/myDB","postgres","postgres");
String sql="select * from rainfall2013";
psmt=con.prepareStatement(sql);
rs=psmt.executeQuery();
while(rs.next())
{
int id=rs.getInt("id");
Date createTime=rs.getDate("date");
int rainfall=rs.getInt("降雨量");
//通过创建Title实体类的对象,把行数据依次存入,之后再把行数据逐次的方法list集合中
Title tl=new Title(id, createTime,rainfall);
list.add(tl);
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
try {
if(rs!=null)
{
rs.close();
}
if(psmt!=null)
{
psmt.close();
}
if(con!=null)
{
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
------------------------------------------------------------------------------------------------------------------------------
titleList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%-- <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> --%>
<%-- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> --%>
<%@page import="cn.ac.radi.example.TitleDAO,cn.ac.radi.example.Title"%>
<%@ page import="java.util.List"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test-newsTitle</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
<td>Date</td>
<td>降雨量(mm)</td>
<td>操作</td>
</tr>
<%
TitleDAO dao=new TitleDAO();
List<Title> list =dao.readFirstTitle();
for(Title tl:list)
{%>
<tr>
<td><%=tl.getId() %></td>
<td><%=tl.getCreateTime() %></td>
<td><%=tl.getRainfall() %></td>
<td><a href=delete.jsp id="<%=tl.getId() %>">删除</a></td>
</tr>
<%}
%>
</table>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------
相关分类