JavaWeb學習之JDBC API中常用的介面和類,javawebjdbc
JDBC API中包含四個常用的介面和一個類分別是:
1、Connection介面
2、Statement介面
3、PreparedStatement介面
4、ResultSet介面
5、DriverManager類
下面一一介紹這四個介面和一個類
(1)Connection介面
Connection介面位於java.sql包當中,是與資料庫連接會的對象,只有獲得特定的資料庫連接對象,才可以訪問資料庫進行資料庫操作。在進行資料庫連接的時候還要用到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。