I want to connect and access to HSQL DB from C#. I am trying to use JNI to invoke Java code in C# environment. The challenge is how to write java import statement in C# environment.
What I have tried:
I used the reference shown in this link https://www.c-sharpcorner.com/UploadFile/ajyadav123/invoking-java-code-in-C-Sharp-net/.
From this link as shown below, java code can be written in C# environment.
1. using System;
2. using net.sf.jni4net;
3.
4. namespace jniDemo
5. {
6. class Program
7. {
8. static void Main(string[] args)
9. {
10.
11. Bridge.CreateJVM(new BridgeSetup());
12. java.lang.System.@out.println("\n\nWelcome Java! in .NET world!\n");
13.
14. int a = java.lang.@Integer.parseInt(args[0]);
15. int b = java.lang.@Integer.parseInt(args[1]);
16. int p = a * b;
17. int q = a + b;
18.
19. java.lang.System.@out.println(a + " * " + b + " = " + p);
20. java.lang.System.@out.println(a + " + " + b + " = " + q);
21.
22.
23.
24. Console.WriteLine("\n\nPress any key....");
25. Console.ReadLine();
26. }
27. }
28. }
Keeping that in mind I want to embed the Java code shown below in the C# environment (source - https://examples.javacodegeeks.com/enterprise-java/sql-enterprise-java/jdbc-hsqldb-tutorial/ ).
The code basically uses JDBC and connect to the DB. And the challenge now is how to do import Java.sql.* classes shown below into the C# environment.
package com.jcg.jdbcexamples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCHSQLDBExample {
public static void getCustomers() {
Connection conn = null;
String db = "jdbc:hsqldb:hsql://localhost/sampledb;ifexists=true";
String user = "SA";
String password = "";
try {
conn = DriverManager.getConnection(db, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select FIRSTNAME, LASTNAME from CUSTOMER");
while(rs.next()) {
System.out.println("Customer Name: " + rs.getString("FIRSTNAME") + " " + rs.getString("LASTNAME"));
}
rs.close();
stmt.close();
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
try {
if (conn != null)
conn.close();
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
public static void main(String[] args) {
getCustomers();
}
}