2 資料庫連線

2021-10-06 01:18:02 字數 1020 閱讀 8056

1.註冊驅動:

class.

forname

("com.mysql.cj.jdbc.driver");

//對於我的電腦為mysql8.0版本的

2.建立連線:

connection connection = null;

string url =

"jdbc:mysql://localhost:3306/grade"

+"?user=root&password=18170021&servertimezone=utc"

;

connection = drivermanager.

getconnection

(url)

;

url的含義:

jdbc:連線的協議

mysql:連線的哪乙個資料庫

localhost:資料庫所在ip位址

3306:埠號

grade:資料庫

user=root&password=18170021 使用者名稱和密碼

servertimezone=asia/shanghai 時區設定,使用mysql的6.0.x以上的jar的時候要寫上,可以通過以下程式訪問得到 自己的時區

calendar cal = calendar.

getinstance()

; timezone timezone = cal.

gettimezone()

; system.out.

println

(timezone.

getid()

);system.out.

println

(timezone.

getdisplayname()

);

2 使用資料庫連線

為了訪問資料庫,需要提供某種連線引數,如執行資料庫的計算機和登入證書。使用sqlconnection類連線sql server。下面的 段說明了如何建立 開啟和關閉books資料庫的連線。public static void openconnection catch exception ex 注意 ...

資料庫連線

第一,設定允許遠端連線,允許sql server驗證 第二,建立登陸帳戶並授權 第三,註冊sql server資料庫 c windows microsoft.net framework v2.0.50727 下執行 aspnet regsql 指令 data server sqlexpress in...

資料庫連線

php host localhost host name db user root mysql username db password root mysql password db name table database name connect to server and select data...