java中 MySQL与驱动jdbc连接不上,想请教高手:jdbc驱动:mysql-connector-java-5.1.18-bin.jar怎么用啊?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中 MySQL与驱动jdbc连接不上,想请教高手:jdbc驱动:mysql-connector-java-5.1.18-bin.jar怎么用啊?相关的知识,希望对你有一定的参考价值。

连接数据库的一段代码如下:(高手们你们看看有问题么??)
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost:3306/bookstore";
String user = "root";
String password = "123";
int i=0;
Class.forName("com.MySQL.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("连接数据库成功!");
statement= conn.createStatement();


报错如下:(总是说没有连接jdbc驱动,大神们!!用mysql时,jdbc应该怎么放或者配置啊???求教啊!!!)
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 connectmsl.Test.<init>(Test.java:27)
at connectmsl.Admin.actionPerformed(Admin.java:59)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Class.forName("com.MySQL.jdbc.Driver").newInstance();
改成 com.mysql.jdbc.Driver
参考技术A MYSQL改小写是必须的。不过你还要把这个jar包放到你项目的lib文件夹里面去,这样他才能找到撒。 参考技术B 那个mysql-connector-java-5.0.8-bin.jar你加上试试! 参考技术C 楼上正解,还有就是要中入mySql数据库的jar驱动包

以上是关于java中 MySQL与驱动jdbc连接不上,想请教高手:jdbc驱动:mysql-connector-java-5.1.18-bin.jar怎么用啊?的主要内容,如果未能解决你的问题,请参考以下文章

网页与mysql数据库怎么连接?

在jdbc中,负责建立与指定数据库连接的方法是

如何用mysql实现ODBC(JDBC桥)连接数据库?

使用jdbc连接mysql为啥报错?

com.mysql.jdbc.Driver

关于mysql jdbc驱动程序的安装