JDBC連線MySQL資料庫

2021-10-09 14:53:28 字數 780 閱讀 7724

在學習jdbc過程中,用idea連線資料庫時出現的問題記錄,來來回回找了好多資料,現在把相應的解決辦法記錄下來。

通過localhost連線mysql資料庫時,可能會遇到時區的問題,簡單設定一下就可以了,但是通過localhost一般都是可以連上的。

string url =

"jdbc:mysql://localhost:3306/資源名"

使用localhost可以連線成功,但是用本機ip位址連線失敗,查了很多部落格是因為mysql預設是root使用者,遠端登入的時候需要分配新使用者。

分配新使用者的時候,大家要注意一下mysql版本,8.0.21版本的mysql版本把將建立賬戶和賦予許可權分開了,要分三步走:

第一步:

create user 'root'@'本機ip位址' identified by  'password'

;

第二步:

grant all privileges on 資源名.* to 'root'@'本機ip位址'

;

第三步:

flush privileges;
設定ip位址的url

string url =

;

這樣我就連線成功了!!!(花了近2小時)

jdbc 連線mysql資料庫

class.forname org.postgresql.driver newinstance 裝載資料庫驅動 string url jdbc postgresql localhost 5432 postgres connection con drivermanager.getconnection ...

JDBC 連線MYSQL資料庫

1.載入驅動 class.forname com.mysql.jdbc.driver com.mysql.jdbc 包名 driver 驅動名,驅動包需要引入進來 mysql com.mysql.jdbc.driver oracle oracle.jdbc.driver.oracledriver s...

jdbc連線mysql資料庫

連線mysql資料庫的步驟 1.載入驅動 2.用drivermanager獲得資料庫連線 3.例項化queryrunner 4.利用qr.update 實現增刪改 5.利用qr.query 得到結果集 1 public class utilmysql catch classnotfoundexcep...