这是数据处理部分
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;
}
}