1 / 14

网络编程

网络编程. 3. 数据库访问和 JDBC 首都师范大学信息工程学院 计算机科学与技术实验教学示范中心. 为什么要用数据库. 数据库是动态网页“动”起来的基础 数据库是企业应用的重要环节. 数据库的种类. 关系数据库、对象数据库、文件等 常见关系数据库 Oracle DB2 MS SQL Server. 访问数据库的方法. JDBC Java Database Connectivity Java 数据库互连 Java Database Connection Java 数据库连接 JDBC-ODBC Bridge 本地 API+ 部分 Java 驱动程序

jam
Télécharger la présentation

网络编程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 网络编程 3.数据库访问和JDBC 首都师范大学信息工程学院 计算机科学与技术实验教学示范中心

  2. 为什么要用数据库 • 数据库是动态网页“动”起来的基础 • 数据库是企业应用的重要环节

  3. 数据库的种类 • 关系数据库、对象数据库、文件等 • 常见关系数据库 • Oracle • DB2 • MS SQL Server

  4. 访问数据库的方法 • JDBC • Java Database Connectivity Java数据库互连 • Java Database Connection Java数据库连接 • JDBC-ODBC Bridge • 本地API+部分Java驱动程序 • 网络协议+完全Java驱动程序 • 本地协议+完全Java驱动程序 • 标准JDBC驱动程序*

  5. 使用JDBC驱动访问数据库 • 直接加载JDBC驱动 • Connection conn = null; • try { • Class.forName(driver); • Connection con = createConnection(); • } • catch (SQLException ex) { • System.err.println(ex.toString()); • } • ……

  6. 使用JDBC驱动访问数据库(续) • 使用数据源 • javax.naming.Context c = new javax.naming.InitialContext(); • javax.sql.DataSourcedataSource = (javax.sql.DataSource) c.lookup("java:comp/env/jdbc/yuyue"); • Connection conn = null; • PreparedStatement prpStmt = null; • ResultSet rs=null; • try{ • conn = getHospital().getConnection(); • prpStmt = conn.prepareStatement(“sql…..”); • rs = prpStmt.executeQuery(); • ……….. • rs.close(); • rs = null; • prpStmt=null; • prpStmt.close(); • conn=null; • conn.close(); • }

  7. 使用JDBC驱动访问数据库(续) • catch (Exception e){ • } • finally{ • if (rs != null) { • try { rs.close(); } catch (SQLException e) { ; } • rs = null; • } • if ( prpStmt != null) { • try { prpStmt.close(); } catch (SQLException e) { ; } • prpStmt = null; • } • if (conn != null) { • try { conn.close(); } catch (SQLException e) { ; } • conn = null; • } • }

  8. 使用JDBC驱动访问数据库(续) • 通过DataSource和JSTL访问数据库 • <%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> • <sql:query var=“rs” dataSource=“jdbc/test” > • select * from ….. • <sql:param values=“${param.username}” /> • </sql:query> • <sql:update dataSource=“jdbc/test” > • insert into users (username,password) values (?, ?) • <sql:param values=“${param.username}”/> • <sql:param values=“${param.password}”/> • </sql:update>

  9. 实例演示 • 演示设计实现一个简单的图书信息管理系统 • 要求可以添加图书信息、删除图书信息、修改图书信息和查询图书

  10. 步骤 • 构建数据库表 • 创建web项目 • 添加和配置类库 • 配置上下文数据源 • 创建JavaBean • 创建相关页面

  11. 构建数据库表 • 建立图书表 • bigint bookid • String bookisbn • String bookname • String bookpublisher • String bookauthor • String bookprice

  12. 创建web项目配置类库 • 在NetBeans里创建Web项目 • 添加JDBC驱动程序库和JSTL库

  13. 配置上下文 • context.xml • <Context path="/yuyue"> • <Resource auth="Container" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/yuyue" password=“chengren" type="javax.sql.DataSource" url="jdbc:microsoft:sqlserver://202.204.220.21:1433;DatabaseName=ChengRen2006" username=“chengren"/> • </Context> • web.xml • <resource-ref> • <description>jdbc:microsoft:sqlserver://202.204.220.21:1433;DatabaseName=ChengRen2006</description> • <res-ref-name>jdbc/yuyue</res-ref-name> • <res-type>javax.sql.DataSource</res-type> • <res-auth>Container</res-auth> • <res-sharing-scope>Shareable</res-sharing-scope> • </resource-ref>

  14. 创建javaBean和构造页面 • 创建图书Bean • 添加页面

More Related