6 基于Servlet 的分页

  实现上图的分页功能,每页显示3条数据,每页显示3页供用户访问,点击左右双箭头时,可以跳转至上一个或者下一个大页,如点击右双箭头显示如下:

  6 基于Servlet 的分页

 

  1 连接数据库的工具类

  1 package com.latiny.db;
  2 
  3 import java.io.*;
  4 import java.sql.*;
  5 import java.util.ArrayList;
  6 import java.util.Properties;
  7 
  8 /*
  9  *操作数据库的工具类
 10  */
 11 
 12 public class DBUtil {
 13 
 14     //定义需要的变量
 15     private static String driver =null;
 16     private static String url =null;
 17     private static String user=null;
 18     private static String password=null;
 19     
 20     private static Connection conn;
 21     //使用PreparedStatment可以防止sql注入
 22     private static PreparedStatement ps;
 23     private static ResultSet rs;
 24     private static CallableStatement cs;
 25     
 26     //读配置文件
 27     private static Properties pp=null;
 28     private static InputStream fis=null;
 29     
 30     //加载驱动,只需要执行一次
 31     static
 32     {
 33         try
 34         {
 35             pp = new Properties();
 36             
 37             //当我们使用java web的时候,读取文件要使用类加载器
 38             fis = DBUtil.class.getClassLoader().getResourceAsStream("dbinfo.properties");
 39             
 40             pp.load(fis);
 41             driver = pp.getProperty("DRIVER");
 42             url = pp.getProperty("URL");
 43             user = pp.getProperty("USER");
 44             password = pp.getProperty("PASSWORD");
 45             
 46             // 1 加载驱动
 47             Class.forName(driver);
 48             
 49         }
 50         catch(Exception e)
 51         {
 52             e.printStackTrace();
 53         }
 54         finally
 55         {
 56             try 
 57             {
 58                 fis.close();
 59             } catch (IOException e) {
 60                 // TODO Auto-generated catch block
 61                 e.printStackTrace();
 62             }
 63             fis = null;
 64         }
 65     }
 66     
 67     /*
 68      * 获取Connection连接
 69      */
 70     public static Connection getConn()
 71     {
 72         try 
 73         {
 74             // 2 获取数据库连接
 75             conn = DriverManager.getConnection(url, user, password);
 76         } 
 77         catch (SQLException e) 
 78         {
 79             // TODO Auto-generated catch block
 80             e.printStackTrace();
 81         }
 82         
 83         return conn;
 84     }
 85     
 86     /*
 87      * 直接返回rs结果,此方法不能关闭rs,因为后面调用它的类还会用到,如果关闭则不能正常使用
 88      */
 89     public static ResultSet queryResult(String sql, String[] parameters)
 90     {
 91         try 
 92         {
 93             conn = getConn();
 94             // 3 创建Statement对象
 95             ps = conn.prepareStatement(sql);
 96             // 4 给问号赋值,即给sql语句的条件参数赋值如果需要的话
 97             if(parameters!=null)
 98             {
 99                 for(int i=1; i<=parameters.length; i++)
100                 {
101                     ps.setString(i, parameters[i-1]);
102                 }
103             }
104             
105             // 5 执行sql获取返回结果
106             rs = ps.executeQuery();
107         } 
108         catch (SQLException e) 
109         {
110             // TODO Auto-generated catch block
111             e.printStackTrace();
112         }
113 
114         return rs;    
115     }
116     
117     /*
118      * 将rs结果封装成ArrayList,然后可以关闭rs,节省数据库访问资源
119      */
120     public static ArrayList queryResult2(String sql, String[] parameters)
121     {
122         ArrayList al = new ArrayList();
123         
124         try 
125         {
126             //2 获取数据库连接
127             conn = getConn();
128             //3 创建Statement对象
129             ps = conn.prepareStatement(sql);
130             
131             //4  给问号赋值,即给sql语句的条件参数赋值如果需要的话
132             if(parameters!=null)
133             {
134                 for(int i=1; i<=parameters.length; i++)
135                 {
136                     ps.setString(i, parameters[i-1]);
137                 }
138             }
139             
140             //5 执行sql语句获取返回结果
141             rs = ps.executeQuery();
142             
143             //获取rs的结构
144             ResultSetMetaData rsmd = rs.getMetaData();
145             //获取查询语句的列数
146             int column = rsmd.getColumnCount();
147             
148             while(rs.next())
149             {
150                 //对象数组,存储一行数据
151                 Object[] objs = new Object[column];
152                 for(int i=0; i<objs.length; i++)
153                 {
154                     objs[i] = rs.getObject(i+1);
155                 }
156                 al.add(objs);
157             }
158             
159         } 
160         catch (SQLException e) 
161         {
162             // TODO Auto-generated catch block
163             e.printStackTrace();
164         }
165         finally
166         {
167             //关闭资源
168             close(rs, ps, conn);
169         }
170 
171         return al;    
172     }
173 
174     //调用存储过程,带输入输出参数的
175     public static CallableStatement callProcedure(String sql, String[] inputPara, Integer[] outputPara)
176     {
177         
178         try 
179         {
180             conn = getConn();
181             cs = conn.prepareCall(sql);
182             for(int i=0; inputPara!=null && i<inputPara.length; i++)
183             {
184                 cs.setObject(i+1, inputPara[i]);
185             }
186             
187             //给output参数赋值
188             for(int j=0; outputPara!=null && j<outputPara.length; j++)
189             {
190                 cs.registerOutParameter(inputPara.length+1+j, outputPara[j]);
191             }
192             
193             cs.execute();
194             
195         } catch (SQLException e) {
196             // TODO Auto-generated catch block
197             e.printStackTrace();
198         }
199         finally
200         {
201             close(rs, ps, conn);
202         }
203         
204         return cs;
205         
206     }
207     
208     //update, insert, delete
209     public static Integer updateData(String sql, String[] parameters)
210     {
211         int result = 0;
212         try
213         {
214             conn = getConn();
215             ps = conn.prepareStatement(sql);
216             if(parameters!=null)
217             {
218                 for(int i=0; i<parameters.length; i++)
219                 {
220                     ps.setObject(i+1, parameters[i]);
221                 }
222             }
223             
224             //执行executeUpdate并且返回受影响的行数
225             result = ps.executeUpdate();
226             
227         }
228         catch(Exception e)
229         {
230             e.printStackTrace();
231         }
232         finally
233         {
234             close(rs, ps, conn);
235         }
236         
237         return result;
238     }
239     
240     //关闭对应的数据库连接资源
241     public static void close(ResultSet rs1, PreparedStatement ps1, Connection conn1)
242     {
243 
244         try 
245         {
246             if(rs1!=null)
247             {
248                 rs1.close();
249             }
250             if(ps1!=null)
251             {
252                 ps1.close();
253             }
254             if(conn1!=null)
255             {
256                 conn1.close();
257             }
258             
259         } catch (SQLException e) {
260             e.printStackTrace();
261         }
262         
263     }
264 }
View Code

相关文章:

  • 2021-11-29
  • 2021-11-17
  • 2021-11-23
  • 2021-11-27
  • 2021-08-02
  • 2022-01-14
  • 2022-03-15
猜你喜欢
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-07-16
相关资源
相似解决方案