我用java连接mysql时出现错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

来源:百度知道 编辑:UC知道 时间:2024/06/05 02:21:53
我的程序如下:
package conmysql;
import java.sql.*;
public class ConMySql {
public static void main(String[] args) throws InstantiationException, IllegalAccessException{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException cfe){
System.out.println(cfe);
}
String url="jdbc:mysql://localhost:3306/tvsmtest_db";
try{
Connection conn=DriverManager.getConnection(url);
}catch(SQLException sqle){
System.out.println(sqle);
}
}
}
我已经设好classpath了,还有怎么把mysql-connector-java-5.1.6-bin添加到项目呢?放在项目下的哪个文件夹?我要将文本读入mysql,现在测试连接,为什么不行啊?

mysql-connector-java-5.1.6-bin要导入工程中

eclipse 中只要右击工程文件夹属性选项-Java Build Path-Libraries-Add Extenal JARs 添加即可……

加入后可在.classpath文件中看到
<classpathentry path="C:/usr/local/mysql-connector-java-5.0.4-bin.jar" kind="lib"/>

放到项目WEB-INF/lib下或者放到tomcat的lib下都可以的

ClassNotFoundException肯定是mysql包导入出错咯