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