Jena to persist owl files to the database

Source: Internet
Author: User

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: This class can persist owl files into the database and automatically create generated tables, noting that multiple tables are generated;<br/> *         The dependent jar package is the Jena-2.6.0.jar/iri-0.7.jar/icu4j-3.4.4.jar/mysql driver package * </p> * * @author Wangxu [email protected] * <p> * Date 2014-10-11 PM 08:32:34 * </p> * @version V1.0 * */public class Owlfile2db {Priv Ate 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";p ublic 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); I DBConnection conn = new DBConnection (Dburl, DBUSER, Dbpwd, DB);//Get connection Modelmaker maker = Modelfactory.createmodelrdbmaker (conn);//Create Modelmaker object Model base = Maker.createmodel ("Ontologyir"); FileInputStream inputstream = null; File File = new file ("Creature.owl"); InputStream = new FileInputStream (file); InputStreamReader in = Null;in = new Inputstr Eamreader (InputStream, "UTF-8"); Base.read (in, null); In.close (); Base.commit ();//persisted to the database Conn.close ();}}

Owl file used

<?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#" &GT;]&GT;&LT;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 Properti ES//////////////////////////////////////////-<!--http://www.semanticweb.org/wangxu/ontologies/201 4/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--&LT;OWL:OBJECTP Roperty 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/wang Xu/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/u Ntitled-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) h Ttp://owlapi.sourceforge.net--


Jena to persist owl files to the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.