Studyon Minte9.com
Jdbc




H2 db embeded



This database supports the in-memory mode, where the data is not persisted (example: read-only database). 

In this case, the database URL is jdbc:h2:mem:db1

Put the h2.jar file on extensions folder

C:Program Files/Java/jdk1.6.0_37/jre/lib/ext/

package testexample; import java.awt.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.*; public class Test extends JFrame { public static void main(String[] args) throws Exception { Test frame = new Test(); frame.setBounds(200, 200, 300, 200); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() throws Exception { Class.forName("org.h2.Driver"); // --- Look Here --- // Connection con = DriverManager.getConnection("jdbc:h2:mem:data/h2/test", "test", "" ); Statement stmt = con.createStatement(); stmt.executeUpdate( "CREATE TABLE IF NOT EXISTS teams ( name varchar(50) )" ); stmt.executeUpdate( "INSERT INTO teams ( name ) VALUES ( 'Fiorentina' )" ); stmt.executeUpdate( "INSERT INTO teams ( name ) VALUES ( 'Juventus' )" ); ResultSet rs = stmt.executeQuery("SELECT * FROM teams"); String strOutput = ""; while( rs.next() ) { strOutput += rs.getString("name") + ", "; // --- Look Here --- // } stmt.close(); con.close(); JLabel label = new JLabel(strOutput); // Outputs: Fiorentina, Juventus JPanel panel = new JPanel(); panel.add(label); panel.setForeground(Color.red); add(panel); } }
  H2 to be contain in the jar file: (see ... Netreans / App embeded jar) 1) Netbeans / testexample / Libraries > add JAR/Folder (h2.jar) 2) Neatbeans, switch to file model, change build.xml 3) build.xml (right click) Run Target Other Targets -> package-for-store D:developments/testexample/store ... testexample.jar


http://www.h2database.com/html/features.html#in_memory_databases
http://www.javatips.net/blog/2014/07/h2-in-memory-database-example