Many JSP beginners in learning simple JSP programming, often stay in the JSP inside the SQL statement to tune a JavaBean database connection stage, stalled.
This simple tutorial is expected to help beginners learn to use OOP ideas for JSP programming.
Scenario: A simple news system that consists of 2-3 data tables.
The database system uses MySQL, and of course it is similar to others.
First datasheet, and the main data sheet: 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 one more sample data:
insert into news2 (newsid, title, content) values (1, 'test title', 'test body');
Design idea: Programming with MVC pattern, packing data in a helper class News.java,
And through the Newsdao.java database operation.
In the design phase, UML is used to sketch the object of the system.
... omitted here
The main methods of Newsdao are:
1. Public News getnewsbyprimarykey (int newsid);
2. Public news[] Getrecentnews ();
3. Public news[] Gethotnews ();
......
The code for News.java is as follows:
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)
{
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;
}
}
Description: This program can be used as JavaBean, as a parameter carrier for entry form (params Holder).