Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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.	         //******* Java code Start...*******  
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.	         //******* Java code end...*******  
22.	  
23.	         // C# code  
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.

Java
package com.jcg.jdbcexamples; 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 *
 * @author Satya Choudhury
 */
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 {
            // Create database connection
            conn = DriverManager.getConnection(db, user, password);
             
            // Create and execute statement
            Statement stmt = conn.createStatement();
            ResultSet rs =  stmt.executeQuery("select FIRSTNAME, LASTNAME from CUSTOMER");
             
            // Loop through the data and print all artist names
            while(rs.next()) {
                System.out.println("Customer Name: " + rs.getString("FIRSTNAME") + " " + rs.getString("LASTNAME"));
            }
             
            // Clean up
            rs.close();
            stmt.close();
        }
        catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        finally {
            try {
                // Close connection
                if (conn != null) 
                    conn.close();
            }
            catch (SQLException e) {
                System.err.println(e.getMessage());
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Retrieve all customers
        getCustomers();
    }
     
}
Posted
Updated 14-Jul-21 20:22pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900