package edu.syiae.business.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import edu.syiae.business.javaBean.Buyer;
import edu.syiae.business.javaBean.Goods;
import edu.syiae.business.javaBean.Seller;
public class Manager {
public Manager() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Connection con() {
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/leaoway", "root", "esoft");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 检查买家用户名是否存在
public boolean checkBuyerName(String username) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from buyer where bName = ?";
try {
con = this.con();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 买家注册
public void regBuyer(Buyer buyer) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = "insert into buyer (bName,bPassword,bRealname,bSex,bTel,bMobile,bQQ) "
+ "values(?,?,?,?,?,?,?)";
con = this.con();
try {
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, buyer.getUsername());
pstmt.setString(2, buyer.getPassword());
pstmt.setString(3, buyer.getRealname());
pstmt.setString(4, buyer.getSex());
pstmt.setString(5, buyer.getTel());
pstmt.setString(6, buyer.getMobile());
pstmt.setString(7, buyer.getQq());
pstmt.executeUpdate();
con.commit();
} catch (SQLException e) {
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 检验卖家用户名是否存在
public boolean checkSellerName(String username) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from seller where sName = ? ";
try {
con = this.con();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 卖家注册
public void regSeller(Seller seller) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = "insert into seller (sName,sPassword,sRealname,sSex,sTel,sMobile,sQQ) "
+ "values(?,?,?,?,?,?,?)";
try {
con = this.con();
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, seller.getUsername());
pstmt.setString(2, seller.getPassword());
pstmt.setString(3, seller.getRealname());
pstmt.setString(4, seller.getSex());
pstmt.setString(5, seller.getTel());
pstmt.setString(6, seller.getMobile());
pstmt.setString(7, seller.getQq());
pstmt.executeUpdate();
con.commit();
} catch (SQLException e) {
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 检查买家登陆账号
public boolean checkBuyerName(String username, String password) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select bPassword from buyer where bName = ?";
con = this.con();
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
if (password.equals(rs.getString("bPassword"))) {
return true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 检查卖家登陆账号
public boolean checkSellerName(String username, String password) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select sPassword from seller where sName = ?";
con = this.con();
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
if (password.equals(rs.getString("sPassword"))) {
return true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 计算总商品数量
public int countAllGoods() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select count(*) from goods";
con = this.con();
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
int count = rs.getInt(1);
return count;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
// 分页显示所有商品
public List<Goods> pageSearchShowAllGoods(int pageStart, int pageCount) {
List<Goods> list = new ArrayList<Goods>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from goods limit ?,?";
con = this.con();
try {
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, pageStart);
pstmt.setInt(2, pageCount);
rs = pstmt.executeQuery();
while (rs.next()) {
Goods g = new Goods();
g.setGid(rs.getInt("gid"));
g.setGname(rs.getString("gname"));
g.setGpic(rs.getString("gpic"));
g.setGsal(rs.getInt("gsal"));
g.setGnum(rs.getInt("gnum"));
g.setGtype(rs.getString("gtype"));
list.add(g);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
// 查找买家ID
public int findBuyerId(String username) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select bid from buyer where bname = ?";
con = this.con();
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
int bid = rs.getInt("bid");
return bid;