📄 opcollect.java
字号:
package hall;
/**
* Project: NWPU online shop
* JDK version used: jdk1.5.0
* Version: 1.01
* class opCollect 用来处理有关于收藏的各种操作
*/
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
public class opCollect {
private Vector collectList;//存储收藏的数组
private int page = 1; //显示的页码
private int pageSize = 10; //每页显示的留言数
private int pageCount = 0; //页面总数
private long recordCount = 0; //查询的记录总数
private DBWrapper myConnection = null;
private String sqlStr = "";
public opCollect() throws Exception {
myConnection = DBWrapper.Instance();
}
/**
* boolean addCollect(String inUser, int inItem)
* Description :往收藏夹中添加收藏
* @param String 用户名
* @param int 货物号
* @return boolean 返回操作是否成功信息
*/
public boolean addCollect(String inUser, int inItem) {
if (inItem == -2)
return true;
//根据当地时间产生收藏日期
Date tempDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String sdate = sdf.format(tempDate);
try {
String sql1 = "select * from collect where userName = '" + inUser
+ "' and producItem = " + inItem;
ResultSet rs1 = myConnection.runQuery(sql1);
if (rs1.next()) {
String sql2 = "update collect set collectDate = '" + sdate
+ "'";
myConnection.runUpdate(sql2);
}
else {
String sql3 = "INSERT INTO [collect]([userName],[producItem],[collectDate])values('"
+ inUser + "'," + inItem + ",'" + sdate + "')";
myConnection.runUpdate(sql3);
}
return true;
} catch (Exception e) {
System.out.println(e);
return false;
}
}
/**
* boolean showCollect(String inName)
* Description :显示收藏夹收藏信息
* @param String 用户名
*
* @return boolean 返回操作是否成功信息
*/
public boolean showCollect(String inName) throws Exception {
sqlStr = "select count(*) from collect where userName = '" + inName
+ "'";
try {
ResultSet rs1 = myConnection.runQuery(sqlStr);
if (rs1.next()) {
recordCount = rs1.getInt(1);
}
rs1.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
if (recordCount < 1)
pageCount = 0;
else
pageCount = (int) (recordCount - 1) / pageSize + 1;
if (page < 1)
page = 1;
else if (page > pageCount)
page = pageCount;
sqlStr = "select * from collect where userName = '" + inName
+ "' order by collectDate";
try {
ResultSet rs = myConnection.runQuery(sqlStr);
collectList = new Vector();
if (page == 1) {
} else {
for (int i = 0; i < pageSize * (page - 1); i++) {
rs.next();
}
}
for (int i = 0; i < pageSize; i++) {
if (rs.next()) {
collect tempcoll = new collect();
tempcoll.setUsername(rs.getString("userName"));
tempcoll.setItem(rs.getInt("producItem"));
tempcoll.setCollDate(rs.getString("collectDate"));
collectList.addElement(tempcoll);
} else {
break;
}
}
rs.close();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* boolean deleteCollect(String inName, int inItem)
* Description :删除收藏夹中一条收藏信息
* @param String 用户名
* @param int 货物号
* @return boolean 返回操作是否成功信息
*/
public boolean deleteCollect(String inName, int inItem) throws Exception {
sqlStr = "delete from collect where userName = '" + inName
+ "' and producItem = " + inItem;
try {
myConnection.runUpdate(sqlStr);
return true;
} catch (SQLException e) {
System.out.println(e);
return false;
}
}
/**
* int getPage()
* Description :得到要显示的页数
* @return int
*/
public int getPage() {
return page;
}
/**
* void setPage(int newpage)
* Description :修改要显示的页数
* @param int
*/
public void setPage(int newpage) {
page = newpage;
}
/**
* int getPageSize()
* Description :得到每页要显示的收藏数
* @return int
*/
public int getPageSize() {
return pageSize;
}
/**
* void setPageSize(int newpsize)
* Description :修改每页要显示的收藏数
* @param int
*/
public void setPageSize(int newpsize) {
pageSize = newpsize;
}
/**
* int getPageCount()
* Description :得到页面总数
* @return int
*/
public int getPageCount() {
return pageCount;
}
/**
* void setPageCount(int newpcount)
* Description :修改页面总数
* @param int
*/
public void setPageCount(int newpcount) {
pageCount = newpcount;
}
/**
* int getRecordCount()
* Description :得到记录总数
* @return long
*/
public long getRecordCount() {
return recordCount;
}
/**
* void setRecordCount(long newrcount)
* Description :修改记录总数
* @param long
*/
public void setRecordCount(long newrcount) {
recordCount = newrcount;
}
/**
* Vector getCollectList()
* Description :得到存储收藏的Vector
* @return Vector
*/
public Vector getCollectList() {
return collectList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -