📄 titlesbean.java
字号:
/*
* y2javaee.sg.ch03.TitlesBean.java
* Y2javaee的ch03的示例,用于演示购物车
*/
package com.apache.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import com.apache.pojo.BookBean;
public class TitlesBean {
private Connection connection;
private PreparedStatement titlesQuery;
private ResultSet results;
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCountPages() {
return countPages;
}
public void setCountPages(int countPages) {
this.countPages = countPages;
}
public int getCountSize() {
return countSize;
}
public void setCountSize(int countSize) {
this.countSize = countSize;
}
private int countPerPage = 9;// 每页的个数
private int pageSize ;// 页数
private int countPages;// 总页数
private int countSize;// 总个数
// 获得总个数
private int getCount() {
int count = 0;
connection = ConnectionManager.getConnction();
try {
titlesQuery = connection
.prepareStatement("select count(*) from titles");
results = titlesQuery.executeQuery();
while (results.next()) {
count = results.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titlesQuery);
ConnectionManager.closeConnection(connection);
}
return count;
}
// 获得每页的数据
public List<BookBean> getTitles() {
List<BookBean> list=new ArrayList<BookBean>();
// 设置条数
this.countSize = this.getCount();
// 设置页数
if (countSize % countPerPage == 0) {
countPages = countSize / countPerPage;
} else {
countPages = countSize / countPerPage + 1;
}
// 获得每页的数据
if(pageSize==0)
{
pageSize=1;
}
int c=(pageSize-1)*countPerPage;
connection = ConnectionManager.getConnction();
String sql = "select top "+countPerPage+" * from titles where isbn not in(select top "+c+" isbn from titles order by isbn) order by isbn";
System.out.println("size:"+this.pageSize);
try {
titlesQuery = connection.prepareStatement(sql);
results=titlesQuery.executeQuery();
while(results.next())
{
BookBean bb=new BookBean();
bb.setISBN(results.getString("isbn"));
String title=results.getString("title");
if(title.length()>20)
{
title=title.substring(0, 20)+"...";
}
bb.setTitle(title);
bb.setEditionNumber(results.getInt("editionNumber"));
bb.setCopyright(results.getString("copyright"));
PublisherDao pd=new PublisherDao();
bb.setPublisher(pd.getPublisherById(results.getInt("publisherID")));
bb.setImageFile(results.getString("imageFile"));
double price=results.getDouble("price");
DecimalFormat df=new DecimalFormat("0.00");
bb.setPrice(Double.parseDouble(df.format(price)));
list.add(bb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titlesQuery);
ConnectionManager.closeConnection(connection);
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -