Java连接到数据库错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [英] Java Connect to database Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
问题描述
嘿,愚蠢的问题,但我有一个困难的时间连接我的java程序到一个mysql数据库。
当我点击这行时抛出异常。
Hey stupid question but I'm having a hard time connecting my java program to a mysql database. Throwing an exception when I hit this line.
Class.forName(driverName).newInstance();
驱动程序名称为com.mysql.jdbc.Driver。我在google上搜索了一下,发现一些关于一个mysql-connector.jar文件,我显然应该有,但我真的没有研究它。
The driver name is com.mysql.jdbc.Driver. I've searched around a bit on google and found something about a mysql-connector.jar file that I'm apparently supposed to have but I really haven't looked into it yet. Thanks.
整个代码:
Connection connection = null;
try
{
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName).newInstance();
String serverName = "*********";
String database = "canteen_web3";
String url = "jdbc:mysql://" + serverName + "/" + database;
final String username = "*****";
final String password = "******";
connection = DriverManager.getConnection(url,username,password);
System.out.println("Connected!");
}
catch(Exception ex)
{
throw new ICException(ex.getMessage());
}
推荐答案
p>
Start your app with
java -classpath .:mysql-connector.jar MyClass
冒号分隔两个路径。的。是您所在的目录(希望是类或基础包),后面的jar是驱动程序。
The colon separates two paths. The . is the directory you are in (and hopefully the class or the base package), the latter jar is the driver.
有关详细信息,请参阅各种文档来源< a href =http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html =nofollow> http://download.oracle.com/javase/1.5。 0 / docs / tooldocs / windows / classpath.html
For further information refer to the various sources of documentation http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html
这篇关于Java连接到数据库错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!