手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

如何写自己的Type3 JDBC 驱动

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

? com.jw.server.RemoteStatementImpl类:是一个Statement类,并且它负责获得JDBC ResultSet。
? com.jw.server.RemoteResultSetImpl类:是一个ResultSet类。

下面我们详细分析每个类
RemoteDriverImpl
RemoteDriverImpl扮演一个RMI服务器(它继承了UnicastRemoteObject)并向JDBC驱动客户端提供了getConnection()方法。图2显示了RemoteDriverImpl的其他类与接口之间的关系。
com.jw.server.IremoteDriver是一个扩展了Remote的接口,它提供了getConnection()方法。这个方法创建JDBC Connection并作为一个IremoteConnection的引用返回给客户端。
由于RemoteDriverImpl同时还是JDBC驱动服务器的RMI服务器,它会读取ODBC的数据源名称(DSN),从部署在服务层中的DriverSettings.properties
文件读取用户名和密码。这些值可以创建一个JDBC Connection,此外,RemoteDriverImpl可以通过名称服务“RemoteDriver”注册自身,所有JDBC 的客户层可以通过Naming.lookup()方法与服务端连接。然后它会加载JDBC-ODBC桥来创建数据库连接:
public static void main(String args[])//自己替换[]
{
System.setSecurityManager(new RMISecurityManager());
try
{
// Get the data source name, data source user, data
source
// Password and log level
ResourceBundle settingsBundle =
ResourceBundle.getBundle(
"DriverSettings");
DSN = settingsBundle.getString("DSN");
dsUser = settingsBundle.getString("User");
dsPassword = settingsBundle.getString
("Password");
// Create a RemoteDriverImpl instance to register
with naming service
RemoteDriverImpl serverInstance = new
RemoteDriverImpl();
Naming.rebind
("RemoteDriver",serverInstance);
// Load the JDBC-ODBC Bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
...
}


getConnection()方法创建了JDBC-ODBC连接,然后返回RemoteConnectionImpl对象远程接口的引用来控制JDBC-ODBC连接:
public IRemoteConnection getConnection()
throws RemoteException,SQLException
{
String URL="jdbc:odbc:" DSN;
Connection sqlCon =
DriverManager.getConnection(URL,dsUser,dsPassword);
RemoteConnectionImpl ConnectionInstance =
new RemoteConnectionImpl(sqlCon);
return (IRemoteConnection)ConnectionInstance;
}


下面让我们来看看RemoteConnectionImpl。
RemoteConnectionImpl类
RemoteConnectionImpl继承了UnicastRemoteObject类并实现了IremoteConnection接口(图2、3显示了RemoteConnectionImpl类的其他类和接口的关系)。IremoteConnection接口提供了创建JDBC Statement和关闭数据库连接的方法,RemoteConnectionImpl同时封装了传入构造中的JDBC连接。
createStatement()创建了JDBC-ODBC Statement并返回RemoteConnectionImpl对象的引用来控制JDBC-ODBC Statement:
public IRemoteStatement createStatement() throws
RemoteException,SQLException
{
RemoteStatementImpl StmtImplInstance = new
RemoteStatementImpl(sqlConnection.createStatement());
return (IRemoteStatement)StmtImplInstance;
}


接下来closeConnection()方法通过内部封装的close()方法关闭JDBC Connection:
public void closeConnection() throws
RemoteException,SQLException
{
sqlConnection.close();
}


下面再看RemoteStatementImpl。
RemoteStatementImpl类
如图3、4中显示的RemoteStatementImpl类,它继承了UnicastRemoteObject类并实现了IremoteStatement接口。这个接口提供了创建JDBC ResultSet和关闭Statement对象的方法。RemoteStatementImpl同样封装了闯入构造的Statement对象。
executeQuery()方法创建了JDBC-ODBC的ResultSet并返回一个RemoteStatementImpl对象的引用来控制JDBC-ODBC的ResultSet:
public IRemoteResultSet executeQuery(String Query)
throws RemoteException,SQLException
{
ResultSet rs = sqlStatment.executeQuery(Query);
RemoteResultSetImpl remoteRs = new
RemoteResultSetImpl(rs);
return (IRemoteResultSet)remoteRs;
}


close()方法简单的关闭了JDBC Statement对象:
public void close() throws RemoteException,
SQLException
{
sqlStatment.close();
}


最后再来看看RemoteResultSetImpl类。
RemoteResultSetImpl类
RemoteResultSetImpl继承了UnicastRemoteObject类并实现了IremoteResult接口(图4 显示了RemoteResultSetImpl类的其他类和接口的关系)。IremoteResult接口获得JDBC ResultSet行数据并关闭ResultSet对象。RemoteResultSetImpl同样封装了构造中的ResultSet对象。
getNextRow()方法返回一个包含对象数组的JDBC ResultSet给客户端JWResultSet,如果ResultSet不包含任何数据会返回null:
public Object[] getNextRow() throws
RemoteException,SQLException//自己替换[]
{
// Return null if all data has already been
iterated
if(sqlRs.next() == false)
return null;
// Prepare the data row in an array of Objects
Object []row = new Object[colNum];//自己替换[]
for(int i = 1; i <= colNum; i )
{
row[i-1] = sqlRs.getString(i);//自己替换[]
}
return row;
}



close()方法会关闭ResultSet对象:

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!