Created
September 14, 2015 14:49
-
-
Save johnllao/fcdc9565b04e9913ff7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hello.derby; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
public class HelloDerby { | |
public static void main(String[] args) { | |
System.out.println("Hello Derby (version 1.0.0)"); | |
System.out.println("(c) hello.org 2015"); | |
try { | |
String connectionUrl = "jdbc:derby:memory:db;"; | |
Connection connection1 = DriverManager.getConnection(connectionUrl + "create=true;"); | |
connection1.prepareStatement("create table settings (name varchar(20), value varchar(50))").execute(); | |
connection1.prepareStatement("insert into settings (name, value) values ('appName', 'Hello Derby') ").execute(); | |
connection1.close(); | |
Connection connection2 = DriverManager.getConnection(connectionUrl + "create=false;"); | |
PreparedStatement query = connection2.prepareStatement("select * from settings"); | |
ResultSet resultSet = query.executeQuery(); | |
while(resultSet.next()) { | |
System.out.print("Key: "); | |
System.out.print(resultSet.getString(1)); | |
System.out.print(", Value: "); | |
System.out.print(resultSet.getString(2)); | |
} | |
System.out.println(); | |
connection2.close(); | |
} | |
catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
finally { | |
System.out.println("Bye!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment