JDBC 查詢數(shù)據(jù)數(shù)據(jù)范例
jdbc 查詢數(shù)據(jù)數(shù)據(jù)范例
在本教程將演示如何在 jdbc 應(yīng)用程序中,查詢數(shù)據(jù)庫的一個表中數(shù)據(jù)記錄。
在執(zhí)行以下示例之前,請確保您已經(jīng)準(zhǔn)備好以下操作:
- 具有數(shù)據(jù)庫管理員權(quán)限,以在給定模式中向數(shù)據(jù)庫插入數(shù)據(jù)。要執(zhí)行以下示例,需要用實際用戶名和密碼替換這里用戶名(username)和密碼(password)。
- mysql或數(shù)據(jù)庫已啟動并運(yùn)行。
1. 所需步驟
使用 jdbc 應(yīng)用程序向表中查詢數(shù)據(jù)記錄,需要以下步驟:
- 導(dǎo)入包:需要包含包含數(shù)據(jù)庫編程所需的jdbc類的包。大多數(shù)情況下,使用import java.sql.*就足夠了。
- 注冊jdbc驅(qū)動程序:需要初始化驅(qū)動程序,以便可以程序中打開數(shù)據(jù)庫的通信通道。
- 打開連接:需要使用drivermanager.getconnection()方法來創(chuàng)建一個connection對象,它表示與數(shù)據(jù)庫服務(wù)器的物理連接。要創(chuàng)建一個新的數(shù)據(jù)庫,不需要在準(zhǔn)備數(shù)據(jù)庫url時提供任何數(shù)據(jù)庫名稱,如下面的示例所述。
- 執(zhí)行查詢:需要使用類型為statement的對象來構(gòu)建和提交sql語句到數(shù)據(jù)庫。
- 清理環(huán)境:需要明確地關(guān)閉所有數(shù)據(jù)庫資源,而不依賴于 jvm 的垃圾收集。
2. 查詢數(shù)據(jù)數(shù)據(jù)范例
復(fù)制以下示例代碼保存到文件:selectrecords.java中,然后編譯并運(yùn)行如下:
//step 1. import required packages import java.sql.*; public class selectrecords { // jdbc driver name and database url static final string jdbc_driver = "com.mysql.jdbc.driver"; static final string db_url = "jdbc:mysql://localhost/jdbc_db"; // database credentials static final string user = "root"; static final string pass = "123456"; public static void main(string[] args) { connection conn = null; statement stmt = null; try{ //step 2: register jdbc driver class.forname("com.mysql.jdbc.driver"); //step 3: open a connection system.out.println("connecting to a selected database..."); conn = drivermanager.getconnection(db_url, user, pass); system.out.println("connected database successfully..."); //step 4: execute a query system.out.println("creating statement..."); stmt = conn.createstatement(); string sql = "select id, first, last, age from student"; resultset rs = stmt.executequery(sql); //step 5: extract data from result set while(rs.next()){ //retrieve by column name int id = rs.getint("id"); int age = rs.getint("age"); string first = rs.getstring("first"); string last = rs.getstring("last"); //display values system.out.print("id: " + id); system.out.print(", age: " + age); system.out.print(", first: " + first); system.out.println(", last: " + last); } rs.close(); }catch(sqlexception se){ //handle errors for jdbc se.printstacktrace(); }catch(exception e){ //handle errors for class.forname e.printstacktrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) conn.close(); }catch(sqlexception se){ }// do nothing try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); }//end finally try }//end try system.out.println("goodbye!"); }//end main }//end jdbcexample
編譯上面代碼,如下 -
f:\worksp\jdbc> javac -djava.ext.dirs=f:\worksp\jdbc\libs selectrecords.java
執(zhí)行上面代碼,如下 -
f:\worksp\jdbc>java -djava.ext.dirs=f:\worksp\jdbc\libs selectrecords connecting to a selected database... thu jun 01 23:11:55 cst 2017 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification. connected database successfully... creating statement... id: 100, age: 18, first: c++, last: li id: 101, age: 25, first: python, last: py id: 102, age: 30, first: ruby, last: ru id: 103, age: 28, first: java, last: ja goodbye! f:\worksp\jdbc>
相關(guān)文章
- JDBC 教程
- JDBC 驅(qū)動類型
- JDBC 連接數(shù)據(jù)庫范例
- JDBC 連接數(shù)據(jù)庫步驟
- JDBC Statement, PreparedStatement 和 CallableStatement
- JDBC ResultSet 結(jié)果集
- JDBC Resultset 結(jié)果集范例
- JDBC 事務(wù)保存點范例
- Scala 教程
- Scala 簡介
- Scala 類和對象
- Scala 文件 I/O
- Spring 教程
- Spring 模塊
- Spring 依賴注入
- Spring 自動裝配
- Spring MVC教程
- Spring MVC表單標(biāo)簽庫
- Spring security