JDBC常用類和方法

來源:互聯網
上載者:User
一、四種驅動程式概念

A、JDBC-ODBC Bridge
    橋接器型的驅動程式,這類驅動程式的特色是必須在使用者端的電腦上事先安裝好ODBC驅動程式,然後通過JDBC-ODBC的調用方法,進而通過ODBC來存取資料庫。
    作為JDK1.1後的一部分,是sun.jdbc.odbc包的一部分
Application--->JDBC-ODBC  Bridge---->JDBC-ODBC  Library--->ODBC  Driver-->Database
適用於快速的原型系統,沒有提供JDBC驅動的資料庫如Access

B、JDBC-Native API Bridge
    也是橋接器驅動程式之一,這類驅動程式也必須先在使用者電腦上先安裝好特定的驅動程式(類似ODBC),然後通過JDBC-Native API橋接器的轉換,把Java API調用轉換成特定驅動程式的調用方法,進而存取資料庫。
    利用開發商提供的本地庫來直接與資料庫通訊。
Application--->JDBC  Driver---->Native  Database  library---->Database
比A類效能略好。

C、JDBC-middleware
    這類型的驅動程式最大的好處就是省去了在使用者電腦上安裝任何驅動程式的麻煩,只需在伺服器端安裝好middleware,而middleware會負責所有存取資料庫必要的轉換。
    Application--->Jdbc  Driver----->java  middleware--->JDBC  Driver---->Database
具有最大的靈活性,通常由那些非資料庫廠商提供,是四種類型中最小的。

D、Pure JDBC driver
    這類型的驅動程式是最成熟的JDBC驅動程式,不但無需在使用者電腦上安裝任何額外的驅動程式,也不需要在伺服器端安裝任何中介程式(middleware),所有存取資料庫的操作,都直接由驅動程式來完成。
    Application--->Jdbc  driver----->database  engine--->database
最高的效能,通過自己的本地協議直接與資料庫引擎通訊,具備在Internet裝配的能力。

二、常用的JDBC類與方法

1、DriverManager類:
    負責管理JDBC驅動程式。使用JDBC驅動程式之前,必須先將驅動程式載入並向DriverManager註冊後才可以使用,同時提供方法來建立與資料庫的串連。

方法:
A、Class.forName(String driver); //載入註冊驅動程式
B、Static Connection getConnection(String url,String user,String password) throws SQLException;  
        //取得對資料庫的串連
C、Static Driver getDriver(String url) throws SQLExcetion;
        //在已經向DriverManager註冊的驅動程式中尋找一個能夠開啟url所指定的資料庫的驅動程式

2、Connection類
    負責維護JSP/JAVA資料庫程式和資料庫之間的聯機。可以建立三個非常有用的類對象。

方法:
A、Statement createStatement() throws SQLException; //建立Statement類對象
   Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException;   
        // 建立Statement類對象

resultSetType值  
TYPE_FORWARD_ONLY 結果集不可滾動  
TYPE_SCROLL_INSENSITIVE 結果集可滾動,不反映資料庫的變化  
TYPE_SCROLL_SENSITIVE 結果集可滾動,反映資料庫的變化  

resultSetConcurrency值  
CONCUR_READ_ONLY 不能用結果集更新資料  
CONCUR_UPDATABLE 能用結果集更新資料  

JDBC2.0中才支援滾動的結果集,而且可以對資料進行更新

B、DatabaseMetaData getMetaData() throws SQLException; //建立DatabaseMetaData類對象
C、PreparedStatement prepareStatement(String sql) throws SQLException;  
        //建立PreparedStatement類對象
D、boolean getAutoCommit() throws SQLException //返回Connection類對象的AutoCommit狀態
E、void setAutoCommit(boolean autoCommit) throws SQLException  
        //設定Connection類對象的AutoCommit狀態
F、void commit() throws SQLException  //確定執行對資料庫新增、刪除或修改記錄的操作
G、void rollback() throws SQLException  //取消執行對資料庫新增、刪除或修改記錄的操作
H、void close() throws SQLException  //結束Connection對象對資料庫的聯機
I、boolean isClosed() throws SQLException //測試是否已經關閉Connection類對象對資料庫的聯機

3、Statement類

    通過Statement類所提供的方法,可以利用標準的SQL命令,對資料庫直接新增、刪除或修改操作

方法:

A、ResultSet executeQuery(String sql) throws SQLException //使用SELECT命令對資料庫進行查詢
B、int executeUpdate(String sql) throws SQLException  
        //使用INSERT/DELETE/UPDATE對資料庫進行新增、刪除和修改操作。
C、void close() throws SQLException //結束Statement類對象對資料庫的聯機

4、PreparedStatement類

    PreparedStatement類和Statement類的不同之處在於PreparedStatement類對象會將傳入的SQL命令事先編好等待使用,當有單一的SQL指令比多次執行時,用PreparedStatement類會比Statement類有效率

方法:

A、ResultSet executeQuery() throws SQLException //使用SELECT命令對資料庫進行查詢
B、int executeUpdate() throws SQLException  
        //使用INSERT/DELETE/UPDATE對資料庫進行新增、刪除和修改操作。
C、ResultSetMetaData getMetaData() throws SQLException
        //取得ResultSet類對象有關欄位的相關資訊
D、void setInt(int parameterIndex,int x) throws SQLException
        //設定整數類型數值給PreparedStatement類對象的IN參數
E、void setFloat(int parameterIndex,float x) throws SQLException
        //設定浮點數類型數值給PreparedStatement類對象的IN參數
F、void setNull(int parameterIndex,int sqlType) throws SQLException
        //設定NULL類型數值給PreparedStatement類對象的IN參數
G、void setString(int parameterIndex,String x) throws SQLException
        //設定字串類型數值給PreparedStatement類對象的IN參數
H、void setDate(int parameterIndex,Date x) throws SQLException
        //設定日期類型數值給PreparedStatement類對象的IN參數
I、void setTime(int parameterIndex,Time x) throws SQLException
        //設定時間類型數值給PreparedStatement類對象的IN參數

5、DatabaseMetaData類

    DatabaseMetaData類儲存了資料庫的所有特性,並且提供許多方法來取得這些資訊。

方法:

A、String getDatabaseProductName() throws SQLException //取得資料庫名稱
B、String getDatabaseProductVersion() throws SQLException //取得資料庫版本代號
C、String getDriverName() throws SQLException //取得JDBC驅動程式的名稱
D、String getDriverVersion()  throws SQLException //取得JDBC驅動程式的版本代號
E、String getURL() throws SQLException //取得串連資料庫的JDBC URL
F、String getUserName() throws SQLException //取得登入資料庫的使用者帳號

6、ResultSet類

    負責儲存查詢資料庫的結果。並提供一系列的方法對資料庫進行新增、刪除和修改操作。也負責維護一個記錄指標(Cursor),記錄指標指向資料表中的某個記錄,通過適當的移動記錄指標,可以隨心所欲的存取資料庫,加強程式的效率。

方法:

A、boolean absolute(int row) throws SQLException  //移動記錄指標到指定的記錄
B、void beforeFirst() throws SQLException  //移動記錄指標到第一筆記錄之前
C、void afterLast() throws SQLException  //移動記錄指標到最後一筆記錄之後
D、boolean first() throws SQLException  //移動記錄指標到第一筆記錄
E、boolean last() throws SQLException  //移動記錄指標到最後一筆記錄
F、boolean next() throws SQLException  //移動記錄指標到下一筆記錄
G、boolean previous() throws SQLException  //移動記錄指標到上一筆記錄
H、void deleteRow() throws SQLException  //刪除記錄指標指向的記錄
I、void moveToInsertRow() throws SQLException  //移動記錄指標以新增一筆記錄
J、void moveToCurrentRow() throws SQLException  //移動記錄指標到被記憶的記錄
K、void insertRow() throws SQLException  //新增一筆記錄到資料庫中
L、void updateRow() throws SQLException  //修改資料庫中的一筆記錄
M、void update類型(int columnIndex,類型 x) throws SQLException  //修改指定欄位的值
N、int get類型(int columnIndex) throws SQLException  //取得指定欄位的值
O、ResultSetMetaData getMetaData() throws SQLException //取得ResultSetMetaData類對象

7、ResultSetMetaData類

    ResultSetMetaData類對象儲存了所有ResultSet類對象中關於欄位的資訊,提供許多方法來取得這些資訊。

方法:

A、int getColumnCount() throws SQLException //取得ResultSet類對象的欄位個數
B、int getColumnDisplaySize() throws SQLException //取得ResultSet類對象的欄位長度
C、String getColumnName(int column) throws SQLException //取得ResultSet類對象的欄位名稱
D、String getColumnTypeName(int column) throws SQLException //取得ResultSet類對象的欄位類型名稱
E、String getTableName(int column) throws SQLException //取得ResultSet類對象的欄位所屬資料表的名稱
F、boolean isCaseSensitive(int column) throws SQLException //測試ResultSet類對象的欄位是否區分大小寫
G、boolean isReadOnly(int column) throws SQLException //測試ResultSet類對象的欄位是否為唯讀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.