UCanAccess、IKVM 和 C#

UCanAccess, IKVM and C#

我正在尝试 UCanAccess work in C#. I'm working on MonoDevelop using Mono on Linux. I already converted the needed .jar files into .dll .NET files using IKVM,这样:

ikvmc -target:library file.jar

然后我在我的C#项目中引用了转换后的.dll文件。所以,这是我引用的所有 DLL:

我还放了 ucanaccess-3.0.3.1.jar、hsqldb.jar、jackcess-2.1.3.jar、commons-logging-1.1.1.jar 和 commons- lang-2.6.jar 到我的 bin/Debug 文件夹中。

这是我在 Main 中编写的 C# 代码:

//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("commons-lang-2.6"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("commons-logging-1.1.1"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("jackcess-2.1.3"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("hsqldb"));
ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("ucanaccess-3.0.3.1"));

java.sql.Driver v_driver = (java.sql.Driver) java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").newInstance();
java.sql.Connection v_con = v_driver.connect("northwind.mdb", null);

//java.lang.Class.forName("org.hsqldb.jdbcDriver, hsqldb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
//java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
//java.sql.Connection v_con = java.sql.DriverManager.getConnection("jdbc:ucanaccess://northwind.mdb");

java.sql.Statement v_st = v_con.createStatement();

java.sql.ResultSet v_res = v_st.executeQuery("select * from Categories");
java.sql.ResultSetMetaData v_resmd = v_res.getMetaData();

for (int i = 0; i < v_resmd.getColumnCount(); i++)
    Console.Write(v_resmd.getColumnLabel(i) + "|");
Console.WriteLine();

while (v_res.next())
{
    for (int i = 0; i < v_resmd.getColumnCount(); i++)
        Console.Write(v_res.getString(i) + "|");
    Console.WriteLine();
}

代码编译成功,当我尝试执行时,出现以下错误:

Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for net.ucanaccess.jdbc.UcanaccessDriver ---> java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00000] in <filename unknown>:0 
at IKVM.Internal.TypeWrapper.RunClassInit () [0x00000] in <filename unknown>:0 
at IKVM.NativeCode.java.lang.Class.forName0 (System.String name, Boolean initialize, java.lang.ClassLoader loader) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName0 (System.String , Boolean , java.lang.ClassLoader ) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName (System.String className, ikvm.internal.CallerID ) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName (System.String className) [0x00000] in <filename unknown>:0 
at Test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for net.ucanaccess.jdbc.UcanaccessDriver ---> java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00000] in <filename unknown>:0 
at IKVM.Internal.TypeWrapper.RunClassInit () [0x00000] in <filename unknown>:0 
at IKVM.NativeCode.java.lang.Class.forName0 (System.String name, Boolean initialize, java.lang.ClassLoader loader) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName0 (System.String , Boolean , java.lang.ClassLoader ) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName (System.String className, ikvm.internal.CallerID ) [0x00000] in <filename unknown>:0 
at java.lang.Class.forName (System.String className) [0x00000] in <filename unknown>:0 
at Test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

注释行我已经试过了,没有成功。

我错过了什么吗?提前致谢!

编辑:

@jamadei 是对的,我将所有 jar 合并到一个 jar 中并且成功了! 这是工作代码(注意列索引从 1 开始,而不是从 0 开始):

ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("ucanaccess-3.0.3.1"));
java.sql.Driver v_driver = (java.sql.Driver) java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").newInstance();

java.sql.Connection v_con = v_driver.connect("northwind.mdb", null);
java.sql.Statement v_st = v_con.createStatement();

java.sql.ResultSet v_res = v_st.executeQuery("select categoryid, categoryname, description from Categories");
java.sql.ResultSetMetaData v_resmd = v_res.getMetaData();

for (int i = 1; i <= v_resmd.getColumnCount(); i++)
    Console.Write(v_resmd.getColumnLabel(i) + "|");
Console.WriteLine();

while (v_res.next())
{
    for (int i = 1; i <= v_resmd.getColumnCount(); i++)
        Console.Write(v_res.getString(i) + "|");
    Console.WriteLine();
}

您需要将所有jar 合并为一个jar,然后将其转换为dll。