JavaWeb學習之JDBC API中常用的接口和類


JDBC API中包含四個常用的接口和一個類分別是:

1、Connection接口

2、Statement接口

3、PreparedStatement接口

4、ResultSet接口

5、DriverManager類

下面一一介紹這四個接口和一個類

(1)Connection接口

Connection接口位於java.sql包當中,是與數據庫連接會的對象,只有獲得特定的數據庫連接對象,才可以訪問數據庫進行數據庫操作。在進行數據庫連接的時候還要用到DriverManager類中的getConnection(url,username,password)方法。例如:

1 String url = "jdbc:mysql://localhost:3306/mysqltest";
2             //數據庫用戶名
3             String userName = "root";
4             //數據庫密碼
5             String passWord = "123456";
6             //創建Connection連接
7             Connection conn = (Connection)DriverManager.getConnection(url,userName,passWord);

 

此外該接口中還有close()方法用於關閉數據庫連接,但是該數據庫還是會占用jdbc資源。

(2)DriverManager類

該類中包含了與數據庫交互操作的方法,該類中的方法全部有數據庫廠商提供。DriverManager類中有6個常用的也是重要的方法分別是:

public static void deregisterDriver(Driver driver) throws SQLException

該方法是從DriverManager的管理列表中刪除一個驅動程序。其中driver參數是要刪除的驅動對象。

public static Connection getConnection(String url) throws SQLException

該方法是根據指定數據庫連接URL,建立與數據庫連接Connection。其中參數url是數據庫連接的URL

public static Connection getConnection(String url,Properties info) 

該方法是根據指定數據庫連接URL,以及數據庫連接屬性信息建立數據庫連接Connection。其中參數url為數據庫連接URL,參數info是數據庫連接屬性。

public static Connection getConnection(String url,String user,String password) throws SQLException

該方法是根據數據庫連接URL、用戶名以及密碼建立數據庫連接Connection。參數url是數據庫連接URL,參數user是連接數據庫的用戶名,參數password是連接數據庫的密碼。

public static Enumeration<Driver>getDrivers()該方法是獲取當前DriverManager中已加載的所有驅動程序,它的返回值為Enumeration。

public static void registerDriver(Driver driver) throws SQLException

該方法是向DriverManager注冊一個驅動對象,參數driver是要注冊的驅動。

(3)Statement接口

Statement接口是Java程序執行數據庫操作的重要接口,用於已經建立數據庫連接的基礎之上,向數據庫發送要執行的SQL語句。它用於執行不帶參數的簡單SQL語句。該接口中包含九個常用的重要方法。

void addBatch(String sql) throws SQLException該方法是將SQL語句添加到此Statement對象的當前命令列表中,此方法用於SQL命令的批處理。

void clearBatch() throws SQLException該方法是清空Statement對象中的命令列表。

void close() throws SQLException 該方法是立即釋放此Statement對象的數據庫和JDBC資源,而不是等待該對象自動關閉時發生此操作。

boolean excute(String sql) throws SQLException 該方法是執行指定的SQL語句。如果sql語句返回結果,此方法返回true,否則返回false。

int[] excuteBatch() throws SQLException 該方法是將一批SQL命令提交給數據庫執行,返回更新計數組成的數組。

ResultSet excuteQuery(String sql) throws SQLException 該方法是執行查詢類型(select)的SQL語句,此方法返回查詢所獲取的結果集ResultSet對象。

excuteUpdate int excuteUpdate(String sql) throws SQLException 該方法執行SQL語句中DML類型(insert、update、delete)的SQL語句,返回更新所影響的行數。

Connection getConnection() throws SQLException 該方法獲取生成此Statement對象的Connection對象

boolean isClosed() throws SQLException 該方法用來判斷Statement對象是否已被關閉,如果Statement對象被關閉,則不能再調用此Statement對象執行SQL語句,此方法返回布爾值。

(4)PreparedStatement接口

PreparedStatement接口位於java.servlet包當中,它繼承了Statement,但是PreparedStatement與Statement有這兩方面的不同,第一:由於 PreparedStatement 對象已預編譯過,所以其執行速度要快於 Statement 對象。因此,多次執行的 SQL 語句經常創建為 PreparedStatement 對象,以提高效率。

作為 Statement 的子類,PreparedStatement 繼承了 Statement 的所有功能。另外它還添加了一整套方法,用於設置發送給數據庫以取代 IN 參數占位符的值。

創建過程:

以下的代碼段(其中 con 是 Connection 對象)創建包含帶兩個 IN 參數占位符的 SQL 語句的 PreparedStatement 對象:
1 PreparedStatement pstmt = con.prepareStatement("UPDATE table4 SET m = ? WHERE x = ?");
查看代碼

pstmt 對象包含語句 "UPDATE table4 SET m = ? WHERE x = ?",它已發送給DBMS,並為執行作好了准備。

void setBinaryStream(int parameterIndex,InputStream x) throws SQLException  將輸入流x作為SQL語句中的參數值,parameterIndex是參數位置索引。

void setBoolean(int parameterIndex,boolean x) throws SQLException 將布爾值x作為SQL語句中的參數值,parameterIndex為參數位置索引。

void setByte(int parameterIndex,byte x) throws SQLException 將byte值x作為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setDate(int parameterIndex,Date x) throws SQLException 將java.sql.Date值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setDouble(int parameterIndex,double x) 將double值x做為SQL語句中的參數值,parameterIndex為參數索引。

void setFloat(int parameterIndex,floatx) throws SQLException 將float值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setInt(int parameterIndex,int x) throws SQLException 將int值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setInt(int parameterIndex,long x) throws SQLException 將long值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setObject(int parameterIndex,Object x) throws SQLException 將object對象x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setShort(int parameterIndex,short x) throws SQLException 將short值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

void setString(int parameterIndex,String x) throws SQLException 將String值x做為SQL語句的參數值,parameterIndex為參數位置的索引。

void setTimestamp(int parameterIndex,Timestamp x) throws SQLException 將java.sql.Timestamp值x做為SQL語句中的參數值,parameterIndex為參數位置的索引。

(5)Result接口

數據庫結果集的結果表,通常通過查詢數據庫的語句生成。

以下代碼片段(其中 con 為有效的 Connection 對象)演示了如何生成可滾動且不受其他更新影響的、可更新的結果集。請參閱 ResultSet 字段以了解其他選項。
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2"); // rs will be scrollable, will not show changes made by others, // and will be updatable
查看代碼

 

ResultSet 接口提供用於從當前行檢索列值的獲取方法(getBoolean、getLong 等)。可以使用列的索引編號或列的名稱檢索值。一般情況下,使用列索引較為高效。列從 1 開始編號。為了獲得最大的可移植性,應該按從左到右的順序讀取每行中的結果集列,而且每列只能讀取一次。
ResultSet接口中常用的方法有:
boolean absolute(int row) throws SQLException 將光標移動到此ResultSet對象的給定行編號,參數row為行編號。
void afterLast() throws SQLException 將光標移動到此ResultSet對象的最后一行之后,如果結果集中不包含任何行,則此方法無效。
void beforeFirst() throws SQLException 立即釋放此ResultSet對象的數據庫和JDBC資源
void deleteRow() throws SQLException 從此ResultSet對象和底層數據庫中刪除當前行。
boolean first() throws SQLException 將光標移動到此ResultSet對象的第一行
InputStream getBinaryStream(String columnLabel) throws SQLException 以byte流的方式獲取ResultSet對象當前行中指定列的值,參數columnLabel為列名稱。
Date getDate(String columnLabel) throws SQLException 以java.sql.Date的方式獲取ResultSet對象當前行中指定列的值,參數columnLabel為列名稱。
double getDouble(String columnLabel) throws SQLException 以double的方式獲取ResultSet對象當前行中指定列的值,參數columnLabel為列名稱。
float getFloat(String columnLabel) throws SQLException 以float的方式獲取ResultSet對象當前行中指定列的值,參數columnLabel為列名稱
int getInt(String columnLabel) throws SQLException 以int的方式獲取ResultSet對象當前行中指定的列的值,參數columnLabel為列名稱。
String getString(String columnLabel) throws SQLException 以String的方式獲取ResultSet對象當前行中指定列的值,參數columnLabel為列名稱。
boolean isClosed() throws SQLException 判斷當前ResultSet對象是否已關閉。
boolean last() throws SQLException 將光標移動到此ResultSet對象的最后一行。
boolean next() throws SQLException 將光標位置向后移動一行,如移動的新行有效返回true,否則返回false。
boolean previous() throws SQLException 將光標位置向前移動一行,如移動的新行有效返回true,否則返回false。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM