DBHelper我是copy上一個jsp項目的代碼,但就是要報異常這個怎么解決?
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
//數據庫驅動
? ?private static final String driver = "com.mysql.jdbc.Driver";
? ?//鏈接數據庫的URL地址
? ?private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8";
? ?//數據庫的用戶名
? ?private static final String username = "root";
? ?//數據庫的密碼
? ?private static final String password = "root";
? ?private static Connection conn = null;
? ?//靜態代碼塊負責加載驅動
? ? static
? ? {
? ? try
? ? {
? ? Class.forName(driver);
? ? }
? ? catch(Exception ex)
? ? {
? ? ex.getStackTrace();
? ? }
? ? }
? ? //單例模式返回數據庫連接對象
? ? public static Connection getConnection() throws Exception
? ? {
? ? if(conn==null)
? ? {
? ? conn = DriverManager.getConnection(url, username, password);
? ? ? ?return conn;
? ? }
? ? return conn;
? ? }
? ??
? ? public static void main(String[] args) {
? ? try
? ? {
? ? Connection conn = DBHelper.getConnection();
? ? if(conn != null)
? ? {
? ? System.out.println("數據庫連接正常!");
? ? }
? ? else
? ? {
? ? System.out.println("數據庫連接異常!");
? ? }
? ? }
? ? catch(Exception ex)
? ? {?
? ? ?ex.getStackTrace();
? ? }
? ?
}
}
這個是報的異常:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8
2017-10-20
你看看你的數據庫中有沒有shopping這個數據庫
2017-10-09
JdbcUrl還可以這么寫的嗎?