JDBC常用類和方法

          [轉(zhuǎn)載] 一、四種驅(qū)動程序概念

          A、JDBC-ODBC Bridge
          ??? 橋接器型的驅(qū)動程序,這類驅(qū)動程序的特色是必須在使用者端的計算機上事先安裝好ODBC驅(qū)動程序,然后通過JDBC-ODBC的調(diào)用方法,進而通過ODBC來存取數(shù)據(jù)庫。
          ??? 作為JDK1.1后的一部分,是sun.jdbc.odbc包的一部分
          Application--->JDBC-ODBC? Bridge---->JDBC-ODBC? Library--->ODBC? Driver-->Database
          適用于快速的原型系統(tǒng),沒有提供JDBC驅(qū)動的數(shù)據(jù)庫如Access

          B、JDBC-Native API Bridge
          ??? 也是橋接器驅(qū)動程序之一,這類驅(qū)動程序也必須先在使用者計算機上先安裝好特定的驅(qū)動程序(類似ODBC),然后通過JDBC-Native API橋接器的轉(zhuǎn)換,把Java API調(diào)用轉(zhuǎn)換成特定驅(qū)動程序的調(diào)用方法,進而存取數(shù)據(jù)庫。
          ??? 利用開發(fā)商提供的本地庫來直接與數(shù)據(jù)庫通信。
          Application--->JDBC? Driver---->Native? Database? library---->Database
          比A類性能略好。

          C、JDBC-middleware
          ??? 這類型的驅(qū)動程序最大的好處就是省去了在使用者計算機上安裝任何驅(qū)動程序的麻煩,只需在服務(wù)器端安裝好middleware,而middleware會負(fù)責(zé)所有存取數(shù)據(jù)庫必要的轉(zhuǎn)換。
          ??? Application--->Jdbc? Driver----->java? middleware--->JDBC? Driver---->Database
          具有最大的靈活性,通常由那些非數(shù)據(jù)庫廠商提供,是四種類型中最小的。

          D、Pure JDBC driver
          ??? 這類型的驅(qū)動程序是最成熟的JDBC驅(qū)動程序,不但無需在使用者計算機上安裝任何額外的驅(qū)動程序,也不需要在服務(wù)器端安裝任何中介程序(middleware),所有存取數(shù)據(jù)庫的操作,都直接由驅(qū)動程序來完成。
          ??? Application--->Jdbc? driver----->database? engine--->database
          最高的性能,通過自己的本地協(xié)議直接與數(shù)據(jù)庫引擎通信,具備在Internet裝配的能力。


          二、常用的JDBC類與方法

          1、DriverManager類:
          ??? 負(fù)責(zé)管理JDBC驅(qū)動程序。使用JDBC驅(qū)動程序之前,必須先將驅(qū)動程序加載并向DriverManager注冊后才可以使用,同時提供方法來建立與數(shù)據(jù)庫的連接。

          方法:
          A、Class.forName(String driver); //加載注冊驅(qū)動程序
          B、Static Connection getConnection(String url,String user,String password) throws SQLException;?
          ??????? //取得對數(shù)據(jù)庫的連接
          C、Static Driver getDriver(String url) throws SQLExcetion;
          ??????? //在已經(jīng)向DriverManager注冊的驅(qū)動程序中尋找一個能夠打開url所指定的數(shù)據(jù)庫的驅(qū)動程序


          2、Connection類
          ??? 負(fù)責(zé)維護JSP/JAVA數(shù)據(jù)庫程序和數(shù)據(jù)庫之間的聯(lián)機??梢越⑷齻€非常有用的類對象。

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

          resultSetType值?
          TYPE_FORWARD_ONLY 結(jié)果集不可滾動?
          TYPE_SCROLL_INSENSITIVE 結(jié)果集可滾動,不反映數(shù)據(jù)庫的變化?
          TYPE_SCROLL_SENSITIVE 結(jié)果集可滾動,反映數(shù)據(jù)庫的變化?

          resultSetConcurrency值?
          CONCUR_READ_ONLY 不能用結(jié)果集更新數(shù)據(jù)?
          CONCUR_UPDATABLE 能用結(jié)果集更新數(shù)據(jù)?

          JDBC2.0中才支持滾動的結(jié)果集,而且可以對數(shù)據(jù)進行更新

          B、DatabaseMetaData getMetaData() throws SQLException; //建立DatabaseMetaData類對象
          C、PreparedStatement prepareStatement(String sql) throws SQLException;?
          ??????? //建立PreparedStatement類對象
          D、boolean getAutoCommit() throws SQLException //返回Connection類對象的AutoCommit狀態(tài)
          E、void setAutoCommit(boolean autoCommit) throws SQLException?
          ??????? //設(shè)定Connection類對象的AutoCommit狀態(tài)
          F、void commit() throws SQLException? //確定執(zhí)行對數(shù)據(jù)庫新增、刪除或修改記錄的操作
          G、void rollback() throws SQLException? //取消執(zhí)行對數(shù)據(jù)庫新增、刪除或修改記錄的操作
          H、void close() throws SQLException? //結(jié)束Connection對象對數(shù)據(jù)庫的聯(lián)機
          I、boolean isClosed() throws SQLException //測試是否已經(jīng)關(guān)閉Connection類對象對數(shù)據(jù)庫的聯(lián)機

          3、Statement類

          ??? 通過Statement類所提供的方法,可以利用標(biāo)準(zhǔn)的SQL命令,對數(shù)據(jù)庫直接新增、刪除或修改操作

          方法:

          A、ResultSet executeQuery(String sql) throws SQLException //使用SELECT命令對數(shù)據(jù)庫進行查詢
          B、int executeUpdate(String sql) throws SQLException?
          ??????? //使用INSERT\DELETE\UPDATE對數(shù)據(jù)庫進行新增、刪除和修改操作。
          C、void close() throws SQLException //結(jié)束Statement類對象對數(shù)據(jù)庫的聯(lián)機


          4、PreparedStatement類

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

          方法:

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


          5、DatabaseMetaData類

          ??? DatabaseMetaData類保存了數(shù)據(jù)庫的所有特性,并且提供許多方法來取得這些信息。

          方法:

          A、String getDatabaseProductName() throws SQLException //取得數(shù)據(jù)庫名稱
          B、String getDatabaseProductVersion() throws SQLException //取得數(shù)據(jù)庫版本代號
          C、String getDriverName() throws SQLException //取得JDBC驅(qū)動程序的名稱
          D、String getDriverVersion()? throws SQLException //取得JDBC驅(qū)動程序的版本代號
          E、String getURL() throws SQLException //取得連接數(shù)據(jù)庫的JDBC URL
          F、String getUserName() throws SQLException //取得登錄數(shù)據(jù)庫的使用者帳號

          6、ResultSet類

          ??? 負(fù)責(zé)存儲查詢數(shù)據(jù)庫的結(jié)果。并提供一系列的方法對數(shù)據(jù)庫進行新增、刪除和修改操作。也負(fù)責(zé)維護一個記錄指針(Cursor),記錄指針指向數(shù)據(jù)表中的某個記錄,通過適當(dāng)?shù)囊苿佑涗浿羔槪梢噪S心所欲的存取數(shù)據(jù)庫,加強程序的效率。

          方法:

          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? //新增一筆記錄到數(shù)據(jù)庫中
          L、void updateRow() throws SQLException? //修改數(shù)據(jù)庫中的一筆記錄
          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類對象中關(guān)于字段的信息,提供許多方法來取得這些信息。

          方法:

          A、int getColumnCount() throws SQLException //取得ResultSet類對象的字段個數(shù)
          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類對象的字段所屬數(shù)據(jù)表的名稱
          F、boolean isCaseSensitive(int column) throws SQLException //測試ResultSet類對象的字段是否區(qū)分大小寫
          G、boolean isReadOnly(int column) throws SQLException //測試ResultSet類對象的字段是否為只讀



          歡迎大家訪問我的個人網(wǎng)站 萌萌的IT人

          posted on 2006-04-05 09:29 見酒就暈 閱讀(113) 評論(0)  編輯  收藏 所屬分類: DB

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          BLOG

          FRIENDS

          LIFE

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 连州市| 土默特右旗| 日照市| 涡阳县| 阿拉善盟| 苍南县| 启东市| 常山县| 托里县| 出国| 宜兴市| 屯昌县| 洪雅县| 富民县| 清水县| 容城县| 白山市| 磐石市| 湄潭县| 清新县| 新余市| 漾濞| 曲水县| 武城县| 武强县| 荃湾区| 清水县| 陵水| 石首市| 峨边| 孝义市| 万宁市| 麻栗坡县| 南汇区| 古田县| 沅陵县| 疏勒县| 霍州市| 松原市| 广灵县| 静海县|