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.
java/FlowerDaoImpl.java

257 lines
7.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package flowershop.daoimpl;
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 flowershop.dao.FlowerDao;
import flowershop.model.Flower;
public class FlowerDaoImpl implements FlowerDao {
//查询所有
@Override
public List<Flower> findAll() {
// TODO 自动生成的方法存根
String sql = "select productid,name,category,image,price,descn from flower";
List<Flower> list = new ArrayList<Flower>();
try (// 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4.绑定参数
// 5.执行查询
ResultSet rs = pstmt.executeQuery(sql)) {
// 6.遍历结果集
while (rs.next()) {
Flower f = new Flower();
f.setProductid(rs.getString("productid"));
f.setName(rs.getString("name"));
f.setCategory(rs.getString("category"));
f.setImage(rs.getString("image"));
f.setPrice(rs.getDouble("price"));
f.setDescn(rs.getString("descn"));
list.add(f);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return list;
}
//按主键查询用户
@Override
public Flower findById(String productid) {
// TODO 自动生成的方法存根
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select productid,name,category,image,price,descn from flower where productid=?";
try {
// 2.创建数据库连接
conn = Jdbc.getConnection();// 1.加载驱动程序
// 3.创建语句对象
pstmt = conn.prepareStatement(sql);
// 4.绑定参数
pstmt.setString(1, productid);
// 5.执行查询
rs = pstmt.executeQuery();
// 6.遍历按主键查询
if (rs.next()) {
Flower f = new Flower();
f.setProductid(rs.getString("productid"));
f.setName(rs.getString("name"));
f.setCategory(rs.getString("category"));
f.setImage(rs.getString("image"));
f.setPrice(rs.getDouble("price"));
f.setDescn(rs.getString("descn"));
return f;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
//按名字查询
@Override
public List<Flower> findByCategory(String category) {
// TODO 自动生成的方法存根
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Flower> list = new ArrayList<Flower>();
String sql = "select productid,name,category,image,price,descn from flower where category=?";
try {
// 2.创建数据库连接
conn = Jdbc.getConnection();// 1.加载驱动程序
// 3.创建语句对象
pstmt = conn.prepareStatement(sql);
// 4.绑定参数
pstmt.setString(1,category);
// 5.执行查询
rs = pstmt.executeQuery();
// 6.遍历按主键查询
while (rs.next()) {
Flower f = new Flower();
f.setProductid(rs.getString("productid"));
f.setName(rs.getString("name"));
f.setCategory(rs.getString("category"));
f.setImage(rs.getString("image"));
f.setPrice(rs.getDouble("price"));
f.setDescn(rs.getString("descn"));
list.add(f);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
@Override
public int create(Flower flower) {
String sql = "insert into flower(productid,name,category,image,price,descn) values (?,?,?,?,?,?)";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);) {
// 4.绑定参数
pstmt.setString(1, flower.getProductid());
pstmt.setString(2, flower.getName());
pstmt.setString(3, flower.getCategory());
pstmt.setString(4, flower.getImage());
pstmt.setDouble(5, flower.getPrice());
pstmt.setString(6, flower.getDescn());
// 5.执行
int executeUpdate = pstmt.executeUpdate();
System.out.printf("成功插入%d数据\n", executeUpdate);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
@Override
public int modify(Flower flower) {
String sql = "update flower set price = ? where productid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setDouble(1, flower.getPrice());
pstmt.setString(2, flower.getProductid());
// 5. 执行修改C、U、D
int executeUpdate = pstmt.executeUpdate();
System.out.printf("成功更新%d条数据。\n", executeUpdate);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int remove(Flower flower) {
String sql = "delete from flower where productid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setString(1, flower.getProductid());
// 5. 执行修改C、U、D
int executeUpdate = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", executeUpdate);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}