Hi,
Here a very useful library H2, the Java SQL database http://www.h2database.com/html/main.html.
The main features of H2 are:
- Very fast, open source, JDBC API
- Embedded and server modes; in-memory databases
- Browser based Console application
- Small footprint: around 1.5 MB jar file size
Features:
- Pure Java
- Memory Mode
- Encrypted Database
- ODBC Driver
- Fulltext Search
- Multi Version Concurrency
Examples:
Examples with the h2-1.4.192.jar and JDK 1.70_72
H2FileDatabaseExample
001 | package com.huo.javablog.h2.tests; |
004 | import java.sql.Connection; |
005 | import java.sql.DriverManager; |
006 | import java.sql.PreparedStatement; |
007 | import java.sql.ResultSet; |
008 | import java.sql.SQLException; |
009 | import java.sql.Statement; |
012 | import org.h2.tools.DeleteDbFiles; |
016 | public class H2FileDatabaseExample { |
018 | private static final String DB_DRIVER = "org.h2.Driver" ; |
019 | private static final String DB_CONNECTION = "jdbc:h2:~/test" ; |
020 | private static final String DB_USER = "" ; |
021 | private static final String DB_PASSWORD = "" ; |
023 | public static void main(String[] args) throws Exception { |
026 | DeleteDbFiles.execute( "~" , "test" , true ); |
027 | insertWithStatement(); |
028 | DeleteDbFiles.execute( "~" , "test" , true ); |
029 | insertWithPreparedStatement(); |
031 | } catch (SQLException e) { |
037 | private static void insertWithPreparedStatement() throws SQLException { |
038 | Connection connection = getDBConnection(); |
039 | PreparedStatement createPreparedStatement = null ; |
040 | PreparedStatement insertPreparedStatement = null ; |
041 | PreparedStatement selectPreparedStatement = null ; |
043 | String CreateQuery = "CREATE TABLE PERSON(id int primary key, name varchar(255))" ; |
044 | String InsertQuery = "INSERT INTO PERSON" + "(id, name) values" + "(?,?)" ; |
045 | String SelectQuery = "select * from PERSON" ; |
047 | connection.setAutoCommit( false ); |
049 | createPreparedStatement = connection.prepareStatement(CreateQuery); |
050 | createPreparedStatement.executeUpdate(); |
051 | createPreparedStatement.close(); |
053 | insertPreparedStatement = connection.prepareStatement(InsertQuery); |
054 | insertPreparedStatement.setInt( 1 , 1 ); |
055 | insertPreparedStatement.setString( 2 , "Jose" ); |
056 | insertPreparedStatement.executeUpdate(); |
057 | insertPreparedStatement.close(); |
059 | selectPreparedStatement = connection.prepareStatement(SelectQuery); |
060 | ResultSet rs = selectPreparedStatement.executeQuery(); |
061 | System.out.println( "H2 Database inserted through PreparedStatement" ); |
063 | System.out.println( "Id=" +rs.getInt( "id" )+ " - Name=" +rs.getString( "name" )); |
065 | selectPreparedStatement.close(); |
068 | } catch (SQLException e) { |
069 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
070 | } catch (Exception e) { |
078 | private static void insertWithStatement() throws SQLException { |
079 | Connection connection = getDBConnection(); |
080 | Statement stmt = null ; |
082 | connection.setAutoCommit( false ); |
083 | stmt = connection.createStatement(); |
084 | stmt.execute( "CREATE TABLE PERSON(id int primary key, name varchar(255))" ); |
085 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(1, 'Anju')" ); |
086 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(2, 'Sonia')" ); |
087 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(3, 'Asha')" ); |
089 | ResultSet rs = stmt.executeQuery( "select * from PERSON" ); |
090 | System.out.println( "H2 Database inserted through Statement" ); |
092 | System.out.println( "Id=" +rs.getInt( "id" )+ " - Name=" +rs.getString( "name" )); |
096 | } catch (SQLException e) { |
097 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
098 | } catch (Exception e) { |
105 | private static Connection getDBConnection() { |
106 | Connection dbConnection = null ; |
108 | Class.forName(DB_DRIVER); |
109 | } catch (ClassNotFoundException e) { |
110 | System.out.println(e.getMessage()); |
113 | dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, |
116 | } catch (SQLException e) { |
117 | System.out.println(e.getMessage()); |
H2MemoryDatabaseExample
001 | package com.huo.javablog.h2.tests; |
004 | import java.sql.Connection; |
005 | import java.sql.DriverManager; |
006 | import java.sql.PreparedStatement; |
007 | import java.sql.ResultSet; |
008 | import java.sql.SQLException; |
009 | import java.sql.Statement; |
013 | public class H2MemoryDatabaseExample { |
015 | private static final String DB_DRIVER = "org.h2.Driver" ; |
016 | private static final String DB_CONNECTION = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" ; |
017 | private static final String DB_USER = "" ; |
018 | private static final String DB_PASSWORD = "" ; |
020 | public static void main(String[] args) throws Exception { |
022 | insertWithStatement(); |
023 | insertWithPreparedStatement(); |
024 | } catch (SQLException e) { |
029 | private static void insertWithPreparedStatement() throws SQLException { |
030 | Connection connection = getDBConnection(); |
031 | PreparedStatement createPreparedStatement = null ; |
032 | PreparedStatement insertPreparedStatement = null ; |
033 | PreparedStatement selectPreparedStatement = null ; |
035 | String CreateQuery = "CREATE TABLE PERSON(id int primary key, name varchar(255))" ; |
036 | String InsertQuery = "INSERT INTO PERSON" + "(id, name) values" + "(?,?)" ; |
037 | String SelectQuery = "select * from PERSON" ; |
040 | connection.setAutoCommit( false ); |
042 | createPreparedStatement = connection.prepareStatement(CreateQuery); |
043 | createPreparedStatement.executeUpdate(); |
044 | createPreparedStatement.close(); |
046 | insertPreparedStatement = connection.prepareStatement(InsertQuery); |
047 | insertPreparedStatement.setInt( 1 , 1 ); |
048 | insertPreparedStatement.setString( 2 , "Jose" ); |
049 | insertPreparedStatement.executeUpdate(); |
050 | insertPreparedStatement.close(); |
052 | selectPreparedStatement = connection.prepareStatement(SelectQuery); |
053 | ResultSet rs = selectPreparedStatement.executeQuery(); |
054 | System.out.println( "H2 In-Memory Database inserted through PreparedStatement" ); |
056 | System.out.println( "Id=" + rs.getInt( "id" ) + " - Name=" + rs.getString( "name" )); |
058 | selectPreparedStatement.close(); |
061 | } catch (SQLException e) { |
062 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
063 | } catch (Exception e) { |
070 | private static void insertWithStatement() throws SQLException { |
071 | Connection connection = getDBConnection(); |
072 | Statement stmt = null ; |
074 | connection.setAutoCommit( false ); |
075 | stmt = connection.createStatement(); |
076 | stmt.execute( "CREATE TABLE PERSON(id int primary key, name varchar(255))" ); |
077 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(1, 'Anju')" ); |
078 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(2, 'Sonia')" ); |
079 | stmt.execute( "INSERT INTO PERSON(id, name) VALUES(3, 'Asha')" ); |
081 | ResultSet rs = stmt.executeQuery( "select * from PERSON" ); |
082 | System.out.println( "H2 In-Memory Database inserted through Statement" ); |
084 | System.out.println( "Id=" + rs.getInt( "id" ) + " - Name=" + rs.getString( "name" )); |
087 | stmt.execute( "DROP TABLE PERSON" ); |
090 | } catch (SQLException e) { |
091 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
092 | } catch (Exception e) { |
099 | private static Connection getDBConnection() { |
100 | Connection dbConnection = null ; |
102 | Class.forName(DB_DRIVER); |
103 | } catch (ClassNotFoundException e) { |
104 | System.out.println(e.getMessage()); |
107 | dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); |
109 | } catch (SQLException e) { |
110 | System.out.println(e.getMessage()); |
H2CsvMemoryDatabaseExample
001 | package com.huo.javablog.h2.tests; |
005 | import java.sql.Connection; |
006 | import java.sql.DriverManager; |
007 | import java.sql.PreparedStatement; |
008 | import java.sql.ResultSet; |
009 | import java.sql.SQLException; |
013 | public class H2CsvMemoryDatabaseExample { |
015 | private static final String DB_DRIVER = "org.h2.Driver" ; |
016 | private static final String DB_CONNECTION = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" ; |
017 | private static final String DB_USER = "" ; |
018 | private static final String DB_PASSWORD = "" ; |
020 | public static void main(String[] args) throws Exception { |
022 | loadAndInsertWithPreparedStatement(); |
023 | writeToCsvWithNewConnection(); |
024 | } catch (SQLException e) { |
029 | private static void loadAndInsertWithPreparedStatement() throws SQLException { |
030 | Connection connection = getDBConnection(); |
031 | PreparedStatement createAndLoadPreparedStatement = null ; |
032 | PreparedStatement insertPreparedStatement = null ; |
033 | PreparedStatement selectPreparedStatement = null ; |
035 | String CreateAndLoadQuery = "CREATE TABLE PERSON(id int primary key, name varchar(255), surname varchar(255)) " |
036 | + " AS SELECT * FROM CSVREAD('C:/HOMEWARE/projects/MyFirstProjects/H2Test/src/com/huo/javablog/h2/tests/people.csv')" ; |
040 | String InsertQuery = "INSERT INTO PERSON" + "(id, name, surname) values" + "(?,?,?)" ; |
041 | String SelectQuery = "select * from PERSON" ; |
044 | connection.setAutoCommit( false ); |
046 | createAndLoadPreparedStatement = connection.prepareStatement(CreateAndLoadQuery); |
047 | createAndLoadPreparedStatement.executeUpdate(); |
048 | createAndLoadPreparedStatement.close(); |
050 | insertPreparedStatement = connection.prepareStatement(InsertQuery); |
051 | insertPreparedStatement.setInt( 1 , 12 ); |
052 | insertPreparedStatement.setString( 2 , "Huseyin" ); |
053 | insertPreparedStatement.setString( 3 , "OZVEREN" ); |
054 | insertPreparedStatement.executeUpdate(); |
055 | insertPreparedStatement.close(); |
057 | selectPreparedStatement = connection.prepareStatement(SelectQuery); |
058 | ResultSet rs = selectPreparedStatement.executeQuery(); |
059 | System.out.println( "H2 CSV In-Memory Database inserted through PreparedStatement" ); |
061 | System.out.println( "Id=" + rs.getInt( "id" ) + " - Name=" + rs.getString( "name" ) + " - Surname=" + rs.getString( "surname" )); |
063 | selectPreparedStatement.close(); |
066 | } catch (SQLException e) { |
067 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
068 | } catch (Exception e) { |
076 | private static void writeToCsvWithNewConnection() throws SQLException { |
077 | Connection connection = getDBConnection(); |
078 | PreparedStatement writePreparedStatement = null ; |
079 | PreparedStatement selectPreparedStatement = null ; |
081 | String WriteCsvQuery = "CALL CSVWRITE('C:/HOMEWARE/projects/MyFirstProjects/H2Test/src/com/huo/javablog/h2/tests/peopleWritten.csv', 'SELECT * FROM PERSON');" ; |
082 | String SelectQuery = "select * from PERSON" ; |
085 | connection.setAutoCommit( false ); |
087 | writePreparedStatement = connection.prepareStatement(WriteCsvQuery); |
088 | writePreparedStatement.executeUpdate(); |
089 | writePreparedStatement.close(); |
091 | selectPreparedStatement = connection.prepareStatement(SelectQuery); |
092 | ResultSet rs = selectPreparedStatement.executeQuery(); |
093 | System.out.println( "H2 CSV In-Memory Database inserted through PreparedStatement" ); |
095 | System.out.println( "Id=" + rs.getInt( "id" ) + " - Name=" + rs.getString( "name" ) + " - Surname=" + rs.getString( "surname" )); |
097 | selectPreparedStatement.close(); |
100 | } catch (SQLException e) { |
101 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
102 | } catch (Exception e) { |
109 | private static Connection getDBConnection() { |
110 | Connection dbConnection = null ; |
112 | Class.forName(DB_DRIVER); |
113 | } catch (ClassNotFoundException e) { |
114 | System.out.println(e.getMessage()); |
117 | dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); |
119 | } catch (SQLException e) { |
120 | System.out.println(e.getMessage()); |
people.csv
id,name,surname
1,Anju,Johnson
2,Sonia,Paulson
3,Asha,Huseyinson
peopleWritten.csv
"ID","NAME","SURNAME"
"1","Anju","Johnson"
"2","Sonia","Paulson"
"3","Asha","Huseyinson"
"12","Huseyin","OZVEREN"
H2CsvMemoryNoDatabaseExample
01 | package com.huo.javablog.h2.tests; |
04 | import java.sql.ResultSet; |
05 | import java.sql.ResultSetMetaData; |
06 | import java.sql.SQLException; |
09 | import org.h2.tools.Csv; |
10 | import org.h2.tools.SimpleResultSet; |
14 | public class H2CsvMemoryNoDatabaseExample { |
16 | public static void main(String[] args) throws Exception { |
18 | loadAndWriteCsvWithoutDB(); |
19 | } catch (SQLException e) { |
24 | private static void loadAndWriteCsvWithoutDB() throws SQLException { |
28 | ResultSet rs = new Csv().read( "C:/HOMEWARE/projects/MyFirstProjects/H2Test/src/com/huo/javablog/h2/tests/people.csv" , null , null ); |
29 | ResultSetMetaData meta = rs.getMetaData(); |
31 | for ( int i = 0 ; i < meta.getColumnCount(); i++) { |
32 | System.out.println( meta.getColumnLabel(i + 1 ) + ": " + rs.getString(i + 1 )); |
38 | System.out.println( "----------------" ); |
41 | SimpleResultSet rsIns = new SimpleResultSet(); |
42 | rsIns.addColumn( "id" , Types.INTEGER, 10 , 0 ); |
43 | rsIns.addColumn( "name" , Types.VARCHAR, 255 , 0 ); |
44 | rsIns.addColumn( "surname" , Types.VARCHAR, 255 , 0 ); |
45 | rsIns.addRow( "13" , "Huseyin13" , "OZVEREN13" ); |
46 | rsIns.addRow( "14" , "Huseyin14" , "OZVEREN14" ); |
47 | new Csv().write( "C:/HOMEWARE/projects/MyFirstProjects/H2Test/src/com/huo/javablog/h2/tests/people3.csv" , rsIns, null ); |
50 | ResultSet rs = new Csv().read( "C:/HOMEWARE/projects/MyFirstProjects/H2Test/src/com/huo/javablog/h2/tests/people3.csv" , null , null ); |
51 | ResultSetMetaData meta = rs.getMetaData(); |
53 | for ( int i = 0 ; i < meta.getColumnCount(); i++) { |
54 | System.out.println( meta.getColumnLabel(i + 1 ) + ": " + rs.getString(i + 1 )); |
61 | } catch (SQLException e) { |
62 | System.out.println( "Exception Message " + e.getLocalizedMessage()); |
63 | } catch (Exception e) { |
people3.csv
"id","name","surname"
"13","Huseyin13","OZVEREN13"
"14","Huseyin14","OZVEREN14"
That’s all!!!
Huseyin OZVEREN
Related