mysql连接java错误,java连接与mysql错误

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值