jena持久化到数据库
来源:互联网 发布:java中线程安全的集合 编辑:程序博客网 时间:2024/06/11 21:10
jena支持的数据库有:
MySQL 5.0.18JDBC driver versions: 3.0, 3.1, 5.0PostgreSQL 7.3
PostgreSQL 8.0JDBC driver 7.3
JDBC driver 8.0Apache Derby 10.1 Oracle 10 XEOracle ojdbc14 driver (thin driver) 10.2.0.2Oracle 9i Release 2Oracle ojdbc14 driver (thin driver) 10.2.0.2Oracle 10g Release 2Oracle ojdbc14 driver (thin driver) 10.2.0.2Microsoft SQL Server 2005 Express SP1Microsoft SQL Server 2005 JDBC DriverMicrosoft SQL Server 2000
Microsoft SQL Server Desktop EditionMicrosoft 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
- jena持久化到数据库
- Jena将owl文件持久化到数据库中
- Jena将owl文件持久化到数据库中
- Jena 利用数据库保存,持久化本体
- Jena——将本体持久化到MySQL数据库中
- Jena持久化模型
- 使用jena持久化OWL本体到MySQL
- 运用jena将本体持久化到Mysql
- jena保存本体到数据库中
- Jena读取Ontology本体到Mysql数据库
- 关于jena OWL持久化 Table 'jena.jena_sys_stmt' doesn't exist的解决
- ActiveMQ 消息持久化到数据库
- tomcat持久化会话到oracl数据库
- ActiveMQ将消息持久化到数据库
- Apache ActiveMQ 持久化到MySQL数据库
- ActiveMQ消息持久化到Mysql数据库
- acticeMq持久化到mysql数据库配置
- activeMQ将消息持久化到数据库
- 你写软件我推广 -- 微软 & WPMind 社区合作计划
- Linux下apache+svn+ssl完美结合搭建安全版本控制平台
- ,英式发音和美式发音有什么区别
- RDF数据查询语言SPARQL
- BlackBerry上中国电信的WAP网关连接解析
- jena持久化到数据库
- inner join和where的区别
- 全球市值500强企业排名出炉 中石油第一
- 浅谈工作流数据
- SQL 操作全集
- 数据结构习题答案-第一章
- 批处理命令大全
- Jena的一个例子
- 第二章