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);
}
}
}
}
Hi nick.
I’m trying your solution but I run into a problem.
I get un error on using org.apache.derby.jdbc.
Am I missing some references?
Could you help?
It seems you didn’t reference the DLLs correctly, and .Net application couldn’t find the assembly.
When you use IKVM, it will generate a bunch of DLLs.
I’m referencing all of them, I suppose:
https://photos.app.goo.gl/ZXpALran9YbWqGQeA
Any help?
Hi,
I have created a demo project, and it has been tested.
Here is the link DerbyTest.
Give it a try and let me know any question.
Thanks for your trouble.
I’ve tested it (error on DriverManager.getConnection(“jdbc:derby:C:/TempRH_DB”)):
java.sql.SQLException: No suitable driver found for jdbc:derby:C:/TempRH_DB
Any ideas?
I think it’s your derby database version. Which version are you using?
The demo I created is using 10.13.1.1.
If you use different version, you may need to re-generate all the DLLs by yourself.