-2

I'm trying to connect a java program in netbeans using the mysql j connector i get an exception message saying com.mysql.jdbc.Driver this my code

package testdb;

import java.sql.*;

public class Testdb {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/location";
        String login = "root";
        String pass = "";
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url,login,pass);
            Statement s = con.createStatement();
            ResultSet r = s.executeQuery("select * from reservation");
            while(r.next())
            {
                System.out.println("id reservation = "+r.getInt("id_reservation"));
            }

        }catch (Exception e) 
        {
             System.out.println(e.getMessage());
        }
    }
}
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • what exception you get??? – G.S Jan 03 '14 at 12:24
  • Print the stack trace e.printStackTrace() instead of the message. You will get a better idea. Check if you have added the required jar file to the build path – sanket Jan 03 '14 at 12:24
  • Don't use e.getMessage(). Use e.printStackTrace() to see a useful description of the exception. – Kayaman Jan 03 '14 at 12:24
  • 4
    change this line `System.out.println(e.getMessage());` to `e.printStackTrace()` and post here the full stacktrace – SpringLearner Jan 03 '14 at 12:25
  • Remove your catch block, and add a throws XxxException to your main method (for each checked exception). Then paste the stack trace you get. – JB Nizet Jan 03 '14 at 12:26

2 Answers2

0

You should add MySQL j Connectors jar to your classpath:

Mysql connector

0

Assuming that the exception says that it cannot find the Driver class of MySQL.

The mysql-connector-java-5.1.27.jar (or newer version, containing the class) has to be taken from MySQL. It has to be on the "class path" during run-time.

Nicest IMHO would be to have a maven project, then you could add a dependency with scope "runtime", and the jar would be taken automatically, and packaged in the resulting application.

Otherwise you can select the project's properties, Libraries, Add JAR/Folder, and add the self-downloaded jar (from mysql.org).

If you have a target\myapp.jar and a target\lib\mysql-connector-java-5.1.27.jar you may add a Class-Path: ... lib\mysql-con... entry in META-INF/MANIFEST.MF in the myapp.jar.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138