- Add the following dependencies to your build.gradle file:
compile group: 'org.apache.derby', name: 'derby', version: '10.15.1.3' compile group: 'org.apache.derby', name: 'derbyshared', version: '10.15.1.3'
- Add the following entries to your module-info.java file:
requires org.apache.derby.engine; requires org.apache.derby.commons; requires java.sql;
- In your Java class, you can create a connection like the following:
final String DATABASE_NAME = "sample_table"; String connectionURL = String.format("jdbc:derby:%s;create=true", DATABASE_NAME); connection = DriverManager.getConnection(connectionURL);
- Do your needed database operations with the connection (e.g. create table, execute a query, or call a stored procedure.).
- When your done using the connection, close the connection and shutdown the Derby engine like the following:
connection.close(); boolean gotSQLExc = false; try { //shutdown all databases and the Derby engine DriverManager.getConnection("jdbc:derby:;shutdown=true"); } catch (SQLException se) { if ( se.getSQLState().equals("XJ015") ) { gotSQLExc = true; } } if (!gotSQLExc) { System.out.println("Database did not shut down normally"); }
A clean shutdown always throws SQL exception XJ015, which can be ignored.
Leave a Reply