本文以一个完整的javabean数据库访问程序简要说明jsp操作数据库。
本程序由3个bean组成,其中webconstants中定义全局变量,connectionmanager管理数据库连接,mainbean利用webconstants和connectionmanager操作数据库。
首先定义全局变量,如下:
package webrelease;
public interface webconstants
{
public static final string driverclass ="driverclass";
const userid is the user id to connect to database
public static final string userid ="comm";
const passwd is the user password to connect to database
public static final string passwd ="comm123";
const url is the url to connect to database
public static final string url="jdbc:oracle:thin:@10.2.0.1:1521:orcl";
public static final string selecttype ="select";
public static final string connection ="connection";
public static final string connerror ="conerror";
}
接着创建一个数据库连接管理bean,如下:
package webrelease;
import java.io.*;
import java.beans.*;
import java.util.*;
import java.sql.*;
import webrelease.webconstants;
import oracle.jdbc.driver.*;
public class connectionmanager implements webconstants
{
private boolean debug = true;
protected connection con;
protected debugwriter writer;
propertychangesupport pcs;
////////////////////////////////////////////////////////////////////////////////
public connectionmanager()
{
pcs = new propertychangesupport(this);
writer = new debugwriter();
}
////////////////////////////////////////////////////////////////////////////////
public void setdebug(string b)
{
debug = b.equals("true");
}
////////////////////////////////////////////////////////////////////////////////
public void addpropertychangelistener( propertychangelistener l)
{
pcs.addpropertychangelistener(l);
}
////////////////////////////////////////////////////////////////////////////////
public void removepropertychangelistener(propertychangelistener l)
{
pcs.removepropertychangelistener(l);
}
////////////////////////////////////////////////////////////////////////////////
public void login()
{
try
{
drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
class.forname("oracle.jdbc.driver.oracledriver");
}
catch(exception e)
{
if(debug) { writer.writedebug("error setting driver:"+e.getmessage());}
}
try
{
con = drivermanager.getconnection(url,userid,passwd);
pcs.firepropertychange(connection,null,con);
if(debug)
{
writer.writedebug("connection succeded ! url:"+url+"user:"+userid+
"pwd:"+passwd);
}
}
catch(exception e)
{
pcs.firepropertychange(connerror,null,e);
if(debug)
{
writer.writedebug("connection failed ! url:"+url+"user:"+userid+
"pwd:"+passwd);
}
}
}
}
在mainbean中,侦听数据库是否连接,如果连接则对数据库进行操作:
package webrelease;
import java.io.*;
import java.beans.*;
import java.util.*;
import java.sql.*;
import webrelease.*;
public class mainbean implements propertychangelistener ,webconstants
{
private boolean debug = true;
protected connection con;
protected debugwriter writer;
////////////////////////////////////////////////////////////////////////////////
public mainbean()
{
writer = new debugwriter();
}
////////////////////////////////////////////////////////////////////////////////
public void propertychange(propertychangeevent evt)
{
string prop = evt.getpropertyname();
//see if we got a connection
if(prop.equals(connection))
{//ok so update the local connection
try
{
//make sure it really is a connection
con = (connection)evt.getnewvalue();
}
catch(exception e)
{
writer.writedebug("error connecting to database"+e.getmessage());
}
}
}
////////////////////////////////////////////////////////////////////////////////
public void setconnectionmanager(connectionmanager cm)
{
if(cm != null)
{
// to remove the old listener from cm
// to avoid confusion in the mainbean
cm.removepropertychangelistener(this);
cm.addpropertychangelistener(this);
if(debug)
{
writer.writedebug("mainbean;set connection manager"+ cm.tostring());
}
}
else
{
if(debug)
{
writer.writedebug("mainbean;tried to set a null connection manager");
}
}
}
////////////////////////////////////////////////////////////////////////////////
public boolean isconnected()
{
//see if datatabe is connected
return (con != null);
}
////////////////////////////////////////////////////////////////////////////////
public void processquery()
{
try
{
statement stmt=con.createstatement();
resultset rs=stmt.executequery("select * from hello order by id");
writer.writedebug("select * from hello order by id");
while(rs.next())
{
writer.writedebug("next");
string str=rs.getstring("name");
writer.writedebug(str+"   ");
}
rs.close();
}
catch(exception e)
{
}
}
}
ok,现在我们来测试一下这个程序,创建一个测试页面:
<%@ page contenttype="text/html; charset=gb2312" %>
<html>
<head>
<title>
hello
</title>
</head>
<body bgcolor="#ffffc0">
<h1>
<jsp:usebean id="main" class="webrelease.mainbean" scope ="session"/>
<jsp:usebean id="con" class="webrelease.connectionmanager" scope ="session"/>
<%
main.setconnectionmanager(con);
con.login();
%>
<%
if(main.isconnected())
out.print("mainbean connect success!!!");
main.processquery();
%>
</h1>
</body>
</html>
在tomcat中浏览这个页面,我们应该可以看到输出“mainbean connect sucess!!!”
注释不是太多,希望你能看明白。
新闻热点
疑难解答