附上代碼,我自己在數(shù)據(jù)庫中建的conn數(shù)據(jù)庫密碼時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ū)動程序");??} catch (SQLException e) {???// TODO Auto-generated catch block???e.printStackTrace();???System.out.println("數(shù)據(jù)庫連接出現(xiàn)異常");??}???}}
2 回答

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