第十四章 JDBC

java data base connectivity

 

1.JDBC结构


JDBC的体系结构

2.JDBC变成要点

在java中使用数据库进行JDBC编程时,java程序中通常应包含以下几部分:

(1)在程序的首部用import语句将Java.sql包引出程序

import java.sql.*;

(2)使用Class.forName()方法加载相应数据库的JDBC驱动程序,若以加载jdbc-odbc桥为例:

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver);

(3)定义JDBC的URL对象

String conURL=”jdbc:odbc:TestDB”;   //TestDB是要创建的数据源

(4)连接数据库

connection s=DriverManager.getConnection(conURL);

(5)使用SQL语句对数据库进行操作

(6)使用close()方法解除Java与数据库的连接并关闭数据库。

 

3.常用的JDBC类与方法

    1、DriverManager类:

负责管理JDBC驱动程序。使用JDBC驱动程序之前,必须先将驱动程序加载并向DriverManager注册后才可以使用。加载和注册驱动程序可以使用Class.forName()方法来完成。

java.sql.DriverManager类提供的常用成员方法:

    Static synchronized Connection getConnection(String url) throws SQLException;  //取得对数据库的连接

Static synchronized Connection getConnection(String url,Properities info) throws SQLExcetion;   //指定数据库及信息创建连接

Static synchronized Connection getConnection(String url,String user,String password) throws SQLExcetion;   //指定数据库的url、用户名、密码创建连接

Static Driver getDriver(String url)throws SQLExcetion;  //定位在给定的url下的驱动程序,让DriverManager从注册的JDBC驱动程序选择一个适当的驱动程序

Static void println(String message)  //给当前的JDBC日志流输出指定的消息。

    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类对象的字段是否为只读

 

 

安装ODBC驱动:

控制面板-管理工具-数据源(ODBC)-用户DSN-用户数据源(U):。。。。。确定。

 

 

 

 

 

posted @ 2012-11-15 19:04  i Traveling Light  阅读(115)  评论(0)    收藏  举报