以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  jena对mysql的读取[求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=83970)


--  作者:KevinNelson
--  发布时间:3/23/2010 4:03:00 PM

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


--  作者:KevinNelson
--  发布时间:3/23/2010 6:00:00 PM

--  
自己解决了。
model.commit()对idbc的connection进行了初始化,但是后面读取的时候没有正确的初始化这个connection,自然会报出连接错误。

解决方法:加上idbc.getConnection();

测试通过。


--  作者:Avansky
--  发布时间:4/22/2010 3:36:00 PM

--  
高手!赞!!
--  作者:zhouying_723
--  发布时间:6/3/2010 7:23:00 PM

--  
您好,我按照源程序执行,但是出现了以下的错误,还想请教您如何解决


06-03 19:21:37 WARN com.hp.hpl.jena.ontology.OntDocumentManager<read>:[1078:main] - An error occurred while attempting to read from urn:x-hp-jena:test2. Msg was 'Not found: urn:x-hp-jena:test2'.
com.hp.hpl.jena.shared.NotFoundException: Not found: urn:x-hp-jena:test2


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