DOC

3104.docjava

By Francis Anderson,2014-10-14 09:25
9 views 0
3104.docjava

    由于程序的原因,本文件未被完整保存。 首页 ? 研究文集 ? 数据库相关

    Castor JDO实例

    Redhouse 原创 (参与分(4082,专家分(20) 发表(2003-09-09 17:58 更新(2003-09-10

     10:46 版本(1.0 阅读(4035

本文采用mysql 1.4 作为实例数据库,安装好Mysql后不要忘了启动

    利用mysql命令或者图形化工具,创建表prod [pre]CREATE TABLE prod (

     id int(11) NOT NULL default '0',

     name varchar(200) NOT NULL default '',

     price decimal(18,2) NOT NULL default '0.00' );[/pre]

    实体Bean——Product代码如下(

    1. package com.castor;

    2.

    3. import org.exolab.castor.jdo.Database;

    4. import org.exolab.castor.jdo.Persistent;

    5.

    6. 7. public class Product implements Persistent

    8. {

    9. private int _id;

    10. private String _name;

    11. private float _price;

    12. private Database _db;

    13.

    14. public int getId()

    15. {

    16. return _id;

    17. }

    18. public void setId( int id )

    19. {

    20. _id = id;

    21. }

    22. public String getName()

    23. {

    24. return _name;

    25. }

    26. public void setName( String name )

    27. {

28. _name = name;

    29. }

    30. public float getPrice() 31. {

    32. return _price;

    33. }

    34. public void setPrice( float price ) 35. {

    36. _price = price;

    37. }

    38. public void jdoPersistent( Database db )

    39. {

    40. _db = db;

    41. }

    42. public void jdoTransient() 43. {

    44. _db = null;

    45. }

    46. public Class jdoLoad(short accessMode)

    47. {

    48. if ( _name.indexOf("PC") >= 0 ) {

    49. return Computer.class; 50. }

    51. return null;

    52. }

    53. public void jdoBeforeCreate( Database db )

    54. {

    55. }

    56. public void jdoAfterCreate() 57. {

    58. }

    59. public void jdoStore(boolean modified)

    60. {

    61. }

    62. public void jdoBeforeRemove() 63. {

    64. }

    65. public void jdoAfterRemove() 66. {

    67. }

    68. public void jdoUpdate() 69. {

    70. }

    71. public String toString()

72. {

    73. return "<id: " + _id + " name: " + _name + " price: " + _price + ">";

    74. }

    75.}

    测试代码如下Test.java

    1. package com.castor;

    2.

    3.

    4. import java.io.PrintWriter;

    5. import org.exolab.castor.jdo.JDO; 6. import org.exolab.castor.jdo.Database; 7. import org.exolab.castor.jdo.OQLQuery; 8. import org.exolab.castor.jdo.QueryResults; 9. import org.exolab.castor.util.Logger; 10.import org.exolab.castor.mapping.Mapping; 11.

    12.public class Test

    13.{

    14.

    15.

    16. public static final String DatabaseFile = "database.xml";

    17.

    18.

    19. public static final String MappingFile = "mapping.xml";

    20.

    21.

    22. public static final String Usage = "Usage: example jdo";

    23.

    24.

    25. private Mapping _mapping;

    26.

    27.

    28. private JDO _jdo;

    29.

    30. public static void main( String[] args ) 31. {

    32. PrintWriter writer;

    33. Test test;

    34.

    35. writer = new Logger( System.out ).setPrefix( "test" );

    36. try

    37. {

    38. test = new Test( writer ); 39. test.run( writer ); 40. }

    41. catch ( Exception except ) 42. {

    43. writer.println( except ); 44. except.printStackTrace( writer ); 45. }

    46. }

    47.

    48. public Test( PrintWriter writer ) 49. throws Exception

    50. {

    51. // Load the mapping file 52. _mapping = new Mapping( getClass().getClassLoader() );

    53. _mapping.setLogWriter( writer ); 54. _mapping.loadMapping( getClass().getResource( MappingFile ) );

    55.

    56. _jdo = new JDO();

    57. _jdo.setLogWriter( writer ); 58. _jdo.setConfiguration( getClass() 59. .getResource( DatabaseFile ).toString() );

    60. _jdo.setDatabaseName( "test" ); 61. }

    62.

    63. public void run( PrintWriter writer ) 64. throws Exception

    65. {

    66. Database db;

    67. Product product = null; 68. OQLQuery productOql; 69. QueryResults results; 70.

    71. db = _jdo.getDatabase(); 72. db.begin();

    73.

    74. Product prod = new Product(); 75.

    76. prod.setId(4);

    77. prod.setName("product"); 78. prod.setPrice(12.03f); 79. db.create(prod);

    80. db.commit();

    81.

    82. db.begin();

    83. prod.setName("sect");

    84. writer.println("persistent:" + db.isPersistent(prod));

    85. db.commit();

    86.

    87. db.begin();

    88. writer.println( "Begin transaction to remove Product objects" );

    89.

    90. productOql = db.getOQLQuery(

    91. "SELECT p FROM com.castor.Product p WHERE p.id = $1" );

    92.

    93. productOql.bind( 4 );

    94. results = productOql.execute();

    95.

    96. writer.println("test1");

    97. if ( results.hasMore())

    98. {

    99. writer.println("test2");

    100. product = ( Product ) results.next();

    101. writer.println( "Deleting existing product: " + product );

    102. writer.println("product:" + product.getName());

    103. }

    104. writer.println( "End transaction to remove Product objects" );

    105. db.commit();

    106. db.close();

    107. }

    108. }

好像还缺少了什么——配置文件

    database.xml

    [pre]<!DOCTYPE databases PUBLIC

     "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN"

     "http://castor.exolab.org/jdo-conf.dtd">

<database name="test" engine="mysql" >

     <driver url="jdbc:mysql://localhost/test"

     class-name="org.gjt.mm.mysql.Driver">

     <param name="user" value="root" />

     <param name="password" value="root" />

     </driver>

     <mapping />

    </database>

    mysql数据库的用户名和密码根据您的数据库作相应的设置 mapping.xml

<!DOCTYPE databases PUBLIC

     "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"

     "http://castor.exolab.org/mapping.dtd"> <mapping>

     <!-- Mapping for Product -->

     <class name="com.castor.Product" identity="id">

     <description>Product definition</description>

     <map-to table="prod" xml="product" />

     <field name="id" type="integer">

     <sql name="id" type="integer" />

     <bind-xml name="id" node="attribute"/>

     </field>

     <field name="name" type="string">

     <sql name="name" type="char" />

     <bind-xml name="name" node="element" />

     </field>

     <field name="price" type="float">

     <sql name="price" type="numeric" />

     <bind-xml name="price" node="element" />

     </field>

     </class>

    </mapping>[/pre]

    最后提醒一句,不要忘了将mysql驱动加到classpath中。 祝你成功!

版权声明 给作者写信 本篇文章对您是否有帮助; 投票( 投票结果(

    作者其它文章(

    作者全部文章 上一篇文章 下一篇文章 返回〔数据库相关〕

Report this document

For any questions or suggestions please email
cust-service@docsford.com