首页 | 手机版 | 三国演义 | 三国志 | 史将 | 背景 | 藏书阁
首页 -> 精彩文章 -> 利用jena 的 api 调用 sparql 语句 查询 导入到Mysql数据库中的文件

利用jena 的 api 调用 sparql 语句 查询 导入到Mysql数据库中的文件

作者拉格浪日 标签mysql 导入 阅读次数:136
import java.io.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.db.*;

public class qdb{
   // Constants
    public static final String DB_URL = "jdbc:mysql://localhost/a";
    public static final String DB_USER = "root";
    public static final String DB_PASSWD = "zxcvb602";
    public static final String DB_TYPE = "Mysql";
    public static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    public static final String MODEL_NAME ="SXF";
   public static void main(String args[])
  {
   String queryString =
 "PREFIX vCard: " +
   "SELECT ?x ?name WHERE {?x vCard:FN ?name}";
   

// Open the file from the filesystem
try {
      Class.forName (DB_DRIVER);    // load driver
    }
    catch (Exception e) {
      System.err.println( "Failed to load the driver for the database: " + e.getMessage() );
      System.err.println( "Have you got the CLASSPATH set correctly?" );
    }
    File file = new File("vc-db-1.rdf");
    IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB_TYPE );
    ModelMaker maker = ModelFactory.createModelRDBMaker(conn);
    Model m = maker.createModel(MODEL_NAME);
    try{
    FileInputStream in = new FileInputStream(file);
    m.read(in,null);
    }
    catch(FileNotFoundException e)
    {
    System.out.println(e.getMessage());
    }
   
    Query query = QueryFactory.create(queryString);
    // Execute the query and obtain results
QueryExecution qe = QueryExecutionFactory.create(query, m);
ResultSet results = qe.execSelect();
    // Output query results
   ResultSetFormatter.out(System.out, results, query);
    // Important - free up resources used running the query
 qe.close();
}

}


浙ICP备06020153号-1