diff --git a/persistence-modules/apache-derby/pom.xml b/persistence-modules/apache-derby/pom.xml new file mode 100644 index 0000000000..7728bd4d8f --- /dev/null +++ b/persistence-modules/apache-derby/pom.xml @@ -0,0 +1,32 @@ + + + + persistence-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + apache-derby + + + + + org.apache.derby + derby + 10.13.1.1 + + + + + org.apache.derby + derbyclient + 10.13.1.1 + + + + + + \ No newline at end of file diff --git a/persistence-modules/apache-derby/src/main/java/com/baeldung/derby/DerbyApplicationDemo.java b/persistence-modules/apache-derby/src/main/java/com/baeldung/derby/DerbyApplicationDemo.java new file mode 100644 index 0000000000..927293558f --- /dev/null +++ b/persistence-modules/apache-derby/src/main/java/com/baeldung/derby/DerbyApplicationDemo.java @@ -0,0 +1,39 @@ +package com.baeldung.derby; + +import java.sql.*; + +/** + * Created by arash on 14.10.21. + */ + +public class DerbyApplicationDemo { + public static void main(String[] args) { + runner("jdbc:derby:baeldung;create=true"); + } + + private static void runner(String urlConnection) { + try { + Connection con = DriverManager.getConnection(urlConnection); + Statement statement = con.createStatement(); + if (!isTableExists("authors", con)) { + String createSQL = "CREATE TABLE authors (id INT PRIMARY KEY,first_name VARCHAR(255),last_name VARCHAR(255))"; + statement.execute(createSQL); + String insertSQL = "INSERT INTO authors VALUES (1, 'arash','ariani')"; + statement.execute(insertSQL); + } + String selectSQL = "SELECT * FROM authors"; + ResultSet result = statement.executeQuery(selectSQL); + while (result.next()) { + // use result here + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private static boolean isTableExists(String tableName, Connection connection) throws SQLException { + DatabaseMetaData meta = connection.getMetaData(); + ResultSet result = meta.getTables(null, null, tableName.toUpperCase(), null); + return result.next(); + } +} diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index a8f1d5c103..0373cc78ec 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -17,6 +17,7 @@ activejdbc apache-bookkeeper apache-cayenne + apache-derby core-java-persistence core-java-persistence-2 deltaspike