Can't find jdbc driver? [duplicate]

主宰稳场 提交于 2021-01-29 00:59:02

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/29653862/cant-find-jdbc-driver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!