很奇怪啊,我在dos下用java命令可以连接并添加mysql数据库,应该说mysql的连接没有问题。
但是在eclipse里面就是不行。是不是eclpise还要配制什么啊?
-------------------------------------------
程序是这样:
import java.sql.*;
public class sql {
/**
* @param args
*/
public static void main(String[] args) {
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/test";
Connection con =DriverManager.getConnection(url,"root", "hehehehe");
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
stmt = con.createStatement();
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
-----------------------------------------
eclipse提示出错信息:
java.lang.ClassNotFoundException: org.gjt.mm.mysql.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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sql.test.main(test.java:12)
----------------------------------------------------------
test.java:12 是指Class.forName("com.mysql.jdbc.Driver");
------解决方法--------------------------------------------------------
是你的MySQL驱动程序没有加到你的Eclipse工程里面去的问题,加进去即可了。
------解决方法--------------------------------------------------------
你要告诉eclipse你的驱动在哪儿。这个和dos下的不一样的
------解决方法--------------------------------------------------------