gpt4 book ai didi

c# - UCanAccess、IKVM 和 C#

转载 作者:行者123 更新时间:2023-11-30 23:29:13 24 4
gpt4 key购买 nike

我正在尝试制作 UCanAccess在 C# 中工作。我正在 Linux 上使用 Mono 开发 MonoDevelop。我已经使用 IKVM 将所需的 .jar 文件转换为 .dll .NET 文件,这样:

ikvmc -target:library file.jar

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

  • ucanaccess-3.0.3.1.dll
  • hsqldb.dll
  • jackcess-2.1.3.dll
  • commons-logging-1.1.1.dll
  • commons-lang-2.6.dll
  • IKVM.OpenJDK.Core.dll
  • IKVM.OpenJDK.Jdbc.dll
  • IKVM.Runtime

我还将 ucanacess-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。

关于c# - UCanAccess、IKVM 和 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35602606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com