result=stmt.executeUpdate(“update stu set name=‘李四’ where no=100”); //删除
result=stmt.executeUpdate(“delete stu where no=100”); }catch(SQLException e){ e.printStackTrace(); }finally{
con.close(); }
? 当使用Statement的executeQuery方法向数据库发送一条Select查询语句
之后,返回一个ResultSet的对象,这个对象是查询结果集对象;
? ResultSet 其实就是一个二维表(行、列),并且具有列标题和对应的值。对
象具有指向其当前数据行的指针。通过ResultSet对象不但可以结果集数据,还可以获取结果集表的列名、数据类型等信息; ? ResultSet res=stmt.executeQuery(sql);
ResultSet类的主要方法: ? 行指针定位方法
? public boolean next();该方法的作用是将数据库游标向前移动一
位,使得下一行成为当前行,当刚刚打开记录集对象时,数据库游标的位置在记录集的最前面,第一次使用next()方法,将会使数据库游标定位到记录集的第一行,第二次使用next()方法,将会使数据库游标定位到记录集的第二行,以此类推。
? public boolean absolute(int row);该方法的作用是将记录集中
的某一行设定为当前行,亦即将数据库游标移动到指定的行,参数row指定了目标行的行号,这是绝对的行号,由记录集的第一行开始计算,不是相对的行号。
? public boolean previous();该方法的作用是将数据库游标向后移
动一位,使得上一行成为当前行。 ? public boolean first();该方法的作用是将当前行定位到数据库记
录集的第一行。
? public boolean last();该方法的作用刚好和first()方法相反,是将
当前行定位到数据库记录集的最后一行。
? public boolean isFirst();该方法的作用是检查当前行是否记录集
的第一行,如果是,返回true,否则,返回false。
? public boolean isLast();该方法的作用是检查当前行是否记录集
的最后一行,如果是,返回true,否则,返回false。
//连接数据库
Connection con=null; Statement stmt=null;ResultSet res=null;
String strURL= \
try{//数据库操作的异常处理
con=DriverManager.getConnection(strURL,”sa”,”sa”); stmt=con.createStatement();
String sql=“select id,userName,age from stu”;
int id=0,age=0; String usename=“”;
//查询Stu表中数据
res=stmt.executeQuery(sql); while(res.next()){ id=res.getInt(1);
username=res.getString(“username”); age=res.getInt(“age”); }
}catch(SQLException e){ e.printStackTrace(); }finally{ res.close(); stmt.close(); con.close(); 实例:
<%@ page import=\
测试JSP中表格显示Stu表中数据
<%//连接数据库
Connection con=null; Statement stmt=null; ResultSet res=null;
String strURL= \
try{//数据库操作的异常处理
con=DriverManager.getConnection(strURL,”sa”,”sa”);
stmt=con.createStatement();
String sql=“select id,userName,age from stu”; String username=“”;
res=stmt.executeQuery(sql);
%>
学号 学生姓名 年龄
<%
while(res.next()){
out.println(“”+res.getInt(1)+””): %>
<%= username %>
<%= res.getInt(“age”);%>岁 <% }
}catch(SQLException e){ e.printStackTrace(); }finally{
res.close(); stmt.close(); con.close(); }%>
}
第五章JSP/Servlet深入编程
5.1 Servlet过滤器
? ? ? ?
什么是Servlet过滤器(过滤器作用和登录验证的流程) 一种特定类型的Servlet,能够拦截请求和响应,可以过滤到客户机和服务器之间交换的数据
过滤器介于浏览器和需要过滤的JSP、Servlet之间
过滤器能够接受请求响应,然后可以检查请求对象的内容,并根据情况将请求转发给下一个组件,或者中止这次请求并返回给浏览器
通过过滤器,对请求提交的数据统一进行编码转换,可以一次性编码解决汉字问题
5.1.2 Servlet过滤器用法
? 建立一个实现Filter接口的类,这个类需要实现3个方法:doFilter,init,
destroy
? 在doFilter方法中进行过滤操作,这个方法中包含ServletRequest和
ServletResponse两个对象
? 调用FilterChain对象中的doFilter方法,进行请求的传递 ? 在web.xml配置文件中,对需要过滤的资源进行设置 ? 使用过滤器解决登录验证– LoginFilter登录过滤程序
public class LoginFilter implements Filter { private FilterConfig filterConfig = null;
public void doFilter(ServletRequest servletRequest,
ServletResponse servletresponse, FilterChain chain) throws IOException, ServletException { try {
HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletresponse;
HttpSession session = request.getSession(); request.setCharacterEncoding(\
String requestpath = request.getServletPath(); if (requestpath.equals(“/index.jsp”) ,//登录页面 chain.doFilter(request, response);
}else if (requestpath.equals(“/main.jsp”) ) ,//主页面
String loginUser = (String) session.getAttribute(“loginUser”);//获取令牌
if (loginUser==null){//如果没有登录,则进行用户名密码检验 String loginName=request.getParameter(“loginName”); String password=request.getParameter(“password”);
if (“admin”.equals(loginUser) && “123456”.equals(password)) , session.setAttribute(\ chain.doFilter(request, response); }else{
RequestDispatcher dispatcher = application.getRequestDispatcher(\ if (dispatcher != null){
dispatcher.forward(request, response); return;
}else{ throw new ServletException(\无法创建RequestDispatcher\
}
}else{//其他页面
String user = (String) session.getAttribute(\
if (user == null) {
RequestDispatcher dispatcher = application.getRequestDispatcher(\ if (dispatcher != null){
dispatcher.forward(request, response); return; }else{throw new ServletException(\无法创建RequestDispatcher\
}else chain.doFilter(request, response); } } }
public void init(FilterConfig filterConfig) throws ServletException { //设置上下文环境对象 this.filterConfig=filterConfig;
}
5.2 Servlet监听器
一种特定类型的Servlet,用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
监听器作用:
? 可以监听客户端的请求、服务端的操作等
? 通过监听器,可以自动激发一些操作,如监听在线用户数量,当增
加一个HttpSession时,给在线人数加1 ? 可以集中代码实现对于同一个事件的处理
5.6 JSP读取CheckBox的值
? 1、显示页面
? 2、读取CheckBox数据
<%
String jobs*+ =request.getParameterValues(“job\ for(int i=0;i
第六章 Java Web开发框架 ? 1. Model2(体系结构和模式原理)