java 找不到jdbc驱动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29653862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:40:36  来源:igfitidea点击:

Can't find jdbc driver?

javamysqljdbc

提问by SadCoffeeBean

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.

我已经开始学习如何将 MySQL 数据库与 Java 连接起来。由于我完全是初学者,我一直在寻找最基本的指南并找到了这个。它看起来很简单,很容易理解并且很有帮助。

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:

这可能是我缺乏知识,这可能不是什么大问题,但是当您不熟悉它时,它看起来像是第一世界的问题 D:

STACK TRACE:

堆栈跟踪:

java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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
  • 获得名为“test”的数据库
  • 在测试中有一个名为“用户”的表(具有指定的字段)
  • 使用 MySQL 5.6 命令行客户端
  • NetBeans 8.0.2 中内置的代码

回答by davidluckystar

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

您应该将带有 MySQL 驱动程序的文件放在 NetBeans 中的类路径中,以便 IDE 知道您要加载的驱动程序类。

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.Driverdriver 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:

另一件事是一个奇怪的驱动程序名称org.gjt.mm.mysql.Driver,通常是com.mysql.jdbc.Driver。请尝试使用com.mysql.jdbc.Driver驱动程序名称并将 mysql-connector jar 放入您的类路径。您可以在 MySQL JDBC Connector JAR 中找到驱动程序,您可以在此处下载:

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

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.

另外我建议你写下e.printStackTrace()你的捕获,这样你总是知道真正出了什么问题。