1) H2 file structure:
--------------------------------
-H2
-Bin
H2-1.1.116.jar // H2 main file (the driver is also inside)
H2.bat // H2 console Startup Program with black screen window (for Windows)
H2.sh // H2 console Startup Program (for Linux)
H2w. bat // H2 console startup program without a black screen window (for Windows)
+ Docs // document folder (with manual in it)
+ Service
+ Src // is it open-source with source code?
Build. bat
Build. sh
Build. xml
--------------------------------
(2) introduce the H2 driver in Eclipse/MyEclipse
Right-click the specified project-> Peoperties-> Java Build Path-> Libraries-> Add External JARs-> introduce h2-1.1.116.jar
(3) Use the memory mode (after the function is disabled, all content disappears and the speed is very fast ):
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Public class MenTest {
Public void runInsertDelete (){
Try {
String sourceURL = "jdbc: h2: tcp: // localhost/mem: testmemdb"; // H2DB mem mode
String user = "sa ";
String key = "";
Try {
Class. forName ("org. h2.Driver"); // HSQLDB Driver
} Catch (Exception e ){
E. printStackTrace ();
}
Connection conn = DriverManager. getConnection (sourceURL, user, key); // put the driver into the Connection
Statement stmt = conn. createStatement ();
// Create a Statement object to send SQL statements to the database.
// Stmt.exe cuteUpdate ("delete from mytable WHERE name = \ 'No. 2 \'");
// Find a method with the same name as the methodName attribute and call this method on the target.
// Stmt.exe cute ("create table idtable (id INT, name VARCHAR (100 ));");
Stmt.exe cute ("insert into idtable VALUES (1, \ 'musoft \')");
Stmt.exe cute ("insert into idtable VALUES (2, \ 'steven Stander \')");
Stmt. close ();
Conn. close ();
} Catch (SQLException sqle ){
System. out. println ("SQL ERROR! ");
}
}
Public void query (String SQL ){
Try {
String sourceURL = "jdbc: h2: tcp: // localhost/mem: testmemdb ";
String user = "sa ";
String key = "";
Try {
Class. forName ("org. h2.Driver ");
} Catch (Exception e ){
E. printStackTrace ();
}
Connection conn = DriverManager. getConnection (sourceURL, user, key); // put the driver into the Connection
Statement stmt = conn. createStatement (); // create a Statement object to send SQL statements to the database.
ResultSet rset1_stmt.exe cuteQuery (SQL); // find a method with the same name as the methodName attribute and call this method on the target.
While (rset. next ())
{
System. out. println (rset. getInt ("id") + "+ rset. getString (" name "));
}
Rset. close ();
Stmt. close ();
Conn. close ();
} Catch (SQLException sqle ){
System. err. println (sqle );
}
}
Public static void main (String args []) {
MenTest mt = new MenTest ();
Mt. runInsertDelete ();
Mt. query ("SELECT * FROM idtable ");
}
}
(4) use the Embedded mode (Create, Insert, Delete ...):
Import java. SQL .*;
Public class UpdateTest {
Public void runInsertDelete (){
Try {
String sourceURL = "jdbc: h2: h2/bin/mydb"; // H2 database
String user = "sa ";
String key = "";
Try {
Class. forName ("org. h2.Driver"); // H2 Driver
} Catch (Exception e ){
E. printStackTrace ();
}
Connection conn = DriverManager. getConnection (sourceURL, user, key );
Statement stmt = conn. createStatement ();
Stmt.exe cute ("create table mytable (name VARCHAR (100), sex VARCHAR (10 ))");
Stmt.exe cuteUpdate ("insert into mytable VALUES ('steven stander', 'male ')");
Stmt.exe cuteUpdate ("insert into mytable VALUES ('Elizabeth Ames ', 'female ')");
Stmt.exe cuteUpdate ("delete from mytable WHERE sex = \ 'male \'");
Stmt. close ();
Conn. close ();
} Catch (SQLException sqle ){
System. err. println (sqle );
}
}
Public static void main (String args []) {
New UpdateTest (). runInsertDelete ();
}
}
(5) use the Embedded mode (Select ):
Import java. SQL .*;
Public class SelectTest {
Public void query (String SQL ){
Try {
String sourceURL = "jdbc: h2: h2/bin/mydb ";
String user = "sa ";
String key = "";
Try {
Class. forName ("org. h2.Driver ");
} Catch (Exception e ){
E. printStackTrace ();
}
Connection conn = DriverManager. getConnection (sourceURL, user, key );
Statement stmt = conn. createStatement ();
ResultSet rset = stmt.exe cuteQuery (SQL );
While (rset. next ()){
System. out. println (rset. getString ("name") + "+ rset. getString (" sex "));
}
Rset. close ();
Stmt. close ();
Conn. close ();
} Catch (SQLException sqle ){
System. err. println (sqle );
}
}
Public static void main (String args []) {
Test tt = new Test ();
Tt. query ("select * from mytable ");
}
}