Showing posts with label h2 database. Show all posts
Showing posts with label h2 database. Show all posts

Thursday, 16 June 2011

How to setup H2 database for database connections in java?

Download H2 database
Download H2 database from - H2 download, I downloaded the platform independent version. Extract the zip to some directory.
Run the H2 engine
Now migrate to bin extracted folder, and go to bin folder. Double click on h2.bat and the database engine starts.
Add the jars 
Add H2 jar present in the same bin folder to the java project and add following class to the project and run it.

Now test the code:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class H2Test
{
public static void main( String args[] ) throws Exception
{
Class.forName( "org.h2.Driver" );
Connection conn = null;
Statement stmt = null;

try
{
conn = DriverManager.getConnection( "jdbc:h2:file:db/test", "sa", "" );
stmt = conn.createStatement();
stmt.executeUpdate( "create table person(name varchar(10));" );
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
if( stmt != null )
{
try
{
stmt.close();
System.out.println("Test done");
}
catch( Exception e )
{
e.printStackTrace();
}
}

if( conn != null )
{
conn.close();
}

}

}
}


List of JDBC drivers for Java

If you need to access a database with Java, you need a driver. This is a list of the drivers available, what database they can access, who makes it, and how to contact them.

IBM DB2
jdbc:db2://HOST:PORT/DB
COM.ibm.db2.jdbc.app.DB2Driver

JDBC-ODBC Bridge
jdbc:odbc:DB
sun.jdbc.odbc.JdbcOdbcDriver

Microsoft SQL Server
jdbc:weblogic:mssqlserver4:DB@HOST:PORT
weblogic.jdbc.mssqlserver4.Driver

Oracle Thin
jdbc:oracle:thin:@HOST:PORT:SID
oracle.jdbc.driver.OracleDriver

PointBase Embedded Server
jdbc:pointbase://embedded:PORT/DB
com.pointbase.jdbc.jdbcUniversalDriver

Cloudscape
jdbc:cloudscape:DB
COM.cloudscape.core.JDBCDriver

Cloudscape RMI
jdbc:rmi://HOST:PORT/jdbc:cloudscape:DB
RmiJdbc.RJDriver

Firebird (JCA/JDBC Driver)
jdbc:firebirdsql://HOST:PORT/DB
org.firebirdsql.jdbc.FBDriver

Informix Dynamic Server
jdbc:informix-sqli://HOST:PORT/DB:INFORMIXSERVER=SERVER_NAME
com.informix.jdbc.IfxDriver

Hypersonic SQL (v1.2 and earlier)
jdbc:HypersonicSQL:DB
hSql.hDriver

Hypersonic SQL (v1.3 and later)
jdbc:HypersonicSQL:DB
org.hsql.jdbcDriver

Microsoft SQL Server (JTurbo Driver)
jdbc:JTurbo://HOST:PORT/DB
com.ashna.jturbo.driver.Driver

Microsoft SQL Server (Sprinta Driver)
jdbc:inetdae:HOST:PORT?database=DB
com.inet.tds.TdsDriver

Microsoft SQL Server 2000 (Microsoft Driver)
jdbc:microsoft:sqlserver://HOST:PORT;DatabaseName=DB
com.microsoft.sqlserver.jdbc.SQLServerDriver

MySQL (MM.MySQL Driver)
jdbc:mysql://HOST:PORT/DB
org.gjt.mm.mysql.Driver

Oracle OCI 8i
jdbc:oracle:oci8:@SID
oracle.jdbc.driver.OracleDriver

Oracle OCI 9i
jdbc:oracle:oci:@SID
oracle.jdbc.driver.OracleDriver

H2 Database (in memory type database)
jdbc:h2:file:db/test
org.h2.Driver

To test your driver once it's installed, try the following java code:

try{
Class.forName("Driver name");
Connection con = DriverManager.getConnenction("jdbcurl","username","password");
//other manipulation using jdbc commands
}
catch(Exception e){
}