Re: Connecting to mysql database

Volker Berlin <[email protected]>
Newsgroups gmane.comp.java.ikvm.devel
Message-ID <[email protected]>
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] <mailto:[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 Exploration
>>     http://p.sf.net/sfu/hpccsystems
>>
>>
>>     _______________________________________________
>>     Ikvm-developers mailing list
>>     [email protected]  <mailto:[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]
>     <mailto:[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

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