📄 categorydao.java
字号:
package com.webshop.db;
import java.sql.*;
import com.webshop.domain.Category;
import com.webshop.exception.ActionException;
/**
* @author w
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class CategoryDAO {
Connection con;//数据库连接
String selectAllCategory="select * from category";
String selectCategoryById="select * from category where catid=?";
/**
* Constructor for CategoryDAO.
*/
public CategoryDAO() {
super();
}
/**
* 根据ID查找商品信息
*/
public Category getCategoryById(String id)
{
Category category=null;
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw new ActionException("不能获得连接,"+e.getMessage());
}
try
{
PreparedStatement pstmt=con.prepareStatement(selectCategoryById);
pstmt.setString(1,id);
ResultSet rst=pstmt.executeQuery();
if(rst.next())
{
category=new Category();
category.setCategoryId(rst.getString("catid"));
category.setName(rst.getString("name"));
category.setDescription(rst.getString("descn"));
}
con.close();
}
catch(SQLException e)
{
throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return category;
}
/**
* 获得所有的商品类别
* @return Collection
*/
public java.util.Collection getAllCategory()
{
java.util.Collection categoryes=new java.util.ArrayList();
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw new ActionException("不能获得连接,"+e.getMessage());
}
try
{
PreparedStatement pstmt=con.prepareStatement(selectAllCategory);
ResultSet rst=pstmt.executeQuery();
Category category;
while(rst.next())
{
category=new Category();
category.setCategoryId(rst.getString("catid"));
category.setName(rst.getString("name"));
category.setDescription(rst.getString("descn"));
categoryes.add(category);
}
con.close();
}
catch(SQLException e)
{
throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return categoryes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -