I am currently working on a software project that involves connecting to a Java database (Apache Derby) via a .Net C# application. I googled a lot and nothing helps. Finally, I decided to find my own one, and managed to get it up running. Here is my solution to share:
Tools we need:
- Java database engine JDBC driver, here I use Apache Derby, you can download from https://db.apache.org/derby/derby_downloads.html#Change+History
- IKVM.NET, you can download from https://www.nuget.org/packages/IKVM/
- Convert JAR to DLL
- Create a .Net Console application and reference DLLs as well as IKVM
- Test it out
using java.lang;
using java.sql;
using System;
using org.apache.derby.jdbc;
namespace DerbyDB
{
class Program
{
static void Main(string[] args)
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
DriverManager.registerDriver(new EmbeddedDriver());
var derbyDBConnection = DriverManager.getConnection("jdbc:derby:C:/DerbyDB");
if (derbyDBConnection != null)
{
Statement st = derbyDBConnection.createStatement();
ResultSet rs = st.executeQuery("select * from eventlog");
while (rs.next())
{
Console.WriteLine(rs.getInt("ID"));
Console.WriteLine(rs.getTime("EVENT_DATE"));
Console.WriteLine(rs.getString("EVENT_MESSAGE"));
}
st.close();
derbyDBConnection.close();
}
}
catch (java.lang.Exception e)
{
Console.WriteLine(e);
}
}
}
}