
一、教学内容
1.JDBC基础
2.连接DB的常用方式
3.MYSQL
4.记录的查询
5.记录的添加
6.记录的更新与删除
二、教学目标
1.理解JSP内置对象;
2.熟练掌握request对象、response对象、session对象、application对象等JSP常用内置对象。
三、教学重点及难点
1.重点:request、response、session、application的用法;
2.难点:session、application的用法。
四、教学方式与方法
演示讲解法,任务驱动法,案例教学法,问题探究法,与多媒体教学演示相结合
五、实施教学
第一部分:新授课
一、JDBC基础
1.什么是JDBC ? Java DataBase Connectivity,Java程序连接和存取数据库的应用程序接口(API)。
由一群类和接口组成,是Java核心API的一部分。
可以使用标准的SQL 语句。
作用:通过连接器与数据库建立连接,调用JDBC API发送SQL语句,处理数据库返回结果。
使用JDBC可以使程序开发人员建立一个与数据库和平台无关的编程接口来建立数据库应用程序。
JDBC是Java实现跨平台数据库访问的基础,支持数据库的两层、三层访问模型,所以它既实用又高效。
2.JDBC结构
3.JDBC驱动程序的四种类型:
(1)基于本地API,部分用Java来编写的驱动程序
此类驱动程序是把客户机API上的JDBC调用转换为DBMS(Database Management System,数据库管理系统)的调用,如Oracle、DB2等。它像JDBC-ODBC桥驱动程序一样,也要求将某些二进制代码加载到每台客户机上。
(2)本地协议纯Java驱动程序
这种类型的驱动程序将JDBC调用直接转换为DBMS所使用的网络协议。这将允许从客户机上直接调用DBMS服务器,是Intranet访问的一个很实用的解决方法。
(3)JDBC-ODBC桥加载ODBC驱动程序
(4)JDBC网络纯Java驱动程序
二、连接DB的常用方式
1.JDBC-ODBC桥加载ODBC驱动程序: JDBC-ODBC桥利用ODBC驱动程序提供JDBC访问。
注意:必须将ODBC二进制代码(在许多情况下还包括数据库客户机的代码)加载到使用该驱动程序的每台客户机上,因此它适用于企业内部网或者是用Java编写的三层结构的应用程序服务器代码。
使用JDBC-ODBC桥接器访问数据库的3个步骤:
(1)建立JDBC-ODBC桥接器;
String dirverName=“sun.jdbc.odbc.JdbcOdbcDriver”;
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
(2)创建ODBC数据源;
(3)和ODBC数据源建立连接;
String conURL=“jdbc:odbc:<数据源标记>”
try { Connection conn=DriverManager.getConnection(conURL,userName,userPass); }catch (SQLException e ){}
2. JDBC网络纯Java驱动程序
此驱动程序将JDBC转换为与DBMS无关的网络协议,之后此网络协议又被某个服务器转换为一种DBMS协议。
使用纯JAVA数据库驱动方式访问数据库的2个步骤:
(1)加载纯Java数据库驱动程序;
String dirverName=“com.mysql.jdbc.Driver”;
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
(2)和指定的数据库建立连接;
String conURL=“jdbc:mysql://127.0.0.1:3306/DatabaseName”
try { Connection conn=DriverManager.getConnection(conURL,userName,userPass); }catch (SQLException e ){}
三、MYSQL
1.安装MySQL5.1
2.为MySQL安装前端工具Navicat 8
3.安装JDBC驱动:把mysql-connector-java-5.1.7-bin. 拷贝到\\WEB-INF\\lib下
四、记录的查询
与数据库的连接一旦建立,就可用来向它所涉及的数据库传送SQL语句。Statement对象用于将SQL语句发送到数据库中。
JDBC提供了三种Statement对象:Statement、PreparedStatement、CallableStatement
1.Statement
(1)作用:Statement对象用于发送简单的SQL语句。
(2)创建:Statement stmt = conn.createStatement();
(3)执行数据库操作:
使用Statement对象的executeQuery()方法执行查询操作;
使用Statement对象的executeUpdate()方法执行插入、修改和删除操作及数据定义语言语句,将SQL语句作为提供给Statement对象的方法。
eg:ResultSet rec = stmt.ExecuteQuery("SELECT * FROM student");
2. PreparedStatement
(1)作用:该对象包含已编译过的SQL语句,用于发送带有一个或多个输入参数的SQL语句。
(2)优点:由于PreparedStatement对象已预编译过,所以其执行效率比Statement对象的执行效率更高。
(3) 创建:String sql = “select * from stu where age >?” );PreparedStatement ps = conn.prepareStatement ( sql );
该语句为每个输入参数保留一个问号“?”作为占位符。每个问号的值必须在该语句执行之前被设置。PreparedStatement拥有一组方法,用于设置输入参数的值。执行语句时,这些输入参数将被送到数据库中。
(4)执行数据库操作具体过程:
1创建 PreparedStatement 对象
2传递输入参数 ,注意输入参数中数据类型的一致性
3执行查询、插入、更新或删除操作
3.ResultSet
(1)作用:ResultSet (结果集)对象包含符合 SQL 语句中条件的所有行集合,它通过一套 get 方法(这些 get 方法可以访问当前行中的不同列)提供了对这些行中数据的访问。结果集一般是一个表,其中有查询所返回的列标题及相应的值。
(2)结果集类型:
TYPE_FORWORD_ONLY: 结果集为只读类型,并且读取顺序从头到尾读取记录,只能读一次。
TYPE_SCROLL_SENSITIVE: 在他人同时对数据库操作时,会影响结果集的纪录。
TYPE_SCROLL_INSENSITIVE: 在他人同时对数据库操作时,不会影响结果集的纪录。
Statement stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
(3)创建:ResultSet rs = stmt.executeQuery("select * from stu");
(4)主要方法:
∙absolute(int):绝对定位到指定记录。 first():当前记录指针移动到第一行。 beforeFirst()
∙last():当前记录指针移动到最后移行。 afterLast()
∙next():把当前记录指针下移一行。当前行有效返回true,否则返回false。
∙previous():把当前记录指针上移一行。当前行有效返回true,否则返回false。 close()
∙getBoolean(),getByte(),getString(),getInt(),getShort(),getLong(), getDouble(),getFloat(),getDate()
∙getRow():返回当前记录指针所在的行号。行号从1开始,若无记录则返回0。
∙isFirst(),isLast(),isBeforeFirst(),isAfterLast()
五、记录的添加
要求:创建一个简易的商品管理后台系统,其中商品信息存放在数据库表中。商品信息至少包含编码、名称、生产地、生产时间、价格等信息。
设计流程:设计原型、建立DB、编码、测试
1.建立DB及相关表:建立DB:Warehouse,建立表:product
2.设计页面:login.jsp,confirm.jsp,left.jsp,manage.jsp,selectProduct.jsp,addProduct.jsp,updateProduct.jsp,deleteProduct.jsp
3.查询操作:顺序查询、随机查询、条件查询、模糊查询、排序查询
4.添加操作
5.更新操作
6.删除操作
7.在Tag文件中执行数据库操作
六、网上投票系统的实现
第二部分:概括和总结
重点:JDBC的基本概念,连接DB的两种常用方式, 记录的查询、添加、更新与删除
难点:JDBC的基本概念,连接DB的两种常用方式, 记录的查询与添加
第三部分:练习 举例:针对重点和难点提问
第四部分:布置作业
第五部分:熟悉教材
代码:
//login.jsp
<%@ page contentType="text/html; charset=gb2312" %>
欢迎登录商品管理后台系统://confirm.jsp
<%@ page contentType="text/html; charset=gb2312"import ="java.sql.*" %>
<%String username = request.getParameter("username"); String password = request.getParameter("password");
boolean success = false;
if (username!=null && password!=null ){ //连接数据库执行查询操作
String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse";
try { Connection conn=DriverManager.getConnection(conURL,"root
Statement stmt = conn.createStatement();
String sql = "select * from user where username='"+username+"'and password='"+password+"'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()){ success = true; } }catch (SQLException e ){e.printStackTrace();} }
if (success){ %>
<%}else {response.sendRedirect("login.jsp");}%>
//left.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import ="java.sql.*" %>
//manage.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import ="java.sql.*" %>
<%String username = request.getParameter("username"); out.println("欢迎"+username+"登录系统"); %>
// pxselectProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
根据条件查询产品信息:
<% String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse"; Connection conn=null;
try { conn=DriverManager.getConnection(conURL,"root
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from product "; String condition =null;
String pprice = request.getParameter("pprice"); String pdate = request.getParameter("pdate");
if (pprice!=null && !pprice.equals("")) condition =" order by pPrice "+pprice;
if (pdate!=null && !pdate.equals("")){
if( condition==null) condition =" order by pDate "+pdate; else condition = condition + " , pDate "+pdate; }
if (condition!=null) sql = sql +" " + condition ; ResultSet rs = stmt.executeQuery(sql); %>
| 产品编码 | 产品名称 | 生产地址 | 生产时间 | 出厂价格 |
| <%=rs.getInt("pid")%> | <%=rs.getString("pname")%> | <%=rs.getString("paddress")%> | <%=rs.getString("pdate")%> | <%=rs.getDouble("pprice")%> |
// ptselectProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
根据条件查询产品信息:
<% String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse"; Connection conn=null;
try { conn=DriverManager.getConnection(conURL,"root
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from product "; String condition =null;
String pprice = request.getParameter("pprice"); String pdate = request.getParameter("pdate");
if (pprice!=null && !pprice.equals("")) condition =" order by pPrice "+pprice;
if (pdate!=null && !pdate.equals("")){
if( condition==null) condition =" order by pDate "+pdate; else condition = condition + " , pDate "+pdate; }
if (condition!=null) sql = sql +" " + condition ;
ResultSet rs = stmt.executeQuery(sql); %>
| 产品编码 | 产品名称 | 生产地址 | 生产时间 | 出厂价格 |
| <%=rs.getInt("pid")%> | <%=rs.getString("pname")%> | <%=rs.getString("paddress")%> | <%=rs.getString("pdate")%> | <%=rs.getDouble("pprice")%> |
//mhselectProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
根据条件查询产品信息:
<% String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse"; Connection conn=null;
try { conn=DriverManager.getConnection(conURL,"root
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from product "; String condition =null;
String pName = request.getParameter("productName"); String pAddress = request.getParameter("productAddress");
String lowPrice = request.getParameter("lowPrice"); String highPrice = request.getParameter("highPrice");
if (pName!=null && !pName.equals("")) { byte[] b = pName.getBytes("ISO-8859-1"); pName=new String(b);
condition =" pName like '%"+pName+"%' "; }
if (pAddress!=null && !pAddress.equals("")){ byte[] b = pAddress.getBytes("ISO-8859-1"); pAddress=new String(b);
if( condition==null) condition =" pAddress like '%"+pAddress+"%' ";
else condition = condition + " and pAddress like '%"+pAddress+"%' "; }
if (condition!=null) sql = sql +" where " + condition ;
ResultSet rs = stmt.executeQuery(sql); %>
| 产品编码 | 产品名称 | 生产地址 | 生产时间 | 出场价格 |
| <%=rs.getInt("pid")%> | <%=rs.getString("pname")%> | <%=rs.getString("paddress")%> | <%=rs.getString("pdate")%> | <%=rs.getDouble("pprice")%> |
// sjselectProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
随机查询产品信息:
<% String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName); }catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse"; Connection conn=null;
try { conn=DriverManager.getConnection(conURL,"root
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from product "; ResultSet rs = stmt.executeQuery(sql); %>
| 产品编码 | 产品名称 | 生产地址 | 生产时间 | 出场价格 |
| <%=rs.getInt("pid")%> | <%=rs.getString("pname")%> | <%=rs.getString("paddress")%> | <%=rs.getString("pdate")%> | <%=rs.getDouble("pprice")%> |
// tjselectProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
根据条件查询产品信息:
<%
String driverName="com.mysql.jdbc.Driver";
try { Class.forName(driverName);
}catch (ClassNotFoundException e ){}
String conURL="jdbc:mysql://localhost:3306/warehouse";
Connection conn=null;
try { conn=DriverManager.getConnection(conURL,"root
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from product ";
String condition =null;
String pName = request.getParameter("productName");
String pAddress = request.getParameter("productAddress");
String lowPrice = request.getParameter("lowPrice");
String highPrice = request.getParameter("highPrice");
if (pName!=null && !pName.equals("")) {
byte[] b = pName.getBytes("ISO-8859-1"); pName=new String(b);
condition =" pName='"+pName+"' "; }
if (pAddress!=null && !pAddress.equals("")){
byte[] b = pAddress.getBytes("ISO-8859-1"); pAddress=new String(b);
if( condition==null) condition =" pAddress='"+pAddress+"' ";
else condition = condition + " and pAddress='"+pAddress+"' ";
}
if (lowPrice!=null && !lowPrice.equals("") )
if (condition==null) condition =" pPrice>='"+lowPrice+"' ";
else condition = condition + " and pPrice>='"+lowPrice+"' ";
if (highPrice!=null && !highPrice.equals(""))
if (condition==null) condition =" pPrice<='"+highPrice+"' ";
else condition = condition + " and pPrice<='"+highPrice+"' ";
if (condition!=null)
sql = sql +" where " + condition ;
System.out.println("sql"+sql);
ResultSet rs = stmt.executeQuery(sql);
%>
| 产品编码 | 产品名称 | 生产地址 | 生产时间 | 出场价格 |
| <%=rs.getInt("pid")%> | <%=rs.getString("pname")%> | <%=rs.getString("paddress")%> | <%=rs.getString("pdate")%> | <%=rs.getDouble("pprice")%> |
// addProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
add
// updateProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
add
// deleteProduct.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
delete
