相关资料:ObjectWeb http://rmijdbc.ow2.org/
下载驱动:RmiJdbc.jar
Make your
CLASSPATH
variable point on the RmiJdbc.jar
package: it is located in the dist/lib subdirectory of the RmiJdbc distribution- Unix example (bash style):
export CLASSPATH=$CLASSPATH:/usr/local/RmiJdbc/dist/lib/RmiJdbc.jar
- Windows example:
set CLASSPATH=%CLASSPATH%;C:\RmiJdbc\dist\lib\RmiJdbc.jar
Start the RmiJdbc Server component:
java org.objectweb.rmijdbc.RJJdbcServer [-noreg] [-port regportnum] [-lp portnum] [-sm] [-ssl] [driverList]
Options:
-noreg
: means you launch the RmiJdbc server with an external rmiregistry.-port regportnum
: specify the rmiregistry port (optional, useful if you launch rmiregistry on a port of your own).-lp portnum
: specify the listener port for remote objects (optional, useful if need to use a single port for remote objects – otherwise, dynamically allocated ports are used).-sm
: use the standard RMI SecurityManager (otherwise, a relaxedSecurityManager
is used, equivalent to the RMISecurityManager
withAllPermissions
set).-ssl
: use in SSL mode (see documentation for details).driverList
: list of JDBC Driver classes available on your server (ex.org.enhydra.instantdb.jdbc.idbDriver
). You can also declare your driver list in thejdbc.drivers
system property (java -Djdbc.drivers=driverList RmiJdbc.RJJdbcServer
).
Then, a remote JDBC client can access your local database!
client code for example:
import java.sql.*;
import java.rmi.*;
import RmiJdbc.*;
import java.net.InetAddress;
/** * This is a sample program for RmiJdbc client/server jdbc Driver * RmiJdbc relies on Java RMI for jdbc objects distribution */
public class TestClient {
public static void main(String[] args) {
try {
// Register RmiJdbc Driver in jdbc DriverManager
// On some platforms with some java VMs, newInstance() is necessary...
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
// Test with InstantDB java database engine
// See http://www.lutris.com/products/instantDB/index.html
// for info & download
String url = "jdbc:idb:sample.prp"; // RMI host will point to local host
String rmiHost = new String( "//" + InetAddress.getLocalHost().getHostName());
// RmiJdbc URL is of the form:
// jdbc:rmi://<rmiHostName[:port]>/<jdbc-url>
java.sql.Connection c = DriverManager.getConnection("jdbc:rmi:" + rmiHost + "/" + url); java.sql.Statement st = c.createStatement();
java.sql.ResultSet rs = st.executeQuery("SELECT * FROM import1");
java.sql.ResultSetMetaData md = rs.getMetaData();
while(rs.next()) {
System.out.print("\nTUPLE: | ");
for(int i=1; i<= md.getColumnCount(); i++) {
System.out.print(rs.getString(i) + " | "); }
} rs.close();
} catch(Exception e) { e.printStackTrace(); } }};
和Spring集成的方式(首先也需要Start the RmiJdbc Server component):
1.加入相应的Jar包
2.在applicationContext.xml中加入bean
<bean id="wldtDataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.objectweb.rmijdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:rmi://172.20.52.26/jdbc:odbc:SiteWeaver</value><!--java默认1099端口-->
</property></bean><bean id="wldtjdbcDAO" class="com.thunisoft.summer.dao.jdbc.JdbcDAO">
<property name="dataSource">
<ref bean="wldtDataSource" />
</property>
</bean>
3.在DAO层注入数据源
private JdbcTemplate getWldtJdbcTemplate() {
return ((JdbcDAO) AppContext.getInstance().getAppContext().getBean( "wldtjdbcDAO")).getJdbcTemplate(); }
其中JdbcDAO继承了 JdbcDAO extends JdbcDaoSupport 在其中注入了相应的数据源,方便调用其提供的模版方法template
4.在RMI的server端还需要设置ODBC数据源
如图:

