本文以一個(gè)完整的JavaBean數(shù)據(jù)庫(kù)訪問(wèn)程序簡(jiǎn)要說(shuō)明jsp操作數(shù)據(jù)庫(kù)。
本程序由3個(gè)bean組成,其中WebConstants中定義全局變量,ConnectionManager管理數(shù)據(jù)庫(kù)連接,MainBean利用WebConstants和ConnectionManager操作數(shù)據(jù)庫(kù)。
首先定義全局變量,如下:
package WebRelease;
public interface WebConstants
{
public static final String 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";
}
接著創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)連接管理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中,偵聽(tīng)數(shù)據(jù)庫(kù)是否連接,如果連接則對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作:
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");
}
}
}
////////////////////////////////////////////////////////////////////////////////
" />