一共需要四部分:1.model(一些属性,以及get put方法),例如,News;2数据库操作(主要是操作数据库的方法,该方法会在jsp页面调用),例如DataNews;3数据库的链接,例如DataBaseConn;4.jsp页面,调用数据库的数据,显示在页面上。
注:三个java文件全部写在notice包中,代码上没有写出来。但jsp中调用这些时会需要写出包名。
具体:
1.News.Java
publicclass News {
privateintnewsItemID;
privateintsectionID;
private StringnewsTitle;
private StringnewsPublisher;
private java.sql.DatenewsDateTime;
private StringnewsDepartment;
private StringnewsContent;
publicint getNewsItemID() {
returnnewsItemID;
}
publicvoid setNewsItemID(int newsItemID) {
this.newsItemID = newsItemID;
}
publicint getSectionID() {
returnsectionID;
}
publicvoid setSectionID(int sectionID) {
this.sectionID = sectionID;
}
public String getNewsTitle() {
returnnewsTitle;
}
publicvoid setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public String getNewsPublisher() {
returnnewsPublisher;
}
publicvoid setNewsPublisher(String newsPublisher) {
this.newsPublisher = newsPublisher;
}
public String getNewsDepartment() {
returnnewsDepartment;
}
publicvoid setNewsDepartment(String newsDepartment) {
this.newsDepartment = newsDepartment;
}
public String getNewsContent() {
returnnewsContent;
}
publicvoid setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
public java.sql.Date getNewsDateTime() {
returnnewsDateTime;
}
publicvoid setNewsDateTime(java.sql.Date newsDateTime) {
this.newsDateTime = newsDateTime;
}
}
2.DataNews.java
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/*数据库名称为news*/
publicclass DataNews {
DataBaseConn dbc = new DataBaseConn();
/*该方法是根据sectionID从数据库里读出8条最新的数据,放在List<News>里*/
public List<News> selectEightNews(int sectionID) {
List<News> eightNews = new ArrayList<News>();
News news = null;
String sql = "select * from news where sectionID='" + sectionID +"'"
+ "order by newsItemID desc limit 8";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
news =new News();
news.setNewsItemID(rs.getInt(1));
news.setSectionID(rs.getInt(2));
news.setNewsTitle(rs.getString(3));
news.setNewsPublisher(rs.getString(4));
news.setNewsDateTime(rs.getDate(5));
news.setNewsDepartment(rs.getString(6));
news.setNewsContent(rs.getString(7));
eightNews.add(news);
}
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
} finally {
dbc.closeConn();
}
return eightNews;
}
/*该方法是根据sectionID和newsItemID从数据库里读出一条信息,放在news里,该文中的jsp未调用该方法*/
public News selectSingleNews(int sectionID,int newsItemID) {
News news = null;
String sql = "select * from news where sectionID='" + sectionID +"'"
+ "and newsItemID='" + newsItemID + "'"
+ "order by newsItemID desc";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
news = new News();
news.setNewsItemID(rs.getInt(1));
news.setSectionID(rs.getInt(2));
news.setNewsTitle(rs.getString(3));
news.setNewsPublisher(rs.getString(4));
news.setNewsDateTime(rs.getDate(5));
news.setNewsDepartment(rs.getString(6));
news.setNewsContent(rs.getString(7));
}
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
} finally {
dbc.closeConn();
}
return news;
}
}
3.DataBaseConn.java
import java.sql.*;
/*数据库为mysql*/
publicclass DataBaseConn {
public Connection conn =null;
publicvoid createConn(){
String DRIVER = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://172.22.144.99:3306/freetradeport?user=root&password=4023&useUnicode=true&characterEncoding=gb2312";
if(conn ==null){
try{
Class.forName(DRIVER).newInstance();
conn = DriverManager.getConnection(url);
}catch(Exception e){
e.printStackTrace();
}
}
}
public Connection getConn(){
if(conn ==null){
createConn();
}
returnconn;
}
public void closeConn(){
if(conn !=null){
try {
conn .close();
conn =null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
if (conn ==null) {
createConn();
}
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
publicvoid executeUpdate(String sql) {
if (conn ==null) {
createConn();
}
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4 index.jsp
<%@ pagelanguage="java"import="java.util.*"%>
<%@ pageimport="notice.DataBaseConn"%>
<%@ pageimport="notice.News"%>
<%@ pagecontentType="text/html;charset=UTF-8"language="java"%>
<jsp:useBeanid="ne"class="notice.DataNewsscope="page"></jsp:useBean>
<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<scriptsrc="JSClass/FusionCharts.js">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description"content="This is my page">
<!--
<link rel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<!—使用了样式list-->
<div class="list">
<%
List<News> latest_news =new ArrayList<News>();
latest_news = ne.selectEightNews(1);
Iterator<News> iter_latest_news = latest_news.iterator();
while (iter_latest_news.hasNext()) {
News news_latest = (News) iter_latest_news.next();
int sectionID = news_latest.getSectionID();
int newsItemID = news_latest.getNewsItemID();
String newsTitle = news_latest.getNewsTitle();
String newsDate = news_latest.getNewsDateTime().toString();
%>
<ul>
<li>
<!—可根据sectionID和newsItemID进行跳转到下一张页面-->
<a
href="List.jsp?sectionID=<%=sectionID%>&newsItemID=<%=newsItemID%>"><%=newsTitle%></a>
<span><%=newsDate%></span>
</li>
</ul>
<%
}
%>
</div>
</body>
</html>