附上代碼,我自己在數(shù)據(jù)庫(kù)中建的conn數(shù)據(jù)庫(kù)密碼時(shí)mysqlpublic class DBTest {?@Test?public void test()? {??final String url="jdbc:mysql://localhost:3306/conn";??final String user="root";??final String password="mysql";??try {??Class.forName("com.mysql.jdbc.Driver");??Connection conn = DriverManager.getConnection(url, user, password);???System.out.println(conn);??}??catch(ClassNotFoundException e) {???e.printStackTrace();???System.out.println("未找到驅(qū)動(dòng)程序");??} catch (SQLException e) {???// TODO Auto-generated catch block???e.printStackTrace();???System.out.println("數(shù)據(jù)庫(kù)連接出現(xiàn)異常");??}???}}
2 回答
已采納

qq_a沙漏中的時(shí)光_0
TA貢獻(xiàn)13條經(jīng)驗(yàn) 獲得超2個(gè)贊
使用SQLyog看是否能連上你的數(shù)據(jù)庫(kù)
查看mysql的服務(wù)是否啟動(dòng)

望遠(yuǎn)
TA貢獻(xiàn)1017條經(jīng)驗(yàn) 獲得超1032個(gè)贊
確保mysql-jdbc驅(qū)動(dòng)加載到了項(xiàng)目引用jar中
檢查mysql數(shù)據(jù)庫(kù)端口是否為3306,show global variables like 'port';
確保數(shù)據(jù)庫(kù)用戶名和密碼正確
確保數(shù)據(jù)庫(kù)conn存在,show databases;
添加回答
舉報(bào)
0/150
提交
取消