1

I've started learning how to connect MySQL database with Java. And since I'm total beginner I was looking for the most basic guide and found this . It looks easy, quite understandable and helpful.

But when i run that code it shows an error and the table is empty. :(

CODE:

import java.sql.*;
import java.util.Calendar;



public class DatabaseClass {

public static void main(String args[]){
    try{
        String myDriver = "org.gjt.mm.mysql.Driver";
        String myUrl = "jdbc:mysql://localhost/test";
        Class.forName(myDriver);
        Connection conn = DriverManager.getConnection(myUrl, "root", "admin");

        Calendar calen = Calendar.getInstance();
        java.sql.Date startDate = new java.sql.Date(calen.getTime().getTime());

        String query = "insert into users (first_name, last_name, date_created, is_admin, num_points)"
                + " values(?,?,?,?,?)";

        PreparedStatement preparedStmt = conn.prepareStatement(query);
        preparedStmt.setString(1, "Name");
        preparedStmt.setString(2, "LName");
        preparedStmt.setDate(3, startDate);
        preparedStmt.setBoolean(4, false);
        preparedStmt.setInt(5, 5000);

        preparedStmt.execute();

        conn.close();

    }catch(Exception e){
        System.err.println("Got an exception!");
        System.err.println(e.getMessage());

    }
}

ERROR:

run:
Got an exception!
org.gjt.mm.mysql.Driver
BUILD SUCCESSFUL (total time: 0 seconds)

Same thing happens with any driver I put in.

It's probably my lack of knowledge and it could be not much of a problem, but when you are new to it it looks like first world's problem D:

STACK TRACE:

java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at Sranje.DatabaseClass.main(DatabaseClass.java:14)

  • Got Database called 'test'
  • In test there is table called 'users' (with specified fields)
  • Using MySQL 5.6 Command Line client
  • Code built in NetBeans 8.0.2
SadCoffeeBean
  • 303
  • 2
  • 5
  • 13
  • 1
    Did you put the MySQL jar in the working directory somewhere? – tim Apr 15 '15 at 15:07
  • What's ant target that you are using? – SMA Apr 15 '15 at 15:08
  • @tim No, just blindly followed the guide, was trying more to understand the code.. Mind explaining which jar ? – SadCoffeeBean Apr 15 '15 at 15:10
  • 1
    Can you post the stack information of the exception? – Sid Zhang Apr 15 '15 at 15:11
  • 1
    Well, the tutorial clearly lacks of explaining that you must add the MySQL connectivity library that provides the class `org.gjt.mm.mysql.Driver` and others that allows you to connect to a MySQL database. Download this jar, add it to the classpath of your application and execute it. – Luiggi Mendoza Apr 15 '15 at 15:13
  • 1
    http://dev.mysql.com/downloads/connector/j/ I think you need something like this. You should find a better tutorial that takes you from beginning to end. http://www.vogella.com/tutorials/MySQLJava/article.html This is a bit vague but it seems to have all the steps. – tim Apr 15 '15 at 15:14
  • @tim thanks for the guide, will follow it from now. And yeah i guess its the jar file part that i skipped. Thanks :) – SadCoffeeBean Apr 15 '15 at 15:23
  • @LuiggiMendoza yeah, I'll switch to the guide that tim provided and try the thing with that jar file. Thanks for help :) – SadCoffeeBean Apr 15 '15 at 15:23

1 Answers1

6

You should put a file with MySQL driver to your classpath in NetBeans, so the IDE know the driver class you want to load.

The other thing is a strange driver name org.gjt.mm.mysql.Driver, usually it is com.mysql.jdbc.Driver. Please try using the com.mysql.jdbc.Driver driver name and put a mysql-connector jar into your classpath. You can find the driver in MySQL JDBC Connector JAR, which you can download here:

http://dev.mysql.com/downloads/connector/j/

Also I advise you to write e.printStackTrace() in your catch, so you always know what really goes wrong.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
davidluckystar
  • 928
  • 5
  • 15
  • Thanks David ^^ I used `org.gjt.mm.mysql.Driver` because I've read somewhere that It's used for MySQL... Dunno really, will use `com.mysql.jdbc.Driver` from now on. – SadCoffeeBean Apr 15 '15 at 15:28
  • 1
    @SadCoffeeBean `org.gjt.mm.mysql.Driver` is just provided for backwards compatibility, it has been `com.mysql.jdbc.Driver` for more than ten years now afaik, on top of that, you no longer **need** to load the driver explicitly with `Class.forName`. The fact that a 'tutorial' that was last updated a year ago still uses these outdated practices shows that it isn't good quality. – Mark Rotteveel Apr 16 '15 at 07:27