連接數(shù)據(jù)庫單元測試

driver=com.mysql.cj.jdbc.Driver(mysql8.x)
url=jdbc:mysql://localhost:3306/mytest?useSSL=false&serverTimezone=UTC
username=root
password=yourpassword

8以上的driver 和url和之前的不同

單元測試代碼

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtlTool {
    public static Connection getConnection() {
        //獲取mySql數(shù)據(jù)庫的驅(qū)動類
        String driver="com.mysql.cj.jdbc.Driver";
        //連接數(shù)據(jù)庫
        String url="jdbc:mysql://localhost:3306/mytest?useSSL=false&serverTimezone=UTC";
        //連接mySql的用戶名
        String name="root";
        //連接mySql的密碼
        String password="yourpassword";
        
        try {
            Class.forName(driver);
            Connection conn=DriverManager.getConnection(url, name, password);
            System.out.println("成功連接數(shù)據(jù)庫");
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加載驅(qū)動程序有錯誤");
            return null;
        } catch(SQLException e) {
            e.printStackTrace();
            System.out.println("取得連接時有錯誤,核對用戶名和密碼");
            return null;
        }
    }

    public static void main(String[] args) throws SQLException{
        Connection cc=JDBCUtlTool.getConnection();
        System.out.println(cc);
    }   
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容