Connecting to mysql database

Kumaravel Sadras <[email protected]>
Newsgroups gmane.comp.java.ikvm.devel
Message-ID <CAFR7Q1sYBu-W8dx50T8hGSTgf1EdcYW1mZ3HQsunQyQd152esA@mail.gmail.com>
I have a java class that connects to a mysql database and queries a table.

package dbconnectmysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DbConnect {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "yahoo";
 public void readDb()
 {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database with password ...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
      //STEP 6: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
 }

}

This work when running in java called by a java application.

I created the dlls for the DBConnect class and the
mysql-connector-java-5.1.5-bin and added them to the project as reference
dlls

My .NET code looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using ikvm;


using System;
using System.Reflection;

namespace JavaLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            //Add references to all jar files that you use not directly

//ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("second"));

//ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("third"));

            dbconnectmysql.DbConnect dbcon = new dbconnectmysql.DbConnect();
            dbcon.readDb();

        }
    }
}

I get a runtime exception

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at IKVM.NativeCode.java.lang.Class.forName0(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:287)
        at dbconnectmysql.DbConnect.readDb(DbConnect.java:25)
        at cli.JavaLibrary.Program.Main(Program.cs:34)
        at cli.System.AppDomain._nExecuteAssembly(Unknown Source)
        at cli.System.AppDomain.ExecuteAssembly(Unknown Source)
        at
cli.Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(U
nknown Source)

How do I make the runtime find the mysql driver class?

Thanks,
-Kumar.

------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems

_______________________________________________
Ikvm-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ikvm-developers
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.