jena持久化到數據庫

 

jena支持的數據庫有:

 

Database Engine JDBC Driver
HSQLDB 1.8.0  
MySQL 4.1.11
MySQL 5.0.18
JDBC driver versions: 3.0, 3.1, 5.0
PostgreSQL 7.3
PostgreSQL 8.0
JDBC driver 7.3
JDBC driver 8.0
Apache Derby 10.1  
Oracle 10 XE Oracle ojdbc14 driver (thin driver) 10.2.0.2
Oracle 9i Release 2 Oracle ojdbc14 driver (thin driver) 10.2.0.2
Oracle 10g Release 2 Oracle ojdbc14 driver (thin driver) 10.2.0.2
Microsoft SQL Server 2005 Express SP1 Microsoft SQL Server 2005 JDBC Driver
Microsoft SQL Server 2000
Microsoft SQL Server Desktop Edition
Microsoft SQL Server 2005 JDBC Driver
jTDS version 1.2

 

  jena可以操作rdf文件和owl文件,下面我們給出例子持久化這些文件到數據庫

 

    Jena supports both memory models and database models. In general, a Jena program may use both types of models identically. However, there are some differences in how the models are created. Creating a memory model can be done with a single Jena call. Creating a database model, or opening a previously created one, requires several steps as as follows.

    Persistent models are created in the same way for any database system:

    1、Load the JDBC driver. This enables the Jena program to communicate with the database instance.

    2、Create a database connection. This creates a Java object for a database connection.

    3、Create a ModelMaker for the database

    4、Create a Model for existing or new data.

    Creating and Accessing Persistent Models有兩種方法:

 

    Factory Methods:

 

    Creating or opening a model is a three-step process. First, the driver class must be loaded and a connection established to the database (note that in Jena2, the database type is specified as part of the database connection). Second, a model maker class is constructed. The model maker creates persistent instances of the Model class. Third, the model maker class is invoked to create new models or to open existing models. The following examples show how this is done.

     // database URL

    String M_DB_URL         = "jdbc:mysql://localhost/test";

 

    // User name

    String M_DB_USER        = "test";

 

   // Password

   String M_DB_PASSWD      = "";

 

   // Database engine name

   String M_DB = "MySQL";

 

  // JDBC driver

  String M_DBDRIVER_CLASS = "com.mysql.jdbc.Driver";

 

  // load the the driver class

  Class.forName(M_DBDRIVER_CLASS);

 

  // create a database connection

  IDBConnection conn = new DBConnection(M_DB_URL, M_DB_USER, M_DB_PASSWD, M_DB);

 

  // create a model maker with the given connection parameters

  ModelMaker maker = ModelFactory.createModelRDBMaker(conn);

 

   // create a default model

   Model defModel = maker.createDefaultModel();

 

  // Open existing default model

  Model defModel = maker.openModel();

 

  // or create a named model

  Model nmModel = maker.createModel("MyNamedModel");

 

  // or open a previously created named model

  Model prvModel = maker.openModel("AnExistingModel");

 

  ModelRDB Methods:


   Creating an instance of ModelRDB is a two-step process. As with the factory methods,the first step is to load the driver class and establish a database connection.Second, the static methods on ModelRDB are used to create new ModelRDB instances or to open existing ones.

   The following examples show how this is done.

   String M_DB_URL          = "jdbc:mysql://localhost/test";

   String M_DB_USER         = "test";

   String M_DB_PASSWD       = "";

   String M_DB              = "MySQL";

   String M_DBDRIVER_CLASS  = "com.mysql.jdbc.Driver";

 

   // load the the driver class

   Class.forName(M_DBDRIVER_CLASS);

 

   // create a database connection

  IDBConnection conn = new DBConnection(M_DB_URL, M_DB_USER, M_DB_PASSWD, M_DB);

 

   // ---- Directly use ModelRDB

   // create a default model

   ModelRDB defModel = ModelRDB.createModel(conn);...

 

   // Open an existing model.

   ModelRDB defModel2 = ModelRDB.openModel(conn);

   ...

 

   // create a named model

   ModelRDB nmModel = ModelRDB.createModel(conn,"MyModelName");

   ...

 

   // open a named model

   ModelRDB nmModel2 = ModelRDB.openModel(conn,"ExistingModelName");

   ...

 

   下面給出一個完整的源碼例子:

   package com.jena.db;

   import java.sql.SQLException;

   import com.hp.hpl.jena.db.*;

   import com.hp.hpl.jena.ontology.OntModel;

   import com.hp.hpl.jena.ontology.OntModelSpec;

   import com.hp.hpl.jena.rdf.model.*;

   public class Example {

      public void test()

      {

           String className = "com.mysql.jdbc.Driver";           // path of driver class

            try {

                      Class.forName (className);

                      String DB_URL =       "jdbc:mysql://localhost/jena";  // URL of database 

                      String DB_USER =     "root";         // database user id

                      String DB_PASSWD = "123";         // database password

                      String DB =          "MySQL";          // database type

                                // Create database connection

                      IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );

                      ModelMaker maker = ModelFactory.createModelRDBMaker(conn) ;

                      // create or open the default model

                     Model model = maker.createDefaultModel();

                     OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, model );

                     m.read("file:creature.owl");

                     conn.close();

                } catch (Exception e) {

                   e.printStackTrace();

             }

         }

   public void test1()

      {

          try{

                   Class.forName( "com.mysql.jdbc.Driver");

                   String DB_URL =       "jdbc:mysql://localhost/jenatest";  // URL of database 

                   String DB_USER =     "root";            // database user id

                   String DB_PASSWD = "123";           // database password

                   String DB =          "MySQL";  

                   IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );

                   ModelRDB defModel = ModelRDB.createModel(conn);

                   defModel.read("file:creature.owl");

                   conn.close();

                  //conn.cleanDB();//這個方法是清除數據庫裏的東西

        }catch(Exception e){e.printStackTrace();} 

    }

   public static void main(String[]args)

     {

            Example e=new Example();

            e.test();

           e.test1();

     }

}

這些都是從幫助文檔裏總結和截取出來的,希望對有需要的朋友提供幫助。。。

 

一個例子:http://jena.sourceforge.net/examples/persistent-ont-model/index.html

mysql持久化:http://jena.sourceforge.net/DB/mysql-howto.html

http://jena.sourceforge.net/DB/creating-db-models.html

參考:http://jena.sourceforge.net/ontology/index.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章