package com.swift; //這裡導入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connection; import java.sql.DriverManager; import java.sql ...
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