JSP第十次课:JSP项目开发高级操作1
一、项目开发通用化
1、将className,url,user、password常用又不变值,定义为常量,编写到一个类(Constants)中
package mybean.util;
public class Constants {
public static String DriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String url="jdbc:sqlserver://localhost:1433; Database=ebuy";
public static String uid="sa";
public static String pwd="123456";
//数据库名,用户名,密码,url有所不同
}
2、创建JDBCTool类,编写连接数据库、关闭连接,实现复用性
package mybean.util;
import java.sql.*;
public class JDBCTool {
public static Connection getConnection()
{
Connection conn=null;
try {
Class.forName(Constants.DriverName);
conn=DriverManager.getConnection(Constants.url,Constants.uid,Constants.pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
拓展:通过编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
解决方案:将相关值放入配置文件中,通过修改配置方法实现和具体的数据解耦
(1)在src文件夹下新建文件(file),jdbc.properties
className=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://10.40.43.202:1433;Database=ebuy
user=shopping
password=shopping
(2)mybean.util包下新建JDBCTool类
package mybean.util;
import java.util.*;
import mybean.*;
import java.sql.*;
import java.io.*;;
public class JDBCTool {
/*
* 编写 一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
*/
public Connection getConnection() throws Exception{
Connection conn=null;
String className=null;
String url=null;
String user=null;
String password=null;
//读取类路径下的jdbc.properties文件输入流
InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
//创建properties对象
Properties properties=new Properties();
//加载in对象输入流
properties.load(in);
/加载字符串
className=properties.getProperty("className");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
Properties info=new Properties();
info.put("user", user);
info.put("password",password);
//方法 1
// Driver driver=(Driver)Class.forName(className).newInstance();
// conn=driver.connect(url, info);
//方法2
Class.forName(className);//加载数据驱动
conn=DriverManager.getConnection(url,user,password);
return conn;
}
}
二、商品管理分页
分页需要知道的有:
(1)共有几页countPage,当前页currentPage,共有多少条记录countNumber,每页显示几条记录pageNum
(2)需要获得当前页数据库中数据GoodsDAO.list(PageInfo)
1、设计分页Bean--PageInfo.java
package mybean.util;
public class PageInfo {
private int currentPage =1;//当前页 //2
private int pageNum = 4;//每页数据数 //1
private int countNumber = 0; //共记录数 3
private int countPage=1;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(String currentPage) {
int page=1;
if(null != currentPage && currentPage.trim().length()>0)
{
page=Integer.parseInt(currentPage);
}
else{
page=1;
}
this.currentPage = page;
}
public int getCountNumber() {
return countNumber;
}
public void setCountNumber(int countNumber) {
this.countNumber = countNumber;
}
public void setCountPage()
{
this.countPage = (int)java.lang.Math.ceil(this.getCountNumber()/(this.getPageNum()+0.0));
}
public int getCountPage()
{
return countPage;
}
}
2、数据库读取相应页的数据GoodsDAO.list(复制原list代码,适当更改)
public LinkedList<Goods> list(PageInfo pageInfo)
{
LinkedList<Goods> ls=new LinkedList<Goods>();
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String url="jdbc:sqlserver://localhost:1433;Database=ebuy";
String user="shopping";
String password="shopping";
String sql="select top (1*?) * from goods where gid not in (select top (1*?) gid from goods)";
String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
Class.forName(className);
conn=DriverManager.getConnection(url, user, password);
//首先知道数据库中有多少条记录
String sql1="select count(*) from goods";
stmt=conn.prepareStatement(sql1);
rs=stmt.executeQuery();
if(rs.next()){
pageInfo.setCountNumber(rs.getInt(1));
}
rs.close();
stmt.close();
//获取制定条数据记录
stmt=conn.prepareStatement(sql);
stmt.setInt(1,pageInfo.getPageNum());
stmt.setInt(2, pageInfo.getPageNum()*(pageInfo.getCurrentPage()-1));
rs=stmt.executeQuery();
while(rs.next()){
Goods g=new Goods();
g.setGid(rs.getInt("gid"));
g.setName(rs.getString("name"));
g.setPrice(rs.getFloat("price"));
g.setNum(rs.getInt("num"));
ls.add(g);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(stmt!=null)
{stmt.close();}
if(conn!=null)
{conn.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ls;
}
3、good_view页添加部分代码实现,显示指定页数据
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="mybean.*,mybean.util.*" %>
<%
// 显示指定页的数据
String currentPage = request.getParameter("currentPage");
PageInfo pageInfo=(PageInfo)request.getAttribute("pageInfo");
if(pageInfo==null)
{
pageInfo=new PageInfo();
request.setAttribute("pageInfo",pageInfo);
}
pageInfo.setCurrentPage(currentPage); //设置当前页
%>
<div style="width:500px;">
<p>商品信息显示</p>
<div style="padding-left:350px;"><a href="good_add.jsp">添加</a></div>
<table width="400" border="1">
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品数量</td>
<td>删除</td>
<td>修改</td>
<td>详细</td>
</tr>
<%
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods>gs=dao.list(pageInfo);
for(Goods g:gs)
{
//out.println(g.getName());
%>
<tr>
<td><%=g.getGid()%></td>
<td><%=g.getName()%></td>
<td><%=g.getPrice()%></td>
<td><%=g.getNum()%></td>
<td><a href="good_delete.jsp?gid=<%=g.getGid()%>">删除</a></td>
<td><a href="good_update.jsp?gid=<%=g.getGid()%>">修改</a></td>
<td>详细</td>
</tr>
<%}
pageInfo.setCountPage(); //设置总页数
%>
</table>
<jsp:include page="page.jsp"/>
</div>
4、page.jsp实现上一页下一页组件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="mybean.*"%>
<%@page import="mybean.util.*"%>
<script type="text/javascript">
function pageChange(currentPage){
document.getElementById(‘currentPage‘).value = currentPage;
document.form1.submit();
}
</script>
<%
PageInfo pageInfo=(PageInfo)request.getAttribute("pageInfo");
if(pageInfo!=null)
{
int countPage=pageInfo.getCountPage();
int upPage=1;
int downPage=countPage;
if(pageInfo.getCurrentPage()!=1)
{
upPage=pageInfo.getCurrentPage()-1;
}
if(pageInfo.getCurrentPage()!=countPage){
downPage=pageInfo.getCurrentPage()+1;
}
%>
<ul style="list-style:none">
<li style="border:1px solid #ccc;width:50px;float:left;margin:5px;" href="#"></a>首页</li>
<li style="border:1px solid #ccc;width:50px;float:left;margin:5px;" onclick="pageChange(<%=upPage%>);">上一页</li>
<li style="border:1px solid #ccc;width:50px;float:left;margin:5px;" onclick="pageChange(<%=downPage%>);">下一页</li>
<li style="border:1px solid #ccc;width:50px;float:left;margin:5px;" onclick="pageChange(<%=pageInfo.getCountPage()%>);">尾页</li>
</ul>
<%} %>
<form id="form1" name="form1" action="good_view.jsp" method="post" target="_self">
<input type="hidden" name="currentPage" id="currentPage" value="<%=pageInfo.getCurrentPage()%>">
</form>
本文出自 “学而不思则罔” 博客,请务必保留此出处http://dyzyxy.blog.51cto.com/944775/1625820
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。