You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
localhost9000/DishDaoImplwcy.java

219 lines
5.4 KiB

package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import dao.DishDAOwcy;
import domain.Dishwcy;
public class DishDaoImplwcy implements DishDAOwcy {
@Override
public ArrayList<Dishwcy> findAll() {
String sql ="select *from dish";
ArrayList<Dishwcy> dishs=new ArrayList<Dishwcy>();
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm =conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery(sql);
while(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));
pr.setUnitcost(rs.getDouble("unitcost"));
dishs.add(pr);
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return dishs;
}
@Override
public Dishwcy findById(String dishid) {
Connection conn=null;
PreparedStatement pstm =null;
ResultSet rs=null;
try {
conn=JDBCUtil.getConnection();
String sql="select * from dish where dishid=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, dishid);
rs=pstm.executeQuery();
if(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));
pr.setUnitcost(rs.getDouble("unitcost"));
return pr;
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
}
return null;
}
@Override
public ArrayList<Dishwcy> findByCategory(String category) {
Connection conn =null;
PreparedStatement pstm =null;
ResultSet rs=null;
ArrayList<Dishwcy> dishs=new ArrayList<Dishwcy>();
try {
conn=JDBCUtil.getConnection();
String sql="select * from dish where category=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, category);
rs=pstm.executeQuery();
while(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));;
pr.setUnitcost(rs.getDouble("unitcost"));
dishs.add(pr);
}
rs.close();
pstm.close();
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
}
return dishs;
}
@Override
public int modify(Dishwcy dish) {
String sql="update info ? ?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功更新%d条数据。\n",affectedRows);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return 0;
}
@Override
public int delete(Dishwcy dish) {
String sql="delect from dish where?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n",affectedRows);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return 0;
}
@Override
public int insert(Dishwcy dish) {
try {
Connection conn=JDBCUtil.getConnection();
String sql="insert into dish values(?,?,?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows=pstm.executeUpdate();
System.out.printf("成功插入%d条数据。\n",affectedRows);
} catch (Exception e) {
return -1;
}
return 0;
}
}