If you want to store metadata in a database instead of in memory, you can use PersistentJenaContext. It requires the following properties:

dbUrl – the JDBC URL of the database
dbUser – the username to connect to the database
dbPasswd – the password to connect to the database
dbType – the kind of database (e.g., "MySQL", "PostgreSQL", "Oracle")
dbDriverClass – the class of the JDBC driver
modelName - the name of the Jena model

If you're creating a new Jena model in an empty database or database schema, you may need to create that database or schema using your database's administration tools, before instantiating PersistentJenaContext and calling initialize.

If you're connecting to an existing Jena model (e.g., one that was created using Jena-based code and not Tupelo), make sure the JDBC URL and model name correspond to the Jena configuration that was used to create it.

The following example will create a Jena model on an existing MySQL database, write a triple to it, and read it back.

        // instantiate a persistent Jena context
        PersistentJenaContext pjc = new PersistentJenaContext();
 
        // initialize the required fields and open the context on
        // an existing mysql database
        pjc.setType( PersistentJenaContext.MYSQL_TYPE );
        pjc.setUrl( "jdbc:mysql://localhost/tupelo" );
        pjc.setUser( "tupelo" );
        pjc.setPassword( "aPassword" );
        pjc.setModelName( "tupeloTest" );
        pjc.setDriverClass( "com.mysql.jdbc.Driver" );
        pjc.initialize(); // model doesn't exist, so create it
 
        // write a triple to the context
        pjc.open();
        Resource subject = Resource.uriRef( "http://test/subject" );
        Resource predicate = Resource.uriRef( "http://test/hasName" );
        Resource object = Resource.literal( "Jane Doe" );
        pjc.addTriple( subject, predicate, object );
 
        // read all triples
        Set<Triple> t = pjc.getTriples();
        System.out.println( "Triples:" );
        for (Triple t1 : t) {
            System.out.println( t1 );
        }
 
        pjc.close();
  • No labels