Java JDBC API JDBC Driver Manager JDBCODBC Bridge
Java应用程序 JDBC API JDBC Driver. Manager JDBC-ODBC Bridge JDBC-Native API Bridge JDBC-Middleware ODBC Driver 特定驱动程序 Middleware on net server 数据库 数据库 Pure JDBC driver 数据库
1、java. sql. Driver. Manager(1) • Public static synchronized Connection get. Connection(String url)throws SQLException:使用指定的数据库URL创建一个连 接,使Driver. Manager从注册的JDBC驱动程序中选择适当的驱 动程序,如果发生数据库访问异常,则抛出一个SQLException 错误 • Public static synchronized Connection get. Connection(String url, Properties info)throws SQLException:使用指定的数据库 URL和相关信息(用户名、密码等属性列表)来创建一个连接, 使Driver. Manager从注册的JDBC驱动程序中选择适当的驱动程 序,如果发生数据库访问异常,则抛出一个SQLException错误 • Public static synchronized Connection get. Connection(String url, String user, String password)throws SQLException:使用指 定的数据库URL和用户名、密码来创建一个连接,使 Driver. Manager从注册的JDBC驱动程序中选择适当的驱动程序, 如果发生数据库访问异常,则抛出一个SQLException错误
java. sql. Driver. Manager(2) • Public static Driver get. Driver(String url)throws SQLException:定位给定URL下的驱动程序,让 Driver. Manager从注册的JDBC驱动程序中选择一个适 当的驱动程序,如果发生数据库访问异常,则抛出一 个SQLException错误 • Public static void deregister. Driver(Driver driver)throws SQLException:从Driver. Manager列表 中删除指定的驱动程序,如果发生数据库访问异常, 则抛出一个SQLException错误 • Public static int get. Login. Timeout():用来获取连接数 据库时驱动程序可以等待的最大时间,以秒为单位 • Public static Print. Stream get. Log. Stream():用来获取 Driver. Manager和所有驱动程序使用的日志 Print. Stream对象 • Public static void println(String message):用来给当 前JDBC日志流输出指定的消息
2、Connection类(1) • Public Statement create. Statement()throws SQLException:用来创建Statement对象 • Public Statement create. Statement(int result. Set. Type, int result. Set. Concurrecy)throws SQLException:用来按指定参数创建Statement对象 • Public Database. Meta. Data get. Meta. Data()throws SQLException:用来创建Database. Meta. Data对象, 不同数据库系统拥有不同的特性, Database. Meta. Data类可以保存数据库的所有特性, 并且还提供一系列成员方法获取数据库特性,如:数 据库名称、JDBC驱动程序名、版本代号以及连接数 据库的JDBC URL • Public Prepared. Statement prepare. Statement(String sql)throws SQLException:用来创建 Prepared. Statement对象
Connection类(2) • Public void commit()throws SQLException:用来提交对数 据库执行添加、修改或删除纪录的操作 • Public boolean get. Auto. Commit()throws SQLException: 用来获取connection对象的Auto_Commit(自动提交)状 态 • Public boolean set. Auto. Commit(boolean auto. Commit)throws SQLException:用来设定Connection 类对象的Auto_Commit(自动提交)状态,如果将 Connection类对象的auto. Commit设置为true,则它的每一 个sql语句将作为一个独立的事务被提交和执行 • Public void rollback()throws SQLException:用来取消对 数据库执行过的添加、删除或修改纪录操作,将数据库恢 复到执行这些操作前的状态 • Public void close()throws SQLException:用来断开 Connection对象与数据库的连接 • Public boolean is. Closed()throws SQLException:用来测 试Connection类对象是否已经关闭
3、Statement类(1) • Public Result. Set execute. Query(String sql)throws SQLException:用来执行指定的sql查询语句,返回查询 结果,如果发生数据库访问错误,则程序抛出一个 SQLException错误 • Public int execute. UPdate(String sql)throws SQLException:用来执行SQL的Insert、Update和Delete 语句,返回值是插入、修改或删除的纪录函数 • Public boolean execute(String sql)throws SQLException: 用来执行指定的sql查询语句,执行结果有多种情况,如 果执行结果为一个结果集对象,则返回true,其他情况返 回false • Public Result. Set get. Result. Set()throws SQLException: 用来获取Result. Set对象的当前结果集,对每一个结果只 调用一次
Statement类(2) • Public int get. Update. Count()throws SQLException:用来 获取当前结果的更新记录数,如果结果是一个Result. Set 对象或没有更多的结果,则返回-1 • Public void clear. Warnings(String sql)throws SQLException:用来清除Statement对象产生的所有警告 信息 • Public void close()throws SQLException:用来释放 Statement对象的数据库和JDBC资源
4、Pre. Pared. Statement类(1) • Public Result. Set execute. Query()throws SQLException: 使用SQL子另SELECT对数据库进行纪录查询操作,并 返回Result. Set对象 • Public int execute. Update()throws SQLException:使 用SQL指令INSERT、DELETE、UPDATE对数据库进 行添加、删除和修改纪录的操作 • Public void set. Date(int parameter. Index, Date x)throws SQLException:用来给指定位置的参数设定日期型数 值 • Public void Set. Time(int parameter. Index, Time x)throws SQLException:用来给指定位置的参数设定时间型数 值
Pre. Pared. Statement类(2) • Public void set. Double(int parameter. Index, double x)throws SQLException:用给定位置的参数设定 Double型数值 • Public void set. Float(int parameter. Index, float x)throws SQLException:用给定位置的参数设定 float型数值 • Public void set. Int(int parameter. Index, int x)throws SQLException:用给定位置的参数设定int型数值 • Public void set. Null(int parameter. Index, int sql. Type)throws SQLException:用给定位置的参数 设定Null型数值
5、Result. Set类(1) 成员方法 功能说明 Public boolean absolute(int row)throws SQLException 移动纪录指针到指定的纪 录 Public boolean first()throws SQLException 移动纪录指针到第一个纪 录 Public void beforefirst()throws SQLException 移动纪录指针到第一个纪 录之前 Public boolean last()throws SQLException 移动纪录指针到最后一个 记录 Public void afterlast()throws SQLException 移动纪录指针到最后一个 记录之后 Public boolean previous()throws SQLException 移动纪录指针到上一个记 录
Result. Set类(2) 成员方法 功能说明 Public boolean next()throws SQLException 移动纪录指针到下一个 记录 Public void insert. Row()throws SQLException 插入一个记录到数据表 中 Public void update. Row()throws SQLException 修改数据表中的一个记 录 Public void delete. Row()throws SQLException 删除纪录指针指向的纪 录 Public void update类型(int Column. Index, 类型 x)throws SQLException 修改数据表中指定字符 的值 Public int get类型(int Column. Index)throws SQLException 取得数据表中指定字符 的值
13. 3 JDBC编程实例 //创 建 顾 客 consumer表 , 此 表 有 三 个 字 段 : 顾 客 编 号 id, 姓 名 name, 购 物 总 金 额 totalmoney import java. sql. *; public class Create_Table {public static void main(String[] args) {String JDriver="sun. jdbc. odbc. Jdbc. Odbc. Driver"; //声明jdbc驱动程序类型 String con. URL="jdbc: odbc: Test. DB"; //定义jdbc的url对象 try { Class. for. Name(JDriver); } //加载jdbc-odbc桥驱动程序 catch(java. lang. Class. Not. Found. Exception e) { System. out. println("forname: "+e. get. Message()); } try {Connection con=Driver. Manager. get. Connection(con. URL); //连接数据库URL Statement s=con. create. Statement(); //建立Statement类对象 //创建一个含有三个字段的顾客表conumser String query="create table consumer(id char(10), name char(15), totalmoney integer)"; s. execute. Update(query); //执行SQL语句 s. close(); //释放statement所连接的数据库及jdbc资源 con. close(); //关闭与数据库的连接 } catch(SQLException e) { System. out. println("SQLException: "+e. get. Message()); } } }
示例程序 Insert_Record //在上例创建的数据表consumer中插入三个顾客的纪录 import java. sql. *; public class Insert_Record {public static void main(String[] args) {String JDriver="sun. jdbc. odbc. Jdbc. Odbc. Driver"; String con. URL="jdbc: odbc: Test. DB"; try{ Class. for. Name(JDriver); } catch(java. lang. Class. Not. Found. Exception e) { System. out. println("forname: "+e. get. Message()); } try{ Connection con=Driver. Manager. get. Connection(con. URL); //使用SQL命令insert插入三条顾客纪录到表中 Statement s=con. create. Statement(); String r 1="insert into consumer values('00001', '王明', 360)"; String r 2="insert into consumer values('00002', '高强', 728)"; String r 3="insert into consumer values('00003', '李丽', 1182)"; s. execute. Update(r 1); s. execute. Update(r 2); s. execute. Update(r 3); s. close(); con. close(); } catch(SQLException e) { System. out. println("SQLException: "+e. get. Message()); } } }
示例程序 Update_Record //修改上例中的第二条和第三条纪录的顾客总消费金额字段的值,并把数据表的内容 输出到屏幕上 import java. sql. *; public class Update_Record {public static void main(String[] args) { String Jdriver="sun. jdbc. odbc. Jdbc. Odbc. Driver"; String con. URL="jdbc: odbc: Test. DB"; String[] id={"00002", "00003"}; int[] totalmoney={989, 1260}; try { Class. for. Name(Jdriver); } catch(java. lang. Class. Not. Found. Exception e) { System. out. println("forname: "+e. get. Message()); } try { Connection con=Driver. Manager. get. Connection(con. URL);
//修改数据库中数据表的内容 Prepared. Statement ps=con. prepare. Statement("UPDATE consumer set totalmoney=? where id=? "); int i=0, idlen=id. length; do { ps. set. Int(1, totalmoney[i]); ps. set. String(2, id[i]); ps. execute. Update(); ++i; }while(i<id. length); ps. close(); //查询数据库并把数据表的内容输出到屏幕上 Statement s=con. create. Statement(); Result. Set rs=s. execute. Query("select * from consumer"); while(rs. next()) {System. out. println(rs. get. String("id")+"t"+rs. get. String("name")+"t"+rs. get. Int("totalmoney")); } s. close(); con. close(); } catch(SQLExceptione) { System. out. println("SQLException: "+e. get. Message()); } } }
示例程序 Delete_Record //在上例创建的数据表consumer中删除第二条记录,然后把数据表的内容输出 import java. sql. *; public class Delete_Record {public static void main(String[] args) {String Jdriver="sun. jdbc. odbc. Jdbc. Odbc. Driver"; String con. URL="jdbc: odbc: Test. DB"; try { Class. for. Name(Jdriver); } catch(java. lang. Class. Not. Found. Exception e) {System. out. println("forname: "+e. get. Message()); } try { Connection con=Driver. Manager. get. Connection(con. URL); Statement s=con. create. Statement(); //删除第二条记录 Prepared. Statement ps=con. prepare. Statement("delete from consumer where id=? "); ps. set. String(1, "00002"); ps. execute. Update(); ps. close(); //查询数据库并把数据表的内容输出到屏幕上 Result. Set rs=s. execute. Query("select * from consumer"); while(rs. next()) {System. out. println(rs. get. String("id")+"t"+rs. get. String("name")+"t"+rs. get. Int("total money")); } s. close(); con. close(); } catch(SQLException e) {System. out. println("SQLException: "+e. get. Message()); } }}
- Slides: 41