以下是使用“結(jié)果集”教程中描述的幾種導(dǎo)航方法的示例。
該示例代碼是根據(jù)前幾章中的環(huán)境和數(shù)據(jù)庫(kù)設(shè)置編寫(xiě)的。
復(fù)制并粘貼以下示例到JDBCExample.java中,如下編譯并運(yùn)行:
//步驟1.導(dǎo)入所需的軟件包
import java.sql.*;
public class JDBCExample {
// JDBC驅(qū)動(dòng)程序名稱(chēng)和數(shù)據(jù)庫(kù)URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// 數(shù)據(jù)庫(kù)憑證
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//步驟2:注冊(cè)JDBC驅(qū)動(dòng)程序
Class.forName("com.mysql.jdbc.Driver");
//步驟3:建立連接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//步驟4:執(zhí)行查詢(xún)以創(chuàng)建陳述
// RS示例的必需參數(shù)。
System.out.println("Creating statement...");
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
// 將光標(biāo)移到最后一行。
System.out.println("Moving cursor to the last...");
rs.last();
//步驟5:從結(jié)果集中提取數(shù)據(jù)
System.out.println("Displaying record...");
//按列名檢索
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//顯示值
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
// 將光標(biāo)移到第一行。
System.out.println("Moving cursor to the first row...");
rs.first();
//步驟6:從結(jié)果集中提取數(shù)據(jù)
System.out.println("Displaying record...");
//按列名檢索
id = rs.getInt("id");
age = rs.getInt("age");
first = rs.getString("first");
last = rs.getString("last");
//顯示值
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
// 將光標(biāo)移到第一行。
System.out.println("Moving cursor to the next row...");
rs.next();
//步驟7:從結(jié)果集中提取數(shù)據(jù)
System.out.println("Displaying record...");
id = rs.getInt("id");
age = rs.getInt("age");
first = rs.getString("first");
last = rs.getString("last");
//顯示值
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
//步驟8:清理環(huán)境
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//處理JDBC錯(cuò)誤
se.printStackTrace();
}catch(Exception e){
//處理Class.forName的錯(cuò)誤
e.printStackTrace();
}finally{
//用于關(guān)閉資源
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}//結(jié)束JDBCExample現(xiàn)在讓我們編譯上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
運(yùn)行時(shí)JDBCExample,它將產(chǎn)生以下結(jié)果-
C:\>java JDBCExample Connecting to database... Creating statement... Moving cursor to the last... Displaying record... ID: 103, Age: 30, First: Sumit, Last: Mittal Moving cursor to the first row... Displaying record... ID: 100, Age: 18, First: Zara, Last: Ali Moving cursor to the next row... Displaying record... ID: 101, Age: 25, First: Mahnaz, Last: Fatma Goodbye! C:\>