數(shù)據(jù)庫鏈接異常怎么處理啊,shopping已在navicat創(chuàng)建
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";
? ?//數(shù)據(jù)庫的用戶名
? ?private static final String username = "root";
? ?//數(shù)據(jù)庫的密碼
? ?private static final String password = "";//
? ?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ù)庫連接對象
? ? 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();
? ? }
? ?
}
}
2017-09-24
characterEncoding=UTF?? 寫錯(cuò)了吧? charactorencoding=UTF