用jena将本体存入mysql_使用jena写入多个本体文件到mysql

代码如下:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

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

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

import com.hp.hpl.jena.ontology.OntModel;

import com.hp.hpl.jena.ontology.OntModelSpec;

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

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

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

public class JenaDBOpration {

// path of driver class

public static final String strDriver = "com.mysql.jdbc.Driver";

// The URL of the MySql database

// this can be modified when the server location changes

public static final String strURL = "jdbc:mysql://localhost/ontologydb";

// database user id

public static final String strUser = "root";

// database password

public static final String strPassWord = "nicky";

// database type

public static final String strDB = "MySQL";

public static final String strFilePath1 = "ontologymetadatamodel.owl/";

public static final String strFilePath2 = "1dhdwqm.owl/";

public static final String strOntologyName1 = "One";

public static final String strOntologyName2 = "Two";

public static boolean TestMySQLDriver(String MySQL_Driver) {

boolean result = false;

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

try {

Class.forName(MySQL_Driver);

result = true;

} catch (ClassNotFoundException e) {

System.out.println(e.getMessage());

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

}

return result;

}

public static InputStreamReader ReadFile(String FilePath) {

FileInputStream inputSreamfile = null;

try {

File file = new File(FilePath);

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();

}

return in;

}

// 建立一个到mysql的连接

public static IDBConnection connectDB(String DB_URL, String DB_USER,

String DB_PASSWARD, String DB_NAME) {

return new DBConnection(DB_URL, DB_USER, DB_PASSWARD, DB_NAME);

}

/* 从文件读取本体并将其存入数据库 */

public static boolean createDBModelFromFile(IDBConnection con,

String Ontology_Name, String filePath) {

try {

ModelMaker maker = ModelFactory.createModelRDBMaker(con);

Model base = maker.createModel(Ontology_Name);

base.read(ReadFile(filePath), null);

base.commit();

} catch (Exception E) {

System.out.println(E.getMessage());

}

return true;

}

/* 从数据库中得到已存入本体 */

public static OntModel getModelFromDB(IDBConnection con,

String Ontology_Name) {

ModelMaker maker = ModelFactory.createModelRDBMaker(con);

Model base = maker.getModel(Ontology_Name);

OntModel newmodel = ModelFactory.createOntologyModel(

getModelSpec(maker), base);

return newmodel;

}

public static OntModelSpec getModelSpec(ModelMaker maker) {

OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);

spec.setImportModelMaker(maker);

return spec;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

boolean test = false;

try {

test = JenaDBOpration.TestMySQLDriver(strDriver);

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

if (test) {

try {

IDBConnection idbc = JenaDBOpration.connectDB(strURL,

strUser, strPassWord, strDB);

JenaDBOpration.createDBModelFromFile(idbc,

strOntologyName1, strFilePath1);

JenaDBOpration.createDBModelFromFile(idbc,

strOntologyName2, strFilePath2);

} catch (Exception ex2) {

System.out.println(ex2.getMessage());

}

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

存储到Mysql后,注意是两个不同名称的本体文件,共计生成了9张表

在7张表的基础上追加了2涨,分别为Jena_g2t0_reif和Jena_g2t1_stmt,

另外,Jena_graph更新了一条记录,Jena_long_lit中存放的是rdf:label的数据,Jena_sys_stmt中追加了若干记录;

Jena_g1t1_stmt和Jena_g2t1_stmt中村法规的是本体文件的数据信息,包括有class、properties(其它的暂时还不清楚,因为我的本体里面暂时只有这些,instance不清楚存放在哪,后续试验会做)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值