不少JSP初學者在學會簡單的jsp編程後
這個簡單的教程希望能夠有助於初學者學會用oop思想進行jsp編程
場景
數據庫系統用的是Mysql
先看第一個數據表
create table news
userid int
kwid int
title varchar(
content text
hits int
cdate varchar
mdate varchar
primary key(newsid));
再插入一個樣本數據
insert into news
設計思路
並通過NewsDAO
設計階段
NewsDAO的主要方法有
News
package news;
public class News {
private int newsid;
private int userid;
private int kwid;
private int hits;
private String title;
private String content;
private String cdate;
private String mdate;
public News(){ }
public News(int newsid
{
this
this
this
this
this
this
this
}
public News(int id
this
this
this
}
public int getNewsid()
{
return newsid;
}
public void setNewsid(int newsid)
{
this
}
public int getUserid()
{
return userid;
}
public void setUserid(int userid)
{
this
}
public int getKwid()
{
return kwid;
}
public void setKwid(int kwid)
{
this
}
public int getHits()
{
return hits;
}
public void setHits(int hits)
{
this
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this
}
public String getCdate()
{
return cdate;
}
public void setCdate(String cdate)
{
this
}
}
說明
最主要的文件NewsDAO
package news;
import java
public class NewsDAO
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url="jdbc:mysql://localhost:
public NewsDAO()
{
try {
Class
}
catch (java
System
}
}
public News getNewsByPrimaryKey(int newsid) throws SQLException
{
Connection conn=null;
Statement stmt;
ResultSet rs;
News news = null;
String sql="select newsid
" where newsid="+newsid+"";
conn = getConnection();
stmt = conn
rs=stmt
if(rs
{
news = new News(rs
}
rs
stmt
conn
return news;
}
private Connection getConnection() throws SQLException
{
Connection conn = null;
conn = DriverManager
return conn;
}
}
說明
如getRecentNews()等
簡單的jsp調用測試程序
<%@page contentType="text/html;charset=gb
<%@page import="news
<%
NewsDAO newsDao = new NewsDAO();
News news = newsDao
if(news != null) {
out
out
out
}
else out
%>
說明
有時間的話
還有
如果系統提示找不到News
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20088.html