Problems connecting to MySQL Database using JDBC

Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Page 1 of 1

4 Replies - 4612 Views - Last Post: 21 April 2009 - 07:05 PM Rate Topic: -----

#1 sbowman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-April 09

Problems connecting to MySQL Database using JDBC

Post icon  Posted 19 April 2009 - 09:59 AM

Hi,

I am trying to connect my code to a MySQL database and am getting the following error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've done research on classpath variables and path variables and not clear on where or what to enter for classpath variables or the path variables. I'm using an IDE called JGRASP running on Windows VISTA. Any help would be greatly appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: Problems connecting to MySQL Database using JDBC

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: Problems connecting to MySQL Database using JDBC

Posted 19 April 2009 - 01:25 PM

This isn't a tutorial. Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problems connecting to MySQL Database using JDBC

Posted 19 April 2009 - 01:48 PM

View Postsbowman, on 19 Apr, 2009 - 08:59 AM, said:

Hi,

I am trying to connect my code to a MySQL database and am getting the following error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've done research on classpath variables and path variables and not clear on where or what to enter for classpath variables or the path variables. I'm using an IDE called JGRASP running on Windows VISTA. Any help would be greatly appreciated.


You have to load the JDBC driver first
That is what I do with HSqlDb... syntax for MySql should be quite similar

 try {
	Class.forName("org.hsqldb.jdbcDriver"); 
}
		  catch (Exception e) {
	  System.exit(0);
  }



probably: "com.mysqldb.jdbcDriver"
Was This Post Helpful? 0
  • +
  • -

#4 jpete7683   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Problems connecting to MySQL Database using JDBC

Posted 21 April 2009 - 06:53 PM

Here is an example of how I do it:

import java.sql.*;

   public class Connect
   {
	   public void getConn(){
		   Connection conn = null;
		   try
		   {
			   String userName = <username>;
			   String userPass = <password>;
			   String url = "jdbc:mysql://localhost/<database name>";
			   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			   conn = DriverManager.getConnection (url, userName, password);
		   }
		   catch (Exception e){
			 e.printStackTrace();
		   }
		 }
	  }


Then when you write your prepared statements you will have to call getConn:
		 getConn();


That will open a connection to the database. I think there is a 'jar' file you will have to download and include into your project to get access to the jdbc driver.
Was This Post Helpful? 1
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problems connecting to MySQL Database using JDBC

Posted 21 April 2009 - 07:05 PM

View Postjpete7683, on 21 Apr, 2009 - 05:53 PM, said:

Here is an example of how I do it:

import java.sql.*;

   public class Connect
   {
	   public void getConn(){
		   Connection conn = null;
		   try
		   {
			   String userName = <username>;
			   String userPass = <password>;
			   String url = "jdbc:mysql://localhost/<database name>";
			   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			   conn = DriverManager.getConnection (url, userName, password);
		   }
		   catch (Exception e){
			 e.printStackTrace();
		   }
		 }
	  }


Then when you write your prepared statements you will have to call getConn:
		 getConn();


That will open a connection to the database. I think there is a 'jar' file you will have to download and include into your project to get access to the jdbc driver.


This should get you on...
notice the little subtile syntax difference (this is what happens when you deal with different vendors):

com.mysql.jdbc.Driver
org.hsqldb.jdbcDriver

one as a . between jdbc and Driver the other doies not :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1