| --  作者:minniest --  发布时间:2/24/2010 1:53:00 PM
 
 --
 我把代码贴在下面,各位帮我看一下哈。我看有一个帖子说出现了这种情况,解决方法是把mysql的表的字符集改成utf8,我改了一下还是不行。。。我这里用的是中文本体,不过我试了下英文本体也出现同样的错误。。。
  package ontology;/**将本体存入MySQL**/
 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.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.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.ModelMaker;
  public class Onto2Database {
 public static final String strDriver="com.mysql.jdbc.Driver";//path of driver class
 public static final String strURL="jdbc:mysql://localhost:3306/emeronto";//URL of database"
 public static final String strUser="root";//database user id
 public static final String strPassWord="nkzhangjing";//database password
 public static final String strDB="MySQL";//database type
 public static void main(String[] args){
 try{
 //创建一个数据库连接
 IDBConnection con=new DBConnection(strURL,strUser,strPassWord,strDB);
 //加载数据库驱动类,并处理异常
 try{
 Class.forName(strDriver);
 }catch(ClassNotFoundException e){
 System.out.println("ClassNotFoundException,Driver is not available...");
 }
 //使用数据库连接参数创建一个模型制造器
 ModelMaker maker=ModelFactory.createModelRDBMaker(con);
 //创建一个默认模型,命名为MyModel
 Model defModel=maker.createModel("MyModel");
 //准备需要存入数据库的本体文件,建立输入文件流
 FileInputStream inputStreamfile=null;
 try{
 File file=new File("G://workspace//myeclipse//EmerOnto.owl");
 inputStreamfile=new FileInputStream(file);
 }catch(FileNotFoundException e){
 e.printStackTrace();
 System.out.println("Ontology File is not available...");
 }
 
 InputStreamReader in=null;
 try{
 in=new InputStreamReader(inputStreamfile,"UTF-8");
 }catch(UnsupportedEncodingException e){
 e.printStackTrace();
 }
 
 //读取文件
 defModel.read(in,null);
 //关闭输入流读取器
 try{
 in.close();
 }catch(IOException e){
 e.printStackTrace();
 }
 
 //执行数据转换,将本体数据存入数据库
 defModel.commit();
 
 //关闭数据库连接
 try{
 con.close();
 }catch(SQLException e){
 e.printStackTrace();
 }
 }catch(RDFRDBException e){
 System.out.println("Exceptions occur..");
 }
 
 }
 
 }//代码结束
 
 
 |