Running H2 in C#

2,451 views
Skip to first unread message

JonoPorter

unread,
Jan 30, 2008, 12:33:24 PM1/30/08
to H2 Database
I managed to get H2 running in C# using IKVM.NET. no porting needed.

The thing is that it's so simple to do.

Just download IKVM.NET and H2.

Then run ikvmc.exe on the h2.jar and get the h2.dll. This converts
java byte code to MSIL.

Then add h2.dll and IKVM.OpenJDK.ClassLibrary.dll to a C# solution.

Start using!

Here is a sample program that ran on my computer:

using System;
using java.sql;

namespace H2Sharp
{
class Program
{
static void Main(string[] args)
{
org.h2.Driver.load();
Connection conn = DriverManager.getConnection("jdbc:h2:~/
test", "sa", "");

PreparedStatement statement =
conn.prepareStatement("CREATE TABLE bob(key int,value
varchar(200));");
statement.execute();

PreparedStatement statement2 =
conn.prepareStatement("INSERT INTO bob(key,value) VALUES(7,'HA7');");
statement2.execute();
PreparedStatement statement3 =
conn.prepareStatement("SELECT * FROM bob;");

ResultSet set = statement3.executeQuery();
while (set.next())
{
Console.WriteLine("Key: {0}, Value: {1}",
set.getInt(1), set.getNString(2));
}
Console.WriteLine("Done");
Console.ReadLine();
}
}
}

Thomas Mueller

unread,
Jan 30, 2008, 4:26:32 PM1/30/08
to h2-da...@googlegroups.com
Hi,

That's great! I had IKVM on my todo list, but never found the time to
test it. Now I have downloaded it and it works! I didn't expect it to
be that easy. Even the Console works; unfortunately it throws an
exception when starting up (in the AWT code). But it still works. Only
the URL is not shown. I will implement a workaround: if there is an
exception in frame.setVisible(true), then the URL will be printed to
the screen (System.out.println), and the exception is ignored. Like
this the browser window opens as well. This will be included in the
next release. I hope I can also document this a bit.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages