不少jsp初學者在學會簡單的jsp編程后,往往停留在用jsp里面的sql語句調一個javabean進行數據庫連接階段,止步不前了。
這個簡單的教程希望能夠有助于初學者學會用oop思想進行jsp編程。
場景:一個簡單的新聞系統,有2-3個數據表構成。
數據庫系統用的是mysql,當然用其它的也類似。
先看第一個數據表,也是主要的數據表:news
create table news2 (newsid int not null,
userid int,
kwid int, // 關鍵詞外鍵
title varchar(100),
content text,
hits int,
cdate varchar2(30),
mdate varchar2(30),
primary key(newsid));
再插入一個樣本數據:
insert into news2 (newsid, title, content) values (1, 'test title', 'test body');
設計思路:用mvc模式編程,將數據以一個helper class news.java 打包,
并通過newsdao.java進行數據庫操作。
設計階段,用uml勾畫出系統的object.
...此處省略
newsdao的主要方法有:
1. public news getnewsbyprimarykey(int newsid);
2. public news[] getrecentnews();
3. public news[] gethotnews();
......
news.java的代碼如下:
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,int userid,int kwid,int hits,string title,string content,string cdate)
{
this.newsid=newsid;
this.userid=userid;
this.kwid=kwid;
this.hits=hits;
this.title=title;
this.content=content;
this.cdate=cdate;
}
public news(int id, string t, string cnt) {
this.newsid = id;
this.title = t;
this.content = cnt;
}
public int getnewsid()
{
return newsid;
}
public void setnewsid(int newsid)
{
this.newsid=newsid;
}
public int getuserid()
{
return userid;
}
public void setuserid(int userid)
{
this.userid=userid;
}
public int getkwid()
{
return kwid;
}
public void setkwid(int kwid)
{
this.kwid=kwid;
}
public int gethits()
{
return hits;
}
public void sethits(int hits)
{
this.hits=hits;
}
public string gettitle()
{
return title;
}
public void settitle(string title)
{
this.title=title;
}
public string getcontent()
{
return content;
}
public void setcontent(string content)
{
this.content=content;
}
public string getcdate()
{
return cdate;
}
public void setcdate(string cdate)
{
this.cdate=cdate;
}
}
說明:這個程序可以用作javabean,作為錄入表單的參數攜帶者(params holder).
|||最主要的文件newsdao.java代碼如下:
package news;
import java.sql.*;
public class newsdao
{
connection conn = null;
statement stmt = null;
resultset rs = null;
string url="jdbc:mysql://localhost:3306/joke?user=root";
public newsdao()
{
try {
class.forname ("com.mysql.jdbc.driver");
}
catch (java.lang.classnotfoundexception e) {
system.err.println("joke():"+e.getmessage());
}
}
public news getnewsbyprimarykey(int newsid) throws sqlexception
{
connection conn=null;
statement stmt;
resultset rs;
news news = null;
string sql="select newsid,title,content from news2"+
" where newsid="+newsid+"";
conn = getconnection();
stmt = conn.createstatement();
rs=stmt.executequery(sql);
if(rs.next())
{
news = new news(rs.getint(1), rs.getstring(2),rs.getstring(3));
}
rs.close();
stmt.close();
conn.close();
return news;
}
private connection getconnection() throws sqlexception
{
connection conn = null;
conn = drivermanager.getconnection(url);
return conn;
}
}
說明:這個程序作為示例代碼,非常簡單,沒有考慮異常,更主要的method
如getrecentnews()等,大家可以自己參考實現。
簡單的jsp調用測試程序:getnews.jsp
<%@page contenttype="text/html;charset=gb2312" %>
<%@page import="news.*" %>
<%
newsdao newsdao = new newsdao();
news news = newsdao.getnewsbyprimarykey(1);
if(news != null) {
out.println("title:"+news.gettitle());
out.println("<br>");
out.println("body:"+news.getcontent());
}
else out.println("failed.");
%>
說明:這個簡化實現其實是dao模式的省略形式,還應該有interface,dao factory的。
有時間的話,可能以后會給出示例,當然,大家在熟悉oop方式之后,也能夠自己補齊。
還有,編譯的時候 用 javac news/*.java 就可以了。
如果系統提示找不到news, 那其實是因為你的newsdao.java有問題,并非真的是news不在路徑上。在同一個包內,一般這樣編譯就可以的。只不過,編譯的錯誤提示誤導人。
新聞熱點
疑難解答