当前位置: 动力学知识库 > 问答 > 编程问答 >

jdbc - java connectivity with mysql error

问题描述:

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

But it gave the following error

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?

网友答案:

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.

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

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.)

网友答案:

As @Ash says, the problem is that the Connector/J drivers are not on your classpath. You can download the latest version (5.0.12) from this page.

分享给朋友:
您可能感兴趣的文章:
随机阅读: