DBHelper我是copy上一個(gè)jsp項(xiàng)目的代碼,但就是要報(bào)異常這個(gè)怎么解決?
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
//數(shù)據(jù)庫驅(qū)動(dòng)
? ?private static final String driver = "com.mysql.jdbc.Driver";
? ?//鏈接數(shù)據(jù)庫的URL地址
? ?private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8";
? ?//數(shù)據(jù)庫的用戶名
? ?private static final String username = "root";
? ?//數(shù)據(jù)庫的密碼
? ?private static final String password = "root";
? ?private static Connection conn = null;
? ?//靜態(tài)代碼塊負(fù)責(zé)加載驅(qū)動(dòng)
? ? static
? ? {
? ? try
? ? {
? ? Class.forName(driver);
? ? }
? ? catch(Exception ex)
? ? {
? ? ex.getStackTrace();
? ? }
? ? }
? ? //單例模式返回?cái)?shù)據(jù)庫連接對(duì)象
? ? 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("數(shù)據(jù)庫連接正常!");
? ? }
? ? else
? ? {
? ? System.out.println("數(shù)據(jù)庫連接異常!");
? ? }
? ? }
? ? catch(Exception ex)
? ? {?
? ? ?ex.getStackTrace();
? ? }
? ?
}
}
這個(gè)是報(bào)的異常:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8
2017-10-20
你看看你的數(shù)據(jù)庫中有沒有shopping這個(gè)數(shù)據(jù)庫
2017-10-09
JdbcUrl還可以這么寫的嗎?