东西在附件里
有劳各位了
这是数据处理部分
package dao;
import java.util.ArrayList;
import model.Catalog;
/**
*
* @author moon
*/
public interface CatalogDAO {
public void add(Catalog c);
public void update(Catalog c);
public void delete(Catalog c);
public Catalog get(int id);
public ArrayList getCatalog();
public ArrayList getThings(Catalog c);
}
import dao.CatalogDAO;
import dao.DBUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Catalog;
import model.Things;
/*
* CatalogDAOImpl.java
*
* Created on 2007年10月30日, 下午6:51
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author wina
*/
public class CatalogDAOImpl implements CatalogDAO{
/** Creates a new instance of CatalogDAOImpl */
public CatalogDAOImpl() {
}
public void add(Catalog c) {
DBUtil db = new DBUtil();
try {
PreparedStatement ps = db.getConnection().prepareStatement("INERT INTO catalog(name) VALUES(?)");
ps.setString(1,c.getName());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void update(Catalog c) {
DBUtil db = new DBUtil();
try {
PreparedStatement ps = db.getConnection().prepareStatement("UPDATE catalog SET name=? WHERE id=?");
ps.setString(1,c.getName());
ps.setInt(2,c.getID());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void delete(Catalog c) {
DBUtil db = new DBUtil();
try {
PreparedStatement ps = db.getConnection().prepareStatement("DELETE catalog WHERE id=?");
ps.setInt(1,c.getID());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public Catalog get(int id) {
Catalog c = new Catalog();
DBUtil db = new DBUtil();
PreparedStatement ps;
try {
ps = db.getConnection().prepareStatement("SELECT * FROM catalog WHERE id=?");
ps.setInt(1,c.getID());
ResultSet rs= ps.executeQuery();
if(rs.next())
{
c.setID(rs.getInt(1));
c.setName(rs.getString(2));
}
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return c;
}
public ArrayList getCatalog() {
ArrayList al = new ArrayList();
DBUtil db = new DBUtil();
Statement s;
try {
s = db.getConnection().createStatement();
ResultSet rs= s.executeQuery("SELECT * FROM catalog");
while (rs.next())
{
Catalog c = new Catalog();
c.setID(rs.getInt(1));
c.setName(rs.getString(2));
al.add(c);
}
s.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return al; //按传说中的命名规则,应该改为result;
}
public ArrayList getThings(Catalog c) {
ArrayList al = new ArrayList();
DBUtil db = new DBUtil();
PreparedStatement ps;
try {
ps = db.getConnection().prepareStatement("SELECT * FROM catalog WHERE catalog_id=?");
ps.setInt(1,c.getID());//设置第一个位置
ResultSet rs= ps.executeQuery();
while (rs.next())
{
Things thing = new Things();
thing.setID(rs.getInt(1));
thing.setTitle(rs.getString(2));
thing.setAuthor(rs.getString(3));
thing.setPrice(rs.getDouble(4));
thing.setPicture(rs.getString(5));
thing.setPostTime(rs.getDate(6));
thing.setClicks(rs.getInt(7));
al.add(thing);
}
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return al; //按传说中的命名规则,应该改为result;
}
}
/*
* DBUtil.java
*
* Created on 2007年10月30日, 下午6:53
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dao;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
*
* @author moon
*/
public class DBUtil {
private static Connection conn;
/** Creates a new instance of DBUtil */
public DBUtil()
{
if(conn ==null)
{
Context ctx;
try {
ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/moonStore");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static Connection getConnection()
{
return this.conn;
}
}
/*
* MoonDAO.java
*
* Created on 2007年11月1日, 上午10:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dao;
import java.util.ArrayList;
import model.Things;
/**
*
* @author moon
*/
public interface ThingsDAO {
public void add(Things thing);
public void update(Things thing);
public void delete(Things thing);
public Things get(int id);
public ArrayList newThings();
public ArrayList hotThings();
}
/*
* ThingsDAOImpl.java
*
* Created on 2007年11月1日, 上午10:25
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Things;
/**
*
* @author moon
*/
public class ThingsDAOImpl implements ThingsDAO {
/** Creates a new instance of ThingsDAOImpl */
public ThingsDAOImpl() {
}
public void add(Things thing) {
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO things(title,author,price,picture,postTime,clicks,catalog_ID) VALUES (?,?,?,?,now(),0,?)");
ps.setString(1,thing.getTitle());
ps.setString(2,thing.getAuthor());
ps.setDouble(3,thing.getPrice());
ps.setString(4,thing.getPicture());
ps.setDate(5,thing.getPostTime());
ps.setInt(6,thing.getClicks());
ps.setInt(7,thing.getCatalogID());
ps.executeUpdate(); //这是干什么? 刷新???
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void update(Things thing) {
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn.prepareStatement("UPDATE things SET(title=?,author=?,price=?,picture=?,postTime=?,clicks=?,catalog_ID=?) VALUES (?,?,?,?,now(),0,?) WHERE id=?");
ps.setString(1,thing.getTitle());
ps.setString(2,thing.getAuthor());
ps.setDouble(3,thing.getPrice());
ps.setString(4,thing.getPicture());
ps.setDate(5,thing.getPostTime());
ps.setInt(6,thing.getClicks());
ps.setInt(7,thing.getCatalogID());
ps.setInt(8,thing.getID());
ps.executeUpdate(); //这是干什么? 刷新???
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void delete(Things thing) {
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn.prepareStatement("DELETE FROM things WHERE id=?");
ps.setInt(1,thing.getID()); //为什么把他设为1
ps.executeUpdate(); //这是干什么? 刷新???
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public Things get(int id) {
Connection conn = DBUtil.getConnection();
Things thing = new Things();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM things WHERE id=?");
ps.setInt(1,thing.getID()); //为什么把他设为1
ResultSet rs = ps.executeQuery();
if(rs.next())
{
ps.setInt(1,thing.getID());
ps.setString(2,thing.getTitle());
ps.setString(3,thing.getAuthor());
ps.setDouble(4,thing.getPrice());
ps.setString(5,thing.getPicture());
ps.setDate(6,thing.getPostTime());
ps.setInt(7,thing.getClicks());
ps.setInt(8,thing.getCatalogID());
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return thing;
}
public ArrayList newThings() {
ArrayList result = new ArrayList();
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM things ORDER BY post_time DESC LIMIT 0,5");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
Things thing = new Things();
ps.setInt(1,thing.getID());
ps.setString(2,thing.getTitle());
ps.setString(3,thing.getAuthor());
ps.setDouble(4,thing.getPrice());
ps.setString(5,thing.getPicture());
ps.setDate(6,thing.getPostTime());
ps.setInt(7,thing.getClicks());
ps.setInt(8,thing.getCatalogID());
result.add(thing);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return result;
}
public ArrayList hotThings() {
return null; //暂未实现
}
}
/*
* UserDAO.java
*
* Created on 2007年11月3日, 上午11:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.User;
/**
*
* @author moon
*/
public class UserDAO {
/** Creates a new instance of UserDAO */
public UserDAO() {
}
public boolean isUserValid(User u)
{
boolean result = false;
PreparedStatement pstmt;
try {
pstmt = DBUtil.getConnection().prepareStatement("SELECT count(*) FROM user WHERE name=? AND password=?");
pstmt.setString(1,u.getName());
pstmt.setString(2,u.getPassword());
ResultSet rs = pstmt.executeQuery();
if(rs.next())
{
if(rs.getInt(1)==1)
{
result = true;
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return result;
}
}
//这是服务端部分
/*
* Catalog.java
*
* Created on 2007年10月30日, 下午6:47
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
/**
*
* @author moon
*/
public class Catalog {
private int ID;
private String name;
/** Creates a new instance of Catalog */
public Catalog() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* CatalogService.java
*
* Created on 2007年10月30日, 下午7:43
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
import CatalogDAOImpl; //什么毛病居然说有问题
import dao.CatalogDAO;
import java.util.ArrayList;
/**
*
* @author moon
*/
public class CatalogService {
public static ArrayList getCatalogs() {
CatalogDAO cDAO = new CatalogDAOImpl() ;
return cDAO.getCatalog();
}
public static ArrayList getThings(Catalog c) {
CatalogDAO cDAO = new CatalogDAOImpl() ;
return cDAO.getThings(c);
}
public static void addThing(Catalog c, Things thing) {
ThingsDAO thingDAO = new ThingsDAOImpl();
thing.setCatalogID(c.getID());
thingDAO.add(thing);
}
public static void removeThing(Catalog c, Things thing) {
ThingsDAO thingDAO = new ThingsDAOImpl();
thingDAO.delete(thing);
}
public static void addCatalog(Catalog c) {
CatalogDAO cDAO = new CatalogDAOImpl() ;
cDAO.add(c);
}
public static void delCatalog(Catalog c) {
CatalogDAO cDAO = new CatalogImpl();
cDAO.delete(c);
}
public static Catalog getCatalog(int id) {
CatalogDAO cDAO = new CatalogImpl();
cDAO.get(id);
}
public static void updateCatalog(Catalog c) {
CatalogDAO cDAO = new CatalogImpl();
cDAO.update(c);
}
public static void getThing(int ID)
{
ThingsDAO thingDAO = new ThingsDAOImpl();
return thingDAO.get(ID);
}
public static void updateThing(Things thing)
{
ThingsDAO thingDAO = new ThingsDAOImpl();
thingDAO.update(thing);
}
public static ArrayList getNewThings()
{
return (new ThingsDAOImpl()).newThings();
}
public static ArrayList getHotThing()
{
return (new ThingsDAOImpl()).hotThings();
}
public static void updateCliks(Things thing)
{
thing.setClicks(thing.getClicks()+1);
CatalogService.updateThing(thing);
}
public static boolean isUserValid(User u)
{
UserDAO uDAO = new UserDAO();
return uDAO.isUserValid();
}
public static void addItem(int id,ShoppingCart cart)
{
Things thing =CatalogService.getThing(id);
cart.addItem(new Integer(id),thing); //为什么非得用Integer 用int不好吗?!
}
}
/*
* CatalogServiceImpl.java
*
* Created on 2007年10月30日, 下午7:50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
import CatalogDAOImpl;
import dao.CatalogDAO;
import java.util.ArrayList;
/**
*
* @author moon
*/
public class CatalogServiceImpl implements CatalogService {
/** Creates a new instance of CatalogServiceImpl */
public CatalogServiceImpl() {
}
public ArrayList getCatalogs() {
CatalogDAO cDAO = new CatalogDAOImpl() ;
return cDAO.getCatalog();
}
public ArrayList getThings(Catalog c) {
}
}
//购物车
/*
* ShoppingCart.java
*
* Created on 2007年11月3日, 下午12:06
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
/**
*
* @author moon
*/
public class ShoppingCart {
HashMap<Integer,ThingsItem> items = null;
/** Creates a new instance of ShoppingCart */
public ShoppingCart() {
items = new HashMap<Integer,ThingsItem>();
}
public synchronized void addItem(Integer thingID,Things thing) //同步避免死锁
{
if(!items.containsKey(thingID))
{
ThingsItem i = new ThingsItem();
i.setThing(thing);
items.put(thingID,i);
}
}
public synchronized void deleteItem(Integer thingID,Things thing) //删除一条
{
if(items.containsKey(thingID))
{
items.remove(thingID);
}
}
public synchronized void clear() //清除购物车
{
items.clear();
}
public int getNumOfItems()
{
return items.size();
}
public synchronized void setItemNum(Integer thingID,int quatity) //大单
{
if(items.containsKey(thingID))
{
if(quatity<=0)
{
items.remove(thingID);
}
else
{
ThingsItem ti = items.get(thingID);
ti.setQuatity(quatity);
}
}
}
public synchronized Collection<ThingsItem> getItems()
{
return items.values();
}
public synchronized double getTotalPrice()
{
double result = 0.0;
Collection<ThingsItem> thingItems = items.values();
Iterator i = thingItems.iterator();
while(i.hasNext())
{
ThingsItem ti = (ThingsItem)i.next();
result+=ti.getItemPrice();
}
return result;
}
}
/*
* Things.java
*
* Created on 2007年11月1日, 上午8:59
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
import java.sql.Date;
/**
*
* @author moon
*/
//作用是将数据库中的数据转化为java中数据
public class Things {
/** Creates a new instance of Things */
private int ID;
private String title; //标注信息
private String author; //厂家
private double price;// 价格
private String picture;//标本图片
private Date postTime; //添加的时间
private int clicks ;//点击次数
public Things() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Date getPostTime() {
return postTime;
}
public void setPostTime(Date postTime) {
this.postTime = postTime;
}
public int getClicks() {
return clicks;
}
public void setClicks(int clicks) {
this.clicks = clicks;
}
public int getCatalogID() { //暂未实现
return 0;
}
}
//用户列表
/*
* BookItem.java
*
* Created on 2007年11月3日, 下午12:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
/**
*
* @author moon
*/
public class ThingsItem {
/** Creates a new instance of BookItem */
private Things thing;
private int quatity;
public ThingsItem() {
}
public Things getThing() {
return thing;
}
public void setThing(Things thing) {
this.thing = thing;
}
public int getQuatity() {
return quatity;
}
public void setQuatity(int quatity) {
this.quatity = quatity;
}
public double getItemPrice()
{
return thing.getPrice()*quatity;
}
}
//用户
/*
* User.java
*
* Created on 2007年11月3日, 上午11:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package model;
/**
* 建立对应表user (name,password,role(权限))
* @author moon
*/
public class User {
/** Creates a new instance of User */
private String ID;
private String name;
private String password;
private int role;
public User() {
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
}