Re: Connecting to mysql database

Kumaravel Sadras <[email protected]>
Newsgroups gmane.comp.java.ikvm.devel
Message-ID <CAFR7Q1vow8SwUC3EJ69=2upYTdB09vxx=gxdUVY2rDb_P8vhkQ@mail.gmail.com>
Hi Volker
 I downloaded a new jdbc connector and i am usinghte 5.1.31

 On the .NET side I added
*  A refercence to the new dll **mysql-connector-java-5.1.31-bin*

* ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("mysql-connector-java-5.1.31-bin"));*

 On the java side I do

*  Class.forName("com.mysql.jdbc.Driver, mysql-connector-java-5.1.31-bin,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");*
*  DriverManager.setLogStream( java.lang.System.out ); *

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


 My Visual Studio logger has this


 I added the logging and I get this message
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe',
Symbols loaded.
*'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\mysql-connector-java-5.1.31-bin.dll'*
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\IKVM.OpenJDK.Core.dll'
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\IKVM.OpenJDK.Jdbc.dll'
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\IKVM.OpenJDK.Util.dll'
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\dbconnect.dll'
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Users\ksadras\documents\visual studio
2012\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\IKVM.Runtime.dll'
'ConsoleApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded
'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll',
Skipped loading symbols. Module is optimized and the debugger option 'Just
My Code' is enabled.
The program '[5968] ConsoleApplication2.vshost.exe: Managed (v4.0.30319)'
has exited with code -1 (0xffffffff).

On the Java side the logged output is

Connecting to database with password ...
DriverManager.getConnection("jdbc:mysql://localhost/EMP")
    skipping:
driver[className=com.mysql.fabric.jdbc.FabricMySQLDriver,com.mysql
.fabric.jdbc.FabricMySQLDriver@30eb465]
    trying
driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcD
river@2eb0733]
    skipping:
driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@2fa51
ce]
getConnection: no suitable driver found for jdbc:mysql://localhost/EMP
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost/EMP
        at IKVM.Internal.ExceptionHelper.toString(Unknown Source)
        at java.lang.Throwable.toString(map.xml:1315)
        at java.lang.Throwable.toString(map.xml:1315)
        at java.lang.Object.toString(map.xml)
        at java.lang.StringHelper.valueOf(StringHelper.java:2390)
        at java.lang.String.valueOf(map.xml:394)
        at java.io.PrintWriter.println(PrintWriter.java:754)
        at
java.lang.ThrowableHelper$WrappedPrintWriter.println(ThrowableHelper.
java:342)
        at
java.lang.ThrowableHelper.printStackTrace(ThrowableHelper.java:232)
        at
java.lang.ThrowableHelper.printStackTrace(ThrowableHelper.java:299)
        at java.lang.Throwable.printStackTrace(map.xml:1188)
        at java.lang.Throwable.printStackTrace(map.xml:1188)
        at java.sql.SQLException.<init>(SQLException.java:102)
        at java.sql.DriverManager.getConnection(DriverManager.java:647)
        at java.sql.DriverManager.getConnection(DriverManager.java:203)
        at dbconnectmysql.DbConnect.readDb(DbConnect.java:47)
        at cli.JavaLibrary.Program.Main(Program.cs:39)
        at cli.System.AppDomain._nExecuteAssembly(Unknown Source)
        at cli.System.AppDomain.ExecuteAssembly(Unknown Source)
        at
cli.Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(U
nknown Source)
SQLException: SQLState(08001)
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost/EMP
        at IKVM.Internal.ExceptionHelper.toString(Unknown Source)
        at java.lang.Throwable.toString(map.xml:1315)
        at java.lang.Throwable.toString(map.xml:1315)
        at java.lang.Object.toString(map.xml)
        at java.lang.StringHelper.valueOf(StringHelper.java:2390)
        at java.lang.String.valueOf(map.xml:394)
        at java.io.PrintWriter.println(PrintWriter.java:754)
        at
java.lang.ThrowableHelper$WrappedPrintWriter.println(ThrowableHelper.
java:342)
        at
java.lang.ThrowableHelper.printStackTrace(ThrowableHelper.java:232)
        at
java.lang.ThrowableHelper.printStackTrace(ThrowableHelper.java:299)
        at java.lang.Throwable.printStackTrace(map.xml:1188)
        at java.lang.Throwable.printStackTrace(map.xml:1188)
        at java.sql.SQLException.<init>(SQLException.java:102)
        at java.sql.DriverManager.getConnection(DriverManager.java:647)
        at java.sql.DriverManager.getConnection(DriverManager.java:203)
        at dbconnectmysql.DbConnect.readDb(DbConnect.java:47)
        at cli.JavaLibrary.Program.Main(Program.cs:39)
        at cli.System.AppDomain._nExecuteAssembly(Unknown Source)
        at cli.System.AppDomain.ExecuteAssembly(Unknown Source)
        at
cli.Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(U
nknown Source)

*If I use the sample code you gave on the java side I get a warning when I
convert the jar to dll*

c:\ikvm-7.2.4630.5\samples\dbconnect>ikvmc -target:library dbconnect.jar
IKVM.NET Compiler version 7.2.4630.5
Copyright (C) 2002-2012 Jeroen Frijters
http://www.ikvm.net/

note IKVMC0002: Output file is "dbconnect.dll"
*warning IKVMC0100: Class "com.mysql.jdbc.Driver" not found*
warning IKVMC0111: Emitted java.lang.NoClassDefFoundError in
"dbconnectmysql.DbC
onnect.readDb()V"
    ("com.mysql.jdbc.Driver")


Any help is appreciated.
-Kumar


On Wed, Jun 11, 2014 at 10:34 AM, Volker Berlin <
[email protected]> wrote:

>  Hi Kumar,
>
> There seems some thing completely wrong. You can try the follow to find
> more information:
>
> //Create a instance of the driver
> Driver driver = (Driver)Class.forName("com.mysql.jdbc.Driver,
> mysql-connector-java-5.1.5-bin, Version=0.0.0.0, Culture=neutral,
> PublicKeyToken=null").newInstance();
> //connect directly without drivermanager
> Properties props = new Properties();
> props.setProperty( "user", USER );
> props.setProperty( "password", PASS );
> conn = driver.connect(DB_URL,props);
> Console.WriteLine( conn );
>
> You can also enable the JDBC logging to receive more information with:
> DriverManager.setLogStream( java.lang.System.@out );
>
> Volker
>
>
> Am 11.06.2014 11:27, schrieb Kumaravel Sadras:
>
>  Hi Volker,
>
>  Thanks much for your reply.
>
>  I did add that. But I still get a runtime class not found in the java
> class at Class.forName("com.mysql.jdbc.Driver");
>
>
>  Then, I got the Assembly name from .NET and replaced the line in Java to
>
>  Class.forName("com.mysql.jdbc.Driver, mysql-connector-java-5.1.5-bin,
> Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
>
>
>  Now it goes past this line when executed from .NET, but chokes on the
> next line
>
>   conn = DriverManager.getConnection(DB_URL,USER,PASS);
>
>  with the exception
>
>  Connecting to database with password ... java.sql.SQLException: No
> suitable driver found for jdbc:mysql://localhost/EMP at
> java.sql.DriverManager.getConnection(DriverManager.java:647) at
> java.sql.DriverManager.getConnection(DriverManager.java:203) at
> dbconnectmysql.DbConnect.readDb(DbConnect.java:31) at
> cli.JavaLibrary.Program.Main(Program.cs:39) at
> cli.System.AppDomain._nExecuteAssembly(Unknown Source) at
> cli.System.AppDomain.ExecuteAssembly(Unknown Source) at
> cli.Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(U
> nknown Source) Goodbye!
>
>
>  Thanks,
>  -Kumar.
>
>
> On Wed, Jun 11, 2014 at 1:15 AM, Volker Berlin <[email protected]>
> wrote:
>
>>  You have add it with:
>>
>> ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("mysql-connector-java-5.1.5-bin
>> "));
>>
>> Volker
>>
>>
>> Am 11.06.2014 09:48, schrieb Kumaravel Sadras:
>>
>> 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 Explorationhttp://p.sf.net/sfu/hpccsystems
>>
>>
>>
>> _______________________________________________
>> Ikvm-developers mailing [email protected]://lists.sourceforge.net/lists/listinfo/ikvm-developers
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> 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
>>
>>
>
>
> ------------------------------------------------------------------------------
> 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 Explorationhttp://p.sf.net/sfu/hpccsystems
>
>
>
> _______________________________________________
> Ikvm-developers mailing [email protected]://lists.sourceforge.net/lists/listinfo/ikvm-developers
>
>
>
>
> ------------------------------------------------------------------------------
> 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
>
>

------------------------------------------------------------------------------
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.