Error loading driver:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

来源:互联网 发布:淘宝加购物车送优惠券 编辑:程序博客网 时间:2024/05/22 04:56
错误:Error loading driver:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 

 

相关代码如下:

public static Connection getConnection() {

  try {
   Class.forName("com.mysql.jdbc.Driver");
   Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test2?useUnicode=true", "root","root");
   return (connection);
  } catch (ClassNotFoundException cnfe) {
   System.err.println("Error loading driver:" + cnfe);
   return null;
  } catch (SQLException sqle) {
   System.err.println("Error connection:" + sqle);
   return null;
  }
 }

 

 

改正方法:

1)ClassPath中缺少你想要的mysql的JDBC包,操作如下:

将mysql-connector-java-5.1.6-bin(具体版本看自己的mysql版本)导入工程中,eclipse 中只要右击工程文件夹属性选项-Java Build Path-Libraries-Add Extenal JARs 添加即可……

2)将该包放入   \WEB-INF\lib  下面