12 Replies - 450 Views - Last Post: 08 June 2014 - 07:51 AM Rate Topic: -----

#1 dman10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

MySQL & Connector/J CNF Exception

Posted 06 June 2014 - 11:12 AM

So I'm trying to create a simple user database that I could eventually turn into a login system. I created a simple table on MySQL with a name and address for the sake of simplicity. I tried to create simple statement inside my main method that would connect to my MySQL server and when I run my program I get a ClassNotFoundException from "org.gjt.mm.mysql.Driver"

Here is what I have in my main method thus far:
	public static void main(String args[]) {
		try {
			Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data", "root", ""); //Connect
/*
			conn.createStatement().execute("CREATE TABLE people (" + "id int(64) NOT NULL AUTO_INCREMENT," + "name varchar(255) NOT NULL," + "address varchar(255) NOT NULL," + "UNIQUE (id)," + "FULLTEXT(name, address))");

			conn.createStatement().execute("INSERT INTO `people` (`name`, `address`) VALUES ('Bob', '123 Fake Street')"); //Insert a row

			conn.createStatement().execute("INSERT INTO `people` (`name`, `address`) VALUES ('Roger', '666 Devil Street')");

			conn.createStatement().execute("INSERT INTO `people` (`name`, `address`) VALUES ('John', '325 Red Street')");

			
			ResultSet rs = conn.createStatement().executeQuery("SELECT name FROM people WHERE id < 4 ORDER BY id");
			while (rs.next() == true){
				System.out.println(rs.getString("name"));
			}*/
		} catch (Exception err) {
			System.out.println(err);
		}
	}

I downloaded Connector/J in the the binary .zip form and extracted it to my C:\ drive and put the .jar file into my java directories lib folder. I also added the directory of the .jar file into my CLASSPATH and I still get the exception. I realize all of these attempts to get it to work are not the correct way but if someone could direct me towards getting this exception fixed that would be great, I'm new to MySQL so sorry for any ignorance :) Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: MySQL & Connector/J CNF Exception

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: MySQL & Connector/J CNF Exception

Posted 06 June 2014 - 11:59 AM

For the official MySql Driver it should be:

Class.forName("com.mysql.jdbc.Driver");


Not sure what you are trying to reference there.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: MySQL & Connector/J CNF Exception

Posted 06 June 2014 - 04:56 PM

Quote

I also added the directory of the .jar file into my CLASSPATH
That's wrong. Directories only appear in classpaths when their leaves are class files, not jar files. A jar file should normally appear as its full path, up to and including the .jar extension
Was This Post Helpful? 0
  • +
  • -

#4 dman10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

Re: MySQL & Connector/J CNF Exception

Posted 07 June 2014 - 11:22 AM

I changed the statement to
Class.forName("com.mysql.jdbc.Driver"); //Load the driver


Also I changed the CLASSPATH to
C:\Program Files\Java\jdk1.8.0_05\lib\mysql-connector-java-5.1.30-bin.jar


and still the same error just referencing the new driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: MySQL & Connector/J CNF Exception

Posted 07 June 2014 - 11:40 AM

Quote

Also I changed the CLASSPATH to
How and where?
Was This Post Helpful? 0
  • +
  • -

#6 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 47
  • Joined: 17-April 14

Re: MySQL & Connector/J CNF Exception

Posted 07 June 2014 - 02:05 PM

Shouldn't you have to import first.

import java.sql.*;


And make sure your JDBC Connector jar is working.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: MySQL & Connector/J CNF Exception

Posted 07 June 2014 - 03:04 PM

Quote

Shouldn't you have to import first.
If you did, it wouldn't compile ;)
Was This Post Helpful? 0
  • +
  • -

#8 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 47
  • Joined: 17-April 14

Re: MySQL & Connector/J CNF Exception

Posted 07 June 2014 - 09:17 PM

Here's some example code for a basic login system. I built it a few months ago, but it still works.

import java.sql.*;

public class MySQL {

	private Connection con;
	private Statement st;
	private ResultSet rs;

	public MySQL() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "");
			st = con.createStatement();
		} catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}

	public void getData() {
		try {
			String query = "select * from accounts";
			rs = st.executeQuery(query);
			System.out.println("Records from Database");
			while (rs.next()) {
				String name = rs.getString("name");
				String username = rs.getString("username");
				String password = rs.getString("password");
				System.out.println("Name: " + name + "  " + "username: " + username + "  " + "password: " + password);
			}
		} catch (Exception x) {
			System.out.println(x);
		}
	}

	public static void main(String[] args) {

		new MySQL();

	}

}



Was This Post Helpful? 0
  • +
  • -

#9 dman10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

Re: MySQL & Connector/J CNF Exception

Posted 08 June 2014 - 07:21 AM

View PostSuperProgrammer, on 07 June 2014 - 09:17 PM, said:

Here's some example code for a basic login system. I built it a few months ago, but it still works.

import java.sql.*;

public class MySQL {

	private Connection con;
	private Statement st;
	private ResultSet rs;

	public MySQL() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "");
			st = con.createStatement();
		} catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}

	public void getData() {
		try {
			String query = "select * from accounts";
			rs = st.executeQuery(query);
			System.out.println("Records from Database");
			while (rs.next()) {
				String name = rs.getString("name");
				String username = rs.getString("username");
				String password = rs.getString("password");
				System.out.println("Name: " + name + "  " + "username: " + username + "  " + "password: " + password);
			}
		} catch (Exception x) {
			System.out.println(x);
		}
	}

	public static void main(String[] args) {

		new MySQL();

	}

}



Still get a ClassNotFoundException when I use this code, so it's probably not the code. I just think it's something to do with my CLASSPATH or where I put my Connector/J file or maybe even a difficulty with my IDE.
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: MySQL & Connector/J CNF Exception

Posted 08 June 2014 - 07:25 AM

What IDE are you using?
Was This Post Helpful? 0
  • +
  • -

#11 dman10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

Re: MySQL & Connector/J CNF Exception

Posted 08 June 2014 - 07:29 AM

View PostRyano121, on 08 June 2014 - 07:25 AM, said:

What IDE are you using?

OK well I just used a command prompt to run the program and everything worked perfectly. So I think there is something I am unaware of that I must do in order to have the Connector/J work in my IDE. To answer your question, Eclipse.

Alright well after it all I feel stupid, I figured it out lol. Thanks guys.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: MySQL & Connector/J CNF Exception

Posted 08 June 2014 - 07:30 AM

http://technojeeves....ystemproperties

Put the driver jar into a directory named by

java Props java.ext.dirs


(and it will always be found - you can afford to use this technique when conflicts are unlikely to happen)

This post has been edited by g00se: 08 June 2014 - 07:46 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: MySQL & Connector/J CNF Exception

Posted 08 June 2014 - 07:51 AM

btw, user authentication, to be efficient, should pass the candidate username and password to a PreparedStatement which selects only those columns. IOW neither my code nor any other code posted here is really appropriate

This post has been edited by g00se: 08 June 2014 - 07:52 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1