使用JDBC連線資料庫的步驟

2021-07-13 09:48:29 字數 683 閱讀 6279

//第一步,載入驅動

class.forname("oracle.jdbc.driver.oracledriver");

//第二步,獲取連線的物件

stringurl ="jdbc:oracle:thin:@localhost:1521:orcl";

stringuser = "bbs";

stringpwd ="123";

connectioncon = drivermanager.getconnection(url, user, pwd);

//第三步,構建命令物件

statement  stm = con.createstatement();

stringsql = "select * "+

"from userinfo "+

"where username='"+strname+"' and userpwd ='"+strpwd+"'";

system.out.println(sql);

//獲取從資料庫表得到的查詢資料,存放在類似於資料表的記憶體中

resultsetrowset = stm.executequery(sql);

booleaniflogin = false;

while(rowset.next())

rowset.close();

con.close();

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...

JDBC連線資料庫的步驟

string user root string password 123 string url jdbc mysql localhost 3306 day14 1.載入驅動 class.forname com.mysql.jdbc.driver 不推薦使用這種方法 1,會載入驅動兩次 載入類一次,n...