Java连接与MySQL错误 [英] java connectivity with mysql error

查看:104
本文介绍了Java连接与MySQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始进行连接并尝试了此示例.我已经安装了必要的软件.还将jar文件复制到/ext文件夹.但是下面的代码出现以下错误

I just started with the connectivity and tried this example. I have installed the necessary softwares. Also copied the jar file into the /ext folder.Yet the code below has the following error

import java.sql.*;
public class Jdbc00 {
  public static void main(String args[]){
        try {
      Statement stmt;

          Class.forName("com.mysql.jdbc.Driver");
          String url =
            "jdbc:mysql://localhost:3306/mysql"
            DriverManager.getConnection(url,"root", "root");

      //Display URL and connection information
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);

      //Get a Statement object
      stmt = con.createStatement();

      //Create the new database
      stmt.executeUpdate(
                       "CREATE DATABASE JunkDB");

      stmt.executeUpdate(
          "GRANT SELECT,INSERT,UPDATE,DELETE," +
          "CREATE,DROP " +
          "ON JunkDB.* TO 'auser'@'localhost' " +
          "IDENTIFIED BY 'drowssap';");
      con.close();
    }catch( Exception e ) {
      e.printStackTrace();
    }//end catch
  }//end main
}//end class Jdbc00

但是它给出了以下错误

D:\Java12\Explore>java Jdbc00
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Jdbc00.main(Jdbc00.java:11)

有人可以指导我更正吗?

Could anyone please guide me in correcting this?

推荐答案

在运行应用程序时,在类路径中找不到包含MySQL驱动程序类(com.mysql.jdbc.Driver)的jar文件.这就是ClassNotFoundException的抱怨.

The jar file that contains the MySQL driver class (com.mysql.jdbc.Driver) isn't being found on the classpath when you run your application. This is what the ClassNotFoundException is complaining about.

您需要将其添加到CLASSPATH环境变量中,或者在运行Java时使用classpath选项.例如:

You'll need to add it either to the CLASSPATH environment variable, or using the classpath option when running Java. For example:

java -cp mysql-connector-java-5.0.8-bin.jar Jdbc00

使用您正在使用的任何MySQL连接器jar文件的名称和位置. (如果尚未在localhost上安装MySQL,则您的应用程序需要连接一些东西,您可能也必须这样做.)

Use the name and location of whatever MySQL connector jar file you're using. (If you haven't already installed MySQL on localhost, so your application has something to connect to, you might have to do that too.)

这篇关于Java连接与MySQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆