jena mysql_使用Jena将本体存入MySQL

/** 将本体存入MySQL **/

import java.io.*;

import java.sql.SQLException;

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

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

public class Onto2Database{

public static final String strDriver = "com.mysql.jdbc.Driver"; //path of driver class

public static final String strURL = "jdbc:mysql://localhost/OntoDB"; // URL of database

public static final String strUser = "root"; //database user id

public static final String strPassWord = "onto"; //database password

public static final String strDB = "MySQL"; //database type

public static void main(String[] args){

try{

// 创建一个数据库连接

IDBConnection conn = new DBConnection ( strURL, strUser, strPassWord, strDB );

// 加载数据库驱动类,需要处理异常

try{

Class.forName(strDriver);

}catch(ClassNotFoundException e) {

System.out.println("Driver is not available...");

}

// 使用数据库连接参数创建一个模型制造器

ModelMaker maker = ModelFactory.createModelRDBMaker(conn);

// 创建一个默认模型,命名为 MyOntology

Model defModel = maker.createModel("MyOntology");

// 准备需要存入数据库的本体文件,建立输入文件流

FileInputStream inputSreamfile = null;

try {

File file = newFile("G:\\eclipse\\workspace.thesis\\data\\MyOntology.owl");

inputSreamfile = new FileInputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

System.out.println("Ontology File is not available...");

}

InputStreamReader in = null;

try {

in = new InputStreamReader(inputSreamfile,"UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

// 读取文件

defModel.read(in,null);

// 关闭输入流读取器

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

// 执行数据转换,将本体数据存入数据库

defModel.commit();

// 关闭数据库连接

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}catch(RDFRDBException e){

System.out.println("Exceptions occur...");

}

}

} // 代码结束

5 查看数据库中的本体

执行程序之后,本体被存入MySQL数据库中。主要数据存在两个表中。

1)    ontodatabase.jena_g1t1_stmt 存储了本体的数据信息

2)    ontodatabase.jena_sys_stmt 存储了本体的元数据信息

可以通过命令行查看或者使用MySQL GUI客户端工具查看。建议使用后者。

链接:以下是关于语义网和本体论的经典论文【英文】

http://sxzx.360doc.com/content/080416/11/23378_1193793.html

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-11-17 13:57

浏览 3455

评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值