Introduction to Jena can be seen in many blogs, such as a simple understanding of Jena and an example. Jena is used to store the ontology into MySQL and Jena. When getting started, I am always confused when I read these articles and have no idea about the operations after I save them to the database. Therefore, after completing the configuration, record the methods used for future reference. To persist an ontology to a database, the first thing we can do is to avoid vulgarity. Let's talk about how to persist the ontology to a database. The general operation process is to read the ontology from the owl file, establish a database connection, and store the Model into the database. This process is relatively simple. See the following code.
private final static String driver = "com.mysql.jdbc.Driver"; private final static String url = "jdbc:mysql://localhost/dbname"; private final static String db = "MySQL"; private final static String user = "your user name"; private final static String pwd = "your password"; /** * @param args */ public static void main(String[] args) { try { IDBConnection con = getConnection(url, user, pwd, db); Class.forName(driver); String path = "your owl file path"; createModel(con, "model name", path); con.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * get db connection * * @param dbUrl * @param dbUser * @param dbPwd * @param dbName * @return */ public static DBConnection getConnection(String dbUrl, String dbUser, String dbPwd, String dbName) { return new DBConnection(dbUrl, dbUser, dbPwd, dbName); } /** * read owl file, create the ontModel, and store in db * * @param conn * @param name * @param filePath * @return */ public static OntModel createModel(IDBConnection conn, String name, String filePath) { ModelMaker maker = ModelFactory.createModelRDBMaker(conn); Model model = maker.createModel(name); try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); model.read(isr, null); isr.close(); fis.close(); model.commit(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); return ModelFactory.createOntologyModel(spec, model); }
After executing the code above, we can see seven tables in the MySQL database.