package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestJDBC { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { //使用com.mysql.jdbc.Connection包会出错 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); System.out.println(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection
如果忘记倒入连接mysql数据库的包,会出现上面的java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
mysql-connector-java-5.1.7-bin.jar
下载地址:链接: https://pan.baidu.com/s/1geBRqqn 密码: 8jxm