[轉(zhuǎn)載] 一、四種驅(qū)動(dòng)程序概念
A、JDBC-ODBC Bridge
??? 橋接器型的驅(qū)動(dòng)程序,這類(lèi)驅(qū)動(dòng)程序的特色是必須在使用者端的計(jì)算機(jī)上事先安裝好ODBC驅(qū)動(dòng)程序,然后通過(guò)JDBC-ODBC的調(diào)用方法,進(jìn)而通過(guò)ODBC來(lái)存取數(shù)據(jù)庫(kù)。
??? 作為JDK1.1后的一部分,是sun.jdbc.odbc包的一部分
Application--->JDBC-ODBC? Bridge---->JDBC-ODBC? Library--->ODBC? Driver-->Database
適用于快速的原型系統(tǒng),沒(méi)有提供JDBC驅(qū)動(dòng)的數(shù)據(jù)庫(kù)如Access
B、JDBC-Native API Bridge
??? 也是橋接器驅(qū)動(dòng)程序之一,這類(lèi)驅(qū)動(dòng)程序也必須先在使用者計(jì)算機(jī)上先安裝好特定的驅(qū)動(dòng)程序(類(lèi)似ODBC),然后通過(guò)JDBC-Native API橋接器的轉(zhuǎn)換,把Java API調(diào)用轉(zhuǎn)換成特定驅(qū)動(dòng)程序的調(diào)用方法,進(jìn)而存取數(shù)據(jù)庫(kù)。
??? 利用開(kāi)發(fā)商提供的本地庫(kù)來(lái)直接與數(shù)據(jù)庫(kù)通信。
Application--->JDBC? Driver---->Native? Database? library---->Database
比A類(lèi)性能略好。
C、JDBC-middleware
??? 這類(lèi)型的驅(qū)動(dòng)程序最大的好處就是省去了在使用者計(jì)算機(jī)上安裝任何驅(qū)動(dòng)程序的麻煩,只需在服務(wù)器端安裝好middleware,而middleware會(huì)負(fù)責(zé)所有存取數(shù)據(jù)庫(kù)必要的轉(zhuǎn)換。
??? Application--->Jdbc? Driver----->java? middleware--->JDBC? Driver---->Database
具有最大的靈活性,通常由那些非數(shù)據(jù)庫(kù)廠商提供,是四種類(lèi)型中最小的。
D、Pure JDBC driver
??? 這類(lèi)型的驅(qū)動(dòng)程序是最成熟的JDBC驅(qū)動(dòng)程序,不但無(wú)需在使用者計(jì)算機(jī)上安裝任何額外的驅(qū)動(dòng)程序,也不需要在服務(wù)器端安裝任何中介程序(middleware),所有存取數(shù)據(jù)庫(kù)的操作,都直接由驅(qū)動(dòng)程序來(lái)完成。
??? Application--->Jdbc? driver----->database? engine--->database
最高的性能,通過(guò)自己的本地協(xié)議直接與數(shù)據(jù)庫(kù)引擎通信,具備在Internet裝配的能力。
二、常用的JDBC類(lèi)與方法
1、DriverManager類(lèi):
??? 負(fù)責(zé)管理JDBC驅(qū)動(dòng)程序。使用JDBC驅(qū)動(dòng)程序之前,必須先將驅(qū)動(dòng)程序加載并向DriverManager注冊(cè)后才可以使用,同時(shí)提供方法來(lái)建立與數(shù)據(jù)庫(kù)的連接。
方法:
A、Class.forName(String driver); //加載注冊(cè)驅(qū)動(dòng)程序
B、Static Connection getConnection(String url,String user,String password) throws SQLException;?
??????? //取得對(duì)數(shù)據(jù)庫(kù)的連接
C、Static Driver getDriver(String url) throws SQLExcetion;
??????? //在已經(jīng)向DriverManager注冊(cè)的驅(qū)動(dòng)程序中尋找一個(gè)能夠打開(kāi)url所指定的數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序
2、Connection類(lèi)
??? 負(fù)責(zé)維護(hù)JSP/JAVA數(shù)據(jù)庫(kù)程序和數(shù)據(jù)庫(kù)之間的聯(lián)機(jī)。可以建立三個(gè)非常有用的類(lèi)對(duì)象。
方法:
A、Statement createStatement() throws SQLException; //建立Statement類(lèi)對(duì)象
?? Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException;??
??????? // 建立Statement類(lèi)對(duì)象
resultSetType值?
TYPE_FORWARD_ONLY 結(jié)果集不可滾動(dòng)?
TYPE_SCROLL_INSENSITIVE 結(jié)果集可滾動(dòng),不反映數(shù)據(jù)庫(kù)的變化?
TYPE_SCROLL_SENSITIVE 結(jié)果集可滾動(dòng),反映數(shù)據(jù)庫(kù)的變化?
resultSetConcurrency值?
CONCUR_READ_ONLY 不能用結(jié)果集更新數(shù)據(jù)?
CONCUR_UPDATABLE 能用結(jié)果集更新數(shù)據(jù)?
JDBC2.0中才支持滾動(dòng)的結(jié)果集,而且可以對(duì)數(shù)據(jù)進(jìn)行更新
B、DatabaseMetaData getMetaData() throws SQLException; //建立DatabaseMetaData類(lèi)對(duì)象
C、PreparedStatement prepareStatement(String sql) throws SQLException;?
??????? //建立PreparedStatement類(lèi)對(duì)象
D、boolean getAutoCommit() throws SQLException //返回Connection類(lèi)對(duì)象的AutoCommit狀態(tài)
E、void setAutoCommit(boolean autoCommit) throws SQLException?
??????? //設(shè)定Connection類(lèi)對(duì)象的AutoCommit狀態(tài)
F、void commit() throws SQLException? //確定執(zhí)行對(duì)數(shù)據(jù)庫(kù)新增、刪除或修改記錄的操作
G、void rollback() throws SQLException? //取消執(zhí)行對(duì)數(shù)據(jù)庫(kù)新增、刪除或修改記錄的操作
H、void close() throws SQLException? //結(jié)束Connection對(duì)象對(duì)數(shù)據(jù)庫(kù)的聯(lián)機(jī)
I、boolean isClosed() throws SQLException //測(cè)試是否已經(jīng)關(guān)閉Connection類(lèi)對(duì)象對(duì)數(shù)據(jù)庫(kù)的聯(lián)機(jī)
3、Statement類(lèi)
??? 通過(guò)Statement類(lèi)所提供的方法,可以利用標(biāo)準(zhǔn)的SQL命令,對(duì)數(shù)據(jù)庫(kù)直接新增、刪除或修改操作
方法:
A、ResultSet executeQuery(String sql) throws SQLException //使用SELECT命令對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)
B、int executeUpdate(String sql) throws SQLException?
??????? //使用INSERT\DELETE\UPDATE對(duì)數(shù)據(jù)庫(kù)進(jìn)行新增、刪除和修改操作。
C、void close() throws SQLException //結(jié)束Statement類(lèi)對(duì)象對(duì)數(shù)據(jù)庫(kù)的聯(lián)機(jī)
4、PreparedStatement類(lèi)
??? PreparedStatement類(lèi)和Statement類(lèi)的不同之處在于PreparedStatement類(lèi)對(duì)象會(huì)將傳入的SQL命令事先編好等待使用,當(dāng)有單一的SQL指令比多次執(zhí)行時(shí),用PreparedStatement類(lèi)會(huì)比Statement類(lèi)有效率
方法:
A、ResultSet executeQuery() throws SQLException //使用SELECT命令對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)
B、int executeUpdate() throws SQLException?
??????? //使用INSERT\DELETE\UPDATE對(duì)數(shù)據(jù)庫(kù)進(jìn)行新增、刪除和修改操作。
C、ResultSetMetaData getMetaData() throws SQLException
??????? //取得ResultSet類(lèi)對(duì)象有關(guān)字段的相關(guān)信息
D、void setInt(int parameterIndex,int x) throws SQLException
??????? //設(shè)定整數(shù)類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
E、void setFloat(int parameterIndex,float x) throws SQLException
??????? //設(shè)定浮點(diǎn)數(shù)類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
F、void setNull(int parameterIndex,int sqlType) throws SQLException
??????? //設(shè)定NULL類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
G、void setString(int parameterIndex,String x) throws SQLException
??????? //設(shè)定字符串類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
H、void setDate(int parameterIndex,Date x) throws SQLException
??????? //設(shè)定日期類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
I、void setTime(int parameterIndex,Time x) throws SQLException
??????? //設(shè)定時(shí)間類(lèi)型數(shù)值給PreparedStatement類(lèi)對(duì)象的IN參數(shù)
5、DatabaseMetaData類(lèi)
??? DatabaseMetaData類(lèi)保存了數(shù)據(jù)庫(kù)的所有特性,并且提供許多方法來(lái)取得這些信息。
方法:
A、String getDatabaseProductName() throws SQLException //取得數(shù)據(jù)庫(kù)名稱(chēng)
B、String getDatabaseProductVersion() throws SQLException //取得數(shù)據(jù)庫(kù)版本代號(hào)
C、String getDriverName() throws SQLException //取得JDBC驅(qū)動(dòng)程序的名稱(chēng)
D、String getDriverVersion()? throws SQLException //取得JDBC驅(qū)動(dòng)程序的版本代號(hào)
E、String getURL() throws SQLException //取得連接數(shù)據(jù)庫(kù)的JDBC URL
F、String getUserName() throws SQLException //取得登錄數(shù)據(jù)庫(kù)的使用者帳號(hào)
6、ResultSet類(lèi)
??? 負(fù)責(zé)存儲(chǔ)查詢(xún)數(shù)據(jù)庫(kù)的結(jié)果。并提供一系列的方法對(duì)數(shù)據(jù)庫(kù)進(jìn)行新增、刪除和修改操作。也負(fù)責(zé)維護(hù)一個(gè)記錄指針(Cursor),記錄指針指向數(shù)據(jù)表中的某個(gè)記錄,通過(guò)適當(dāng)?shù)囊苿?dòng)記錄指針,可以隨心所欲的存取數(shù)據(jù)庫(kù),加強(qiáng)程序的效率。
方法:
A、boolean absolute(int row) throws SQLException? //移動(dòng)記錄指針到指定的記錄
B、void beforeFirst() throws SQLException? //移動(dòng)記錄指針到第一筆記錄之前
C、void afterLast() throws SQLException? //移動(dòng)記錄指針到最后一筆記錄之后
D、boolean first() throws SQLException? //移動(dòng)記錄指針到第一筆記錄
E、boolean last() throws SQLException? //移動(dòng)記錄指針到最后一筆記錄
F、boolean next() throws SQLException? //移動(dòng)記錄指針到下一筆記錄
G、boolean previous() throws SQLException? //移動(dòng)記錄指針到上一筆記錄
H、void deleteRow() throws SQLException? //刪除記錄指針指向的記錄
I、void moveToInsertRow() throws SQLException? //移動(dòng)記錄指針以新增一筆記錄
J、void moveToCurrentRow() throws SQLException? //移動(dòng)記錄指針到被記憶的記錄
K、void insertRow() throws SQLException? //新增一筆記錄到數(shù)據(jù)庫(kù)中
L、void updateRow() throws SQLException? //修改數(shù)據(jù)庫(kù)中的一筆記錄
M、void update類(lèi)型(int columnIndex,類(lèi)型 x) throws SQLException? //修改指定字段的值
N、int get類(lèi)型(int columnIndex) throws SQLException? //取得指定字段的值
O、ResultSetMetaData getMetaData() throws SQLException //取得ResultSetMetaData類(lèi)對(duì)象
7、ResultSetMetaData類(lèi)
??? ResultSetMetaData類(lèi)對(duì)象保存了所有ResultSet類(lèi)對(duì)象中關(guān)于字段的信息,提供許多方法來(lái)取得這些信息。
方法:
A、int getColumnCount() throws SQLException //取得ResultSet類(lèi)對(duì)象的字段個(gè)數(shù)
B、int getColumnDisplaySize() throws SQLException //取得ResultSet類(lèi)對(duì)象的字段長(zhǎng)度
C、String getColumnName(int column) throws SQLException //取得ResultSet類(lèi)對(duì)象的字段名稱(chēng)
D、String getColumnTypeName(int column) throws SQLException //取得ResultSet類(lèi)對(duì)象的字段類(lèi)型名稱(chēng)
E、String getTableName(int column) throws SQLException //取得ResultSet類(lèi)對(duì)象的字段所屬數(shù)據(jù)表的名稱(chēng)
F、boolean isCaseSensitive(int column) throws SQLException //測(cè)試ResultSet類(lèi)對(duì)象的字段是否區(qū)分大小寫(xiě)
G、boolean isReadOnly(int column) throws SQLException //測(cè)試ResultSet類(lèi)對(duì)象的字段是否為只讀