我在图形中有以下数据(命名为“ data.n3”或“ data.ttl”,我尝试了两个扩展名):-
# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ns: <http://example.org/ns#> .
<http://example/book1> ns:price 42 .
我尝试了以下2个代码段:-
代码1
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.rdfconnection.RDFConnectionFactory;
import org.apache.jena.system.Txn;
public class DataWriting {
public static void main(String[] args) {
Dataset ds = DatasetFactory.create();
try (RDFConnection conn = RDFConnectionFactory.connect(ds);) {
conn.load("data.ttl") ;
conn.update("PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" +
"INSERT DATA\r\n" +
"{ \r\n" +
" <http://example/book1> dc:title \"A new book\" ;\r\n" +
" dc:creator \"A.N.Other\" .\r\n" +
"}") ;
conn.put("data.n3");
conn.commit();
}) ;
}
}
}
代码2
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.jena.jdbc.mem.MemDriver;
public class DataWriting {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
MemDriver.register();
conn = DriverManager.getConnection("jdbc:jena:mem:dataset=data.ttl");
stmt = conn.createStatement();
stmt.executeUpdate("PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" +
"INSERT DATA\r\n" +
"{ \r\n" +
" <http://example/book1> dc:title \"A new book\" ;\r\n" +
" dc:creator \"A.N.Other\" .\r\n" +
"}");
} catch (SQLException e) {
System.err.println("SQL Error - " + e.getMessage());
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
期望文件“ data.n3”或“ data.ttl”中的数据变为以下内容:-
# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ns: <http://example.org/ns#> .
<http://example/book1> ns:price 42 .
<http://example/book1> dc:title "A new book" .
<http://example/book1> dc:creator "A.N.Other" .
但是,在上述两个Java程序中的任何一个执行之后,这实际上并没有发生。我是否缺少某些东西或做错了什么?