JDO数据库配置指定了去获取数据库服务器的连接,映射了Java类和Table表之间的关系;Castor不能用JDBC-ODBC桥,Access不被支持。获取一个数据库连接有下列三种方式:
one:作为一个JDBC 2.0的驱动URL;
two:作为JDBC2.0的数据源;
three:作为一个通过JNDI寻找的数据源;
当JDO作为J2EE应用时推荐第三种用法。允许服务器管理连接池和分布式事务;类映射被包含在一个外部映射文件中,允许一个数据库数据库配置文件中包含多个配置文件,或者两个数据库配置文件共享一个映射文件,但是两个数据库决不要去用重叠的映射文件,一个数据库推荐只用一个数据库文件。
作为客户端(JDBC应用):
JDO      jdo;
Database db;// Define the JDO object
jdo = new JDO();
jdo.setDatabaseName( "mydb" );
jdo.setConfiguration( "database.xml" );
jdo.setClassLoader( getClass().getClassLoader() );// Obtain a new database
db = jdo.getDatabase();
// Begin a transaction
db.begin();
// Do something
. . .
// Commit the transaction, close database
db.commit();
db.close();
J2EE应用:
InitialContext  ctx;
UserTransaction ut;
Database        db;// Lookup databse in JNDI
ctx = new InitialContext();
db = (Database) ctx.lookup( "java:comp/env/jdo/mydb" );// Begin a transaction
ut = (UserTransaction) ctx.lookup( "java:comp/UserTransaction" );
ut.begin();
// Do something
. . .
// Commit the transaction, close database
ut.commit();
db.close();
           要写一个datebase.xml和tablename.xml两个文件,里面:
<!DOCTYPE databases PUBLIC "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN"
                           "http://castor.exolab.org/jdo-conf.dtd">
The following configuration file uses an Oracle 8 thin JDBC driver and three mapping files:   <database name="ebiz" engine="oracle">
    <driver class-name="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@machine:post:SID">
      <param name="user" value="scott" />
      <param name="password" value="tiger" />
    </driver>
    <mapping href="product.xml" />
    <mapping href="orders.xml" />
    <mapping href="customers.xml" />
  </database>
         The following configuration file uses a connection obtained from the J2EE application server and a single mapping file:   <database name="ebiz" engine="oracle">
    <jndi name="java:comp/env/jdbc/mydb" />
    <mapping href="product.xml" />
  </database>products.xml是一个表的描述:
多个表也可以整合在一个xml文件中,xml文件名随便,只要关联统一就行了Here is an example of a mapping file and the corresponding Java object and DDL for the databse table.The following is an example Java object:package myapp;public class Product 
{
    private int       _id;    private String    _name;     private float     _price;     private ProductGroup _group;
    public int getId()
    ...    public void setId( int anId )
    ...    public String getName()
    ...    public void setName( String aName )
    ...    public float getPrice()
    ...    public void setPrice( float aPrice )
    ...    public ProductGroup getProductGroup()
    ...    public void setProductGroup( ProductGroup aProductGroup )
    ...
}
                    
 The following is the relational database table:create table prod 
(
  id        int           not null,
  name      varchar(200)  not null,
  price     numeric(18,2) not null,
  group_id  int           not null
);
                    
 The following is the mapping file for the example Java object:<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
                         "http://castor.exolab.org/mapping.dtd">
<mapping>    <class name="myapp.Product" identity="id">        <map-to table="prod" />        <field name="id" type="integer">
            <sql name="id" type="integer" />
        </field>        <field name="name" type="string">
            <sql name="name" type="char" />
        </field>        <field name="price" type="float">
            <sql name="price" type="numeric" />
        </field>        <field name="group" type="myapp.ProductGroup" >
            <sql name="group_id" />
        </field>    </class>
</maping>

解决方案 »

  1.   

    可以参考:http://castor.exolab.org
      

  2.   

    空口无凭,我想最关键的是 db = (Database) ctx.lookup( "java:comp/env/jdo/mydb" );
    jdni怎么能造型为(Database?)
      

  3.   

    这个问题也好办啊,你可以ctx.rebind(key, val);val可以是Database啊,key也不一定是"java:comp/env/jdo/mydb"。这样就可以lookup了。