JDBC資料庫連線步驟

2021-07-22 04:05:57 字數 600 閱讀 7439

1、載入資料庫驅動

class.forname("com.mysql.jdbc.driver");

2、建立資料庫連線池

conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/test2?user=root&password=1234");

3、由當前資料庫連線生成乙個資料操作物件

stmt=conn.createstatement(); 

4、獲取preparedstatement

preparedstatement ps=conn.preparestatement(sql);

5、寫入要執行的sql語句

string sql = "insert into user(name,age,submitdate) value(?,?,now())"; 6、

用這個資料操作物件進行資料庫操作

ps.executeupdate();//更新

ps.executeupdate();//查詢

7、關閉資料庫物件

ps.close();

8、關閉資料連線

conn.close() ; 

jdbc資料庫連線步驟

筆記 連線資料庫的幾大步驟 1.載入驅動 class.forname 資料庫驅動的名稱 2.建立連線 connection conn driver.getconnection 路徑 使用者名稱 密碼 埠號 3.建立預編譯物件 statement stat conn.createstatement 4...

JDBC連線資料庫步驟

宣告資料庫驅動,資料來源的url,用於登入資料庫的賬戶和密碼 將其他功能封裝成方法的時候方便使用 string driver 資料庫驅動名稱 string url 資料庫連線位址 string user 用來連線資料庫的使用者名稱 string pwd 用來連線資料庫的密碼 載入資料庫驅動 clas...

JDBC連線資料庫的步驟

1 載入驅動 class.forname oracle.jdbc.driver.oracledriver 2建立資料庫連線 connection conn drivermanager.getconnection string url,string user,string password strin...