📄 typedao.java
字号:
package com.wl.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.wl.dbconnection.DBManager;
import com.wl.entity.Type;
/**
* 商品型号表dao操作
*
* @author yuan xinfeng
*
*/
public class TypeDAO {
DBManager db = new DBManager();
ResultSet rs = null;
public TypeDAO() {
}
/**
* 查询所有的商品型号
*
* @return list
*/
// ---------------------查询所有的商品型号--------------------------
public List<Type> selectAllType() {
List<Type> list = new ArrayList<Type>();
String sql = "select * from typetable";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
rs = db.ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("Id");
String typeid = rs.getString("TypeId");
String typename = rs.getString("TypeName");
int categoryid = rs.getInt("CategoryId");
Type type = new Type(id, typeid, typename, categoryid);
list.add(type);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return list;
}
/**
* 根据商品型号信息表自动编号查询表中记录数据
*
* @param id
* @return type
*/
// ---------------------------根据商品型号信息表自动编号查询表中记录数据---------------------------
public Type selectTypeById(int id) {
Type type = null;
String sql = "select * from typetable where Id=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(1, id);
rs = db.ps.executeQuery();
while (rs.next()) {
id = rs.getInt("Id");
String typeid = rs.getString("TypeId");
String typename = rs.getString("TypeName");
int categoryid = rs.getInt("CategoryId");
type = new Type(id, typeid, typename, categoryid);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return type;
}
/**
* 根据商品型号查询表中信息
*
* @param typeid
* @return list
*/
// ----------------------根据商品型号查询表中信息---------------------------------
public Type selectTypeBytypeId(String typeid) {
Type type=null;
String sql = "select * from typetable where TypeId=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setString(1, typeid);
rs = db.ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("Id");
typeid = rs.getString("TypeId");
String typename = rs.getString("TypeName");
int categoryid = rs.getInt("CategoryId");
type = new Type(id, typeid, typename, categoryid);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return type;
}
public static void main(String [] args){
TypeDAO dao = new TypeDAO();
dao.selectTypeBytypeId("ck450");
}
/**
* 根据商品名称查询表中的记录信息
*
* @param typename
* @return list
*/
// -----------------------根据商品名称查询表中的记录信息---------------------------
public List<Type> selectTypeByName(String typename) {
List<Type> list = new ArrayList<Type>();
String sql = "select * from typetable where TypeName=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setString(1, typename);
rs = db.ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("Id");
String typeid = rs.getString("TypeId");
typename = rs.getString("TypeName");
int categoryid = rs.getInt("CategoryId");
Type type = new Type(id, typeid, typename, categoryid);
list.add(type);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return list;
}
/**
* 根据商品分类id查询表中记录数据
*
* @param categoryid
* @return list
*/
// ---------------------------根据商品分类id查询表中记录数据---------------------------
public List<Type> selectTypeByCategoryId(int categoryid) {
List<Type> list = new ArrayList<Type>();
String sql = "select * from typetable where Id=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(1, categoryid);
rs = db.ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("Id");
String typeid = rs.getString("TypeId");
String typename = rs.getString("TypeName");
categoryid = rs.getInt("CategoryId");
Type type = new Type(id, typeid, typename, categoryid);
list.add(type);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return list;
}
/**
* 根据商品型号信息表自动编号删除商品型号表中的记录
*
* @param id
* @return boolean
*/
// ----------------------根据商品型号信息表自动编号删除商品型号表中的记录---------------------
public boolean delTypeById(int id) {
String sql = "delete from typetable where Id=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(1, id);
db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return true;
}
/**
* 根据商品型号删除商品型号表中的记录
*
* @param typeid
* @return boolean
*/
// ----------------------根据商品型号删除商品型号表中的记录---------------------
public boolean delTypeByTypeId(String typeid) {
String sql = "delete from typetable where TypeId=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setString(1, typeid);
db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return true;
}
/**
* 根据商品名称删除商品型号表中的记录
*
* @param typename
* @return boolean
*/
// ----------------------根据商品名称删除商品型号表中的记录---------------------
public boolean delTypeByTypeName(String typename) {
String sql = "delete from typetable where TypeName=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setString(1, typename);
db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return true;
}
/**
* 根据商品分类id删除商品型号表中的记录
*
* @param categoryid
* @return boolean
*/
// ----------------------根据商品分类id删除商品型号表中的记录---------------------
public boolean delTypeByCategoryId(int categoryid) {
String sql = "delete from typetable where categoryid=?";
db.openDB();
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(1, categoryid);
db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
} finally {
db.closeDB();
}
return true;
}
/**
* 向商品型号表中插入数据记录
* @param type
* @return boolean
*/
// ------------------------向商品型号表中插入数据记录---------------------------------
public boolean insertType(Type type){
String sql = "insert into typetable(TypeId,TypeName,CategoryId) values(?,?,?)";
db.openDB();
try {
db.ps=db.conn.prepareStatement(sql);
db.ps.setString(1, type.getTypeId());
db.ps.setString(2,type.getTypeName());
db.ps.setInt(3, type.getCategoryId());
db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}finally{
db.closeDB();
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -