jdbc 連線mysql資料庫

2021-07-05 01:11:38 字數 984 閱讀 5109

class.forname("org.postgresql.driver").newinstance(); 裝載資料庫驅動

string url = "jdbc:postgresql://localhost:5432/postgres";

connection con = drivermanager.getconnection(url,"postgres","123");

配置資料庫,並獲得鏈結。上面三句資料庫,使用者名稱密碼都不變的話是可以通用的。

preparedstatement stmt = con.preparestatement("select * from student where s*** =? ");

stmt.setstring(1,"男");

這一句是使用動態查詢 select * from student where s*** =? 這就是你將要向資料庫傳送的查詢語句,其中?是萬用字元。stmt.setstring(1,"男");這一句就是設定統配符,將第乙個?替換成「男」,

這個時候你將要傳送的語句就是select * from student where s*** ='男'。

resultset rs = stmt.executequery();

stmt.executequery();就是向資料庫傳送查詢語句並將結果傳到 型別為resultset 的物件中。

while (rs.next())

這一段就是迴圈了,從得到的第乙個結果開始迴圈,將每乙個迴圈到的結果列印出來。

rs.close();

stmt.close();

這個就是關閉資料庫的連線釋放資源啦。

至於下面這個你一定注意到了。

trycatch (exception ee)

這個分成兩部分,乙個是try的{}中的**,乙個是catch{}中的**。作用就是使虛擬機器監控try中語句的執行,當發生異常時會根據捕捉到的異常執行catch中的**。你的**中的就是捕捉到異常直接列印異常

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資料庫

在學習jdbc過程中,用idea連線資料庫時出現的問題記錄,來來回回找了好多資料,現在把相應的解決辦法記錄下來。通過localhost連線mysql資料庫時,可能會遇到時區的問題,簡單設定一下就可以了,但是通過localhost一般都是可以連上的。string url jdbc mysql loca...

jdbc連線mysql資料庫

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