Jena API Usage Details (focus on the operations after the ontology is persisted to MySQL and Chinese garbled characters)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.