?? customerrecorddao.java
字號:
package com.mole.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.mole.struts.bean.CustomerTradeRecordBean;
public class CustomerRecordDAO {
private Connection conn;
private int pageSize;
public CustomerRecordDAO() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Failed to initial context!");
DataSource ds = (DataSource) ctx
.lookup("java:comp/env/jdbc/crmdata");
conn = ds.getConnection();
conn.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
}
}
// 獲取消費記錄數量
public int getCount(String cid, String end) {
int count = 0;
ResultSet rs = null;
String sql = "SELECT COUNT(*) FROM [v_CustomerRecord] a WHERE a.[customerId]="
+ cid + end;
try {
rs = conn.prepareStatement(sql).executeQuery();
if (rs.next())
count = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
// 獲取是否公開消費記錄
public String getPublic(String cid) {
String result = "";
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT [ShowRecord] FROM [Customer] WHERE [ID]=" + cid;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next())
result = rs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
// 獲取消費過的店鋪信息
public ArrayList<Object[]> getStoreInfo(String ID) throws Exception {
ArrayList<Object[]> al = new ArrayList<Object[]>();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select distinct a.[storeId],a.[storename] from [v_CustomerRecord] a where a.[customerId]="
+ ID;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Object[] o = new Object[2];
o[0] = rs.getString(1);
o[1] = rs.getString(2);
al.add(o);
}
return al;
} finally {
if (ps != null)
ps.close();
}
}
// 獲取消費記錄
public ArrayList<Object[]> getRecord(String cid, String end,
int currentPage, int pageSize) throws Exception {
ArrayList<Object[]> al = new ArrayList<Object[]>();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT TOP "
+ pageSize
+ " a.[Id],a.[StoreName],a.[NominalPrice],a.[totalPrice],convert(char(20),a.[DealTime],120) FROM [v_CustomerRecord] a WHERE a.[CustomerID]="
+ cid + end + " AND a.[ID] NOT IN (" + "SELECT TOP "
+ (currentPage - 1) * pageSize
+ " [ID] FROM [v_CustomerRecord] WHERE a.[CustomerID]=" + cid
+ end + ")";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Object[] o = new Object[5];
o[0] = rs.getString(1);
o[1] = rs.getString(2);
o[2] = rs.getString(3);
o[3] = rs.getString(4);
o[4] = rs.getString(5);
al.add(o);
}
return al;
} finally {
if (ps != null)
ps.close();
}
}
// 添加消費評論
public void addComment(String customerId, String swiftId, String comment,
String point) {
PreparedStatement ps = null;
try {
conn.setAutoCommit(true);
String sql = "insert into RecordComment(CustomerID,swiftID,Grade,CommentContext)values ('"
+ customerId
+ "','"
+ swiftId
+ "','"
+ point
+ "','"
+ comment + "')";
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 獲取分頁信息
public int adminGetPageInfo(String mer, String area, int pageSize) {
int count = 0;
this.pageSize = pageSize;
String sql = "";
if (mer == null && area == null) {
sql = "select count(*) from [v_CustomerRecord]";
} else if (mer != null && area == null) {
sql = "select count(*) from [v_CustomerRecord] where MerchantName like '%"
+ mer + "%'";
} else if (mer == null && area != null) {
sql = "select count(*) from [v_CustomerRecord] where AreaName like '%"
+ area + "%'";
} else if (mer != null && area != null) {
sql = "select count(*) from [v_CustomerRecord] where MerchantName like '%"
+ mer + "%' and AreaName '%" + area + "%'";
}
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next())
count = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
// 獲取用戶記錄
public ArrayList adminGetCustomerRecord(String mer, String area,
int currentPage, int pageSize) throws Exception {
ResultSet rs = null;
PreparedStatement ps = null;
ArrayList al = new ArrayList();
String sql = "";
if (mer == null && area == null) {
sql = "select top "
+ pageSize
+ " [customerName],[CardID],[MerchantName],[GoodPrice],[DealTime],[GoodName] from [v_CustomerRecord] WHERE ID NOT IN (select TOP "
+ (currentPage - 1) * pageSize
+ " [ID] FROM [v_CustomerRecord])";
} else if (mer != null && area == null) {
sql = "select top "
+ pageSize
+ " [customerName],[CardID],[MerchantName],[GoodPrice],[DealTime],[GoodName] from [v_CustomerRecord] where MerchantName like '%"
+ mer
+ "%' and ID not in(select TOP "
+ (currentPage - 1)
* pageSize
+ " [ID] FROM [v_CustomerRecord] where MerchantName like '%"
+ mer + "%')";
} else if (mer == null && area != null) {
sql = "select top "
+ pageSize
+ " [customerName],[CardID],[MerchantName],[GoodPrice],[DealTime],[GoodName] from [v_CustomerRecord] where AreaName like '%"
+ area + "%' and ID not in(select TOP " + (currentPage - 1)
* pageSize
+ " [ID] FROM [v_CustomerRecord] where AreaName like '%"
+ area + "%')";
} else if (mer != null && area != null) {
sql = "select top "
+ pageSize
+ " [customerName],[CardID],[MerchantName],[GoodPrice],[DealTime],[GoodName] from [v_CustomerRecord] where MerchantName like '%"
+ mer
+ "%' and AreaName '%"
+ area
+ "%' and ID not in(select TOP "
+ (currentPage - 1)
* pageSize
+ " [ID] FROM [v_CustomerRecord] where MerchantName like '%"
+ mer + "%' and AreaName '%" + area + "%')";
}
try {
conn.setAutoCommit(true);
ps = conn.prepareStatement(sql);
System.out.println(sql);
rs = ps.executeQuery();
while (rs.next()) {
CustomerTradeRecordBean ctr = new CustomerTradeRecordBean();
ctr.setCustomerName(rs.getString(1));
ctr.setCardID(rs.getString(2));
ctr.setMerchantName(rs.getString(3));
ctr.setPurchasePrice(rs.getString(4));
ctr.setPurchaseDate(rs.getString(5));
ctr.setGoodName(rs.getString(6));
al.add(ctr);
}
return al;
} finally {
if (ps != null)
ps.close();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -