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 阅读(1549) 评论(0)  编辑  收藏 所属分类: SQLServer

只有注册用户登录后才能发表评论。


网站导航: