以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  [原创]本体写入到oracle  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=120852)


--  作者:caeser
--  发布时间:8/31/2011 9:40:00 PM

--  [原创]本体写入到oracle

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.db.RDFRDBException;
import com.hp.hpl.jena.db.impl.IRDBDriver;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class Onto2Database {
 
 public static final String strDriver = "oracle.jdbc.driver.OracleDriver"; // path of driver class
    public static final String strURL = "jdbc:oracle:thin:@ip:port:database"; // URL of database
    public static final String strUser = "wsk"; // database user id
    public static final String strPassWord = "wsk123"; // database password
    public static final String strDB =  "Oracle"; // database type


    public static void main(String[] args){
        try{
           // 创建一个数据库连接
         DBConnection conn = new DBConnection(strURL, strUser, strPassWord, strDB);
             // 加载数据库驱动类,需要处理异常
             try{
                  Class.forName(strDriver);

             }catch(ClassNotFoundException e) {

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

             }
             System.out.println(conn);
           // 使用数据库连接参数创建一个模型制造器
             ModelMaker maker = ModelFactory.createModelRDBMaker(conn);
             
             // 创建一个默认模型,命名为 MyOntology
             Model defModel = maker.createModel("MyOntology");
             
             System.out.println("猪跑跑。。。");
             // 准备需要存入数据库的本体文件,建立输入文件流
             FileInputStream inputSreamfile = null;
             try {
                  File file = new File("F://P3.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...");
        }
    }
}


--  作者:admin
--  发布时间:8/31/2011 9:42:00 PM

--  
感谢分享!
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
109.375ms