新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → jena对mysql的读取[求助] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 14940 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: jena对mysql的读取[求助] 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Language | RDF/RDFS | Description Logics    
     KevinNelson 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:11
      积分:108
      门派:XML.ORG.CN
      注册:2009/8/17

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给KevinNelson发送一个短消息 把KevinNelson加入好友 查看KevinNelson的个人资料 搜索KevinNelson在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看KevinNelson的博客楼主
    发贴心情 jena对mysql的读取[求助]

    最近在做一个jena的例子,写入两个本体到mysql中,然后去读取它,成功;
    问题是我把写入操作注释掉后,只运行读取的操作,就会出错;
    获取的异常信息是
    Failure to instantiate DB Driver:MySQL java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/ontologydb

    百思不得其解啊,写入+读取可以运行,不会出现连接驱动错误;但是单独读取,就会报错。。不知道是什么问题。
    附源码如下

    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 = "F:\\毕业论文相关\\Ontology开发日志\\本体元数据模型本体\\OntologyMetaDataModel.owl";

     public static final String strOntologyName1 = "One";

     public static final String strOntologyName2 = "Two";
     public static final String strFilePath2 = "F:\\毕业论文相关\\Ontology开发日志\\一维水质模型本体\\1DHDWQM.owl";

     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(

      OntModelSpec.OWL_DL_MEM, 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 stubgetModelgetModel
      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());
      }

      try {
       IDBConnection idbc = JenaDBOpration.connectDB(strURL, strUser,
         strPassWord, strDB);
       ModelMaker mmr = ModelFactory.createModelRDBMaker(idbc);
       Model m = mmr.getModel(strOntologyName1);

       OntModel ontM = getModelFromDB(idbc, strOntologyName1);
      } catch (Exception ex) {
       System.out.println(ex.getMessage());
      }
     }
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/3/23 16:03:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/15 6:29:41

    本主题贴数4,分页: [1]

     *树形目录 (最近20个回帖) 顶端 
    主题:  jena对mysql的读取[求助](4954字) - KevinNelson,2010年3月23日
        回复:  您好,我按照源程序执行,但是出现了以下的错误,还想请教您如何解决06-03 19:21:37..(340字) - zhouying_723,2010年6月3日
        回复:  高手!赞!!(12字) - Avansky,2010年4月22日
        回复:  自己解决了。model.commit()对idbc的connection进行了初始化,但是后面读..(183字) - KevinNelson,2010年3月23日

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