首页 > 开发 > JSP > 正文

实战JSP进阶编程之一

2020-02-05 13:31:20
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。

  不少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不在路径上。在同一个包内,一般这样编译就可以的。只不过,编译的错误提示误导人。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表