jena持久化到数据库

Jena 由 HP Labs (http://www.hpl.hp.com ) 开发的Java 开发工具包, 用于Semantic Web( 语义网) 中 的应用程序开发;Jana 是开源的,在下载的文档中有Jena 的完整代码。

jena支持的数据库有:

Database EngineJDBC 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 XEOracle ojdbc14 driver (thin driver) 10.2.0.2
Oracle 9i Release 2 Oracle ojdbc14 driver (thin driver) 10.2.0.2
Oracle 10g Release 2Oracle ojdbc14 driver (thin driver) 10.2.0.2
Microsoft SQL Server 2005 Express SP1Microsoft 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值