# CategoryDAO
CategoryDAO 這個類比起后面的DAO比較單純,基本上就是提供數據庫相關的CRUD操作:
```
package com.dodoke.dao.inter;
import java.util.List;
import com.dodoke.bean.Category;
public interface CategoryDao {
/**
* 增加
*
* @param category
*/
public void add(Category category);
/**
* 刪除
*
* @param id
*/
public void delete(int id);
/**
* 修改
*
* @param bean
*/
public void update(Category category);
/**
* 根據id獲取
*
* @param id
* @return
*/
public Category get(int id);
/**
* 分頁查詢
*
* @param start
* @param count
* @return
*/
public List<Category> list(int start, int count);
/**
* 查詢所有
*
* @return
*/
public List<Category> list();
/**
* 獲取總數
*
* @return
*/
public int getTotal();
}
```
# 實現CategoryDaoImpl
```
package com.dodoke.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dodoke.bean.Category;
import com.dodoke.dao.inter.CategoryDao;
import com.dodoke.util.DBUtil;
public class CategoryDaoImpl implements CategoryDao {
@Override
public void add(Category category) {
String sql = "insert into t_category values(null,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, category.getName());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
category.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(int id) {
String sql = "delete from t_category where id = " + id;
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Category category) {
String sql = "update t_category set name= ? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, category.getName());
ps.setInt(2, category.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public Category get(int id) {
Category category = null;
String sql = "select * from t_category where id = " + id;
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ResultSet rs = ps.executeQuery(sql);
if (rs.next()) {
category = new Category();
String name = rs.getString("name");
category.setName(name);
category.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return category;
}
@Override
public List<Category> list(int start, int count) {
List<Category> categorys = new ArrayList<Category>();
String sql = "select * from t_category order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Category category = new Category();
int id = rs.getInt("id");
String name = rs.getString("name");
category.setId(id);
category.setName(name);
categorys.add(category);
}
} catch (SQLException e) {
e.printStackTrace();
}
return categorys;
}
@Override
public List<Category> list() {
// 利用現有的分頁方法,獲取所有數據。至于short.MAX_VALUE數值足夠大,不夠再說
return list(0, Short.MAX_VALUE);
}
@Override
public int getTotal() {
int total = 0;
String sql = "select count(*) from t_category";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
}
```
- 項目簡介
- 功能一覽
- 前臺
- 后臺
- 開發流程
- 需求分析-展示
- 首頁
- 產品頁
- 分類頁
- 搜索結果頁
- 購物車查看頁
- 結算頁
- 確認支付頁
- 支付成功頁
- 我的訂單頁
- 確認收貨頁
- 評價頁
- 頁頭信息展示
- 需求分析-交互
- 分類頁排序
- 立即購買
- 加入購物車
- 調整訂單項數量
- 刪除訂單項
- 生成訂單
- 訂單頁功能
- 確認付款
- 確認收貨
- 提交評價信息
- 登錄
- 注冊
- 退出
- 搜索
- 前臺需求列表
- 需求分析后臺
- 分類管理
- 屬性管理
- 產品管理
- 產品圖片管理
- 產品屬性設置
- 用戶管理
- 訂單管理
- 后臺需求列表
- 表結構設計
- 數據建模
- 表與表之間的關系
- 實體類設計
- DAO類設計
- 工具類
- CategoryDao設計
- Service業務類設計
- 后臺-分類管理
- 可運行的項目
- 靜態資源
- FILTER配合SERVLET
- JSP包含關系
- 查詢
- 分頁
- 增加
- 刪除
- 編輯
- 修改
- 后臺其他管理
- 屬性管理
- 產品管理
- 產品圖片管理
- 產品屬性值設置
- 用戶管理
- 訂單管理