posts - 188,comments - 176,trackbacks - 0

          一、四種驅動程序概念

           

          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、DatabaseData getData() throws SQLException; //建立DatabaseData類對象


          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、ResultSetData getData() 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、DatabaseData類

              DatabaseData類保存了數據庫的所有特性,并且提供許多方法來取得這些信息。

          方法:

          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、ResultSetData getData() throws SQLException //取得ResultSetData類對象

           

          7、ResultSetData類

              ResultSetData類對象保存了所有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類對象的字段是否為只讀

          posted on 2007-05-24 11:41 cheng 閱讀(1567) 評論(0)  編輯  收藏 所屬分類: SQLServer
          主站蜘蛛池模板: 库伦旗| 托克托县| 偏关县| 剑阁县| 江安县| 宜春市| 梅河口市| 潮安县| 准格尔旗| 嘉禾县| 昆明市| 黄陵县| 绍兴市| 江门市| 米林县| 丁青县| 巴青县| 凌云县| 隆化县| 木兰县| 石林| 尼玛县| 新化县| 南部县| 河南省| 公主岭市| 张掖市| 济源市| 涟水县| 达尔| 监利县| 沙河市| 九龙县| 扶余县| 广东省| 清水县| 保定市| 永年县| 东阳市| 龙南县| 铅山县|