第12节 设计数据存取类.ppt

  1. 1、本文档共80页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第12章 设计数据存取类 访问数据库的标准查询语言; DA类可执行4项基本任务:检索客户、存储客户、更改客户、删除客户。分别使用方法find、addNew、update和delete实现。 这4个方法调用Statement类中的方法来执行SQL语句。 java.sql包包含使用关系数据库时会调用的方法 类DriverManager:getConnection方法可建立到该数据库的连接并返回一个连接实例。 类Connection:createStatement方法可返回一个语句实例,用于执行SQL语句。 类Statement:包含执行SQL语句的方法。 类ResultSet:包含由Statement方法executeQuery返回的数据实例。 initialize方法:与数据库建立连接; find, addNew, update, delete, getAll: 直接访问数据库; terminate方法: 切断与数据库的连接。 使用前面示例中的Customer和Tester类对使用关系数据库的CustomerDA类进行测试, 结果相同. 1. initialize方法 加载JDBC-ODBC驱动程序,创建链接到该数据库的一个连接实例。 通过调用Class类的forName方法来加载该驱动程序; 通过调用DriverManager类中的getConnection方法创建连接实例。 12.5.4 使用数据库实现持久性 public static void initialize() {try { // load the jdbc - odbc bridge driver for Windows Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); // create connection instance with no username and password aConnection = DriverManager.getConnection(url, , ); // create statement object instance for this connection aStatement = aConnection.createStatement(); } catch (ClassNotFoundException e) {System.out.println(e);} catch (SQLException e) { System.out.println(e); } } 12.5.4 使用数据库实现持久性 编写TesterOne类,完成以下任务: 创建两个新的客户实例; 调用initialize方法; 调用addNew添加两个要存储的新客户; 调用find检索对第一位客户的引用; 调用getAll以检索对这两位客户的引用; 为第二位客户调用delete并验证删除; 更改第一位客户的地址并验证更改; 调用terminate方法。 12.4.2 测试CustomerDA import java.util.Vector; public class TesterOne { public static void main(String args[]) {// create two customers Customer firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); ; Customer secondCustomer = new Customer(Mike, Boston, 467-1234); Customer.initialize(); try // test addNew {firstCustomer.addNew(); secondCustomer.addNew(); System.out.println(added two customers); } catch (DuplicateException e) {System.out.println(e);} 12.4.2 测试CustomerDA try // test find { firstCustomer = Customer.find(123-4567); System.out.println(find + firstCustomer.tellAboutSelf()); } catch (NotFoundException e) {System.out.println(e);} // test getAll Vector allCustomer

文档评论(0)

xiaofei2001129 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档