Jena将owl文件持久化到数据库中

package cn.edu.shu.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;

/**
 * 
 * <p>
 * ClassName OWLFile2DB
 * </p>
 * <p>
 * Description :该类能够将owl文件持久化到数据库中。并自己主动创建生成的表。注意会生成多个表。<br/>
 * 依赖的jar包是jena-2.6.0.jar/iri-0.7.jar/icu4j-3.4.4.jar/mysql驱动包
 * </p>
 * 
 * @author wangxu wangx89@126.com
 *         <p>
 *         Date 2014-10-11 下午08:32:34
 *         </p>
 * @version V1.0
 * 
 */

public class OWLFile2DB {
	private static final String DB = "MySQL";
	String CLASSNAME = "com.mysql.jdbc.Driver";
	String DBURL = "jdbc:mysql://localhost:3306/ontologyIR?useUnicode=true&characterEncoding=UTF8";
	String DBUSER = "root";
	String DBPWD = "admin";

	public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
		new OWLFile2DB().createTable();
		System.out.println("succeed");
	}

	public void createTable() throws ClassNotFoundException, IOException, SQLException {
		Class.forName(CLASSNAME);
		IDBConnection conn = new DBConnection(DBURL, DBUSER, DBPWD, DB);// 获取连接
		ModelMaker maker = ModelFactory.createModelRDBMaker(conn);// 创建ModelMaker对象
		Model base = maker.createModel("ontologyIR");
		FileInputStream inputStream = null;
		File file = new File("Creature.owl");
		inputStream = new FileInputStream(file);
		InputStreamReader in = null;
		in = new InputStreamReader(inputStream, "UTF-8");

		base.read(in, null);
		in.close();
		base.commit();// 持久化到数据库中
		conn.close();
	}

}

用到的owl文件

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#"
     xml:base="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"/>
    


    <!-- 
    ///
    //
    // Object Properties
    //
    ///
     -->

    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated">
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat">
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat">
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- 
    ///
    //
    // Classes
    //
    ///
     -->

    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:Class>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值