Compare commits

..

5 Commits
src ... daoimpl

@ -0,0 +1,257 @@
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;
}
}

@ -0,0 +1,84 @@
package flowershop.daoimpl;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class Jdbc {
// 数据连接URL
static String url;
// 创建Properties对象
static Properties info = new Properties();
// 静态代码块
static {
// 获取属性文件的输入流
InputStream input = Jdbc.class.getClassLoader().getResourceAsStream("config.properties");
try {
if (input == null) {
throw new NullPointerException("配置文件未找到");
}
// 加载属性文件
info.load(input);
// 从属性文件中读取url
url = info.getProperty("url");
// 从属性文件中读取driver
String driverClassName = info.getProperty("driver");
// 加载驱动程序
Class.forName(driverClassName);
System.out.println("驱动程序加载成功...");
} catch (ClassNotFoundException e) {
System.out.println("驱动程序加载失败...");
e.printStackTrace();
} catch (IOException e) {
System.out.println("属性文件加载失败...");
e.printStackTrace();
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
// 创建数据连接
public static Connection getConnection() throws SQLException {
// 创建数据连接
Connection conn = DriverManager.getConnection(url, info);
return conn;
}
// 关闭连接方法
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
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();
}
}
}
}

@ -0,0 +1,151 @@
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.OrderDao;
import flowershop.model.Order;
public class OrderDaoImpl implements OrderDao {
@Override
public List<Order> findAll() {
//方法用于查询所有订单信息执行了一个SQL查询语句将查询结果封装为Order对象列表并返回。
String sql = "select orderid,userid,orderdate from order";
List<Order> list = new ArrayList<Order>();
try (// 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4.绑定参数
// 5.执行查询
ResultSet rs = pstmt.executeQuery()) {
// 6.遍历结果集
while (rs.next()) {
Order order = new Order();
order.setOrderid(rs.getLong("orderid"));
order.setOrderdate(rs.getDate("orderdate"));
order.setUserid(rs.getString("userid"));
list.add(order);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return list;
}
@Override
public Order findById(String id) {
//方法根据订单ID查询特定订单信息执行了一个带参数的SQL查询语句
//将查询结果封装为Orderwjx对象并返回。在方法结束时关闭了数据库连接。
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 2.创建数据库连接
conn = Jdbc.getConnection(); //1.加载驱动程序
String sql = "select orderid,userid,orderdate,status,amount from order where orderid = ?";
// 3.创建语句对象
pstmt = conn.prepareStatement(sql);
// 4.绑定参数
pstmt.setString(1, id);
// 5.执行查询
rs = pstmt.executeQuery();
// 6.遍历结果集
if (rs.next()) {
Order order = new Order();
order.setOrderid(rs.getLong("orderid"));
order.setUserid(rs.getString("userid"));
order.setOrderdate(rs.getDate("orderdate"));
return order;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Jdbc.close(rs, pstmt, conn);
}
return null;
}
@Override
public int create(Order order) {
//方法用于插入新的订单信息执行了一个带参数的SQL插入语句并打印出成功插入的数据条数。
String sql = "insert into order(orderid,userid,orderdate,status,amount) values(?,?,?,?,?)";
try (// 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);) {
// 4.绑定参数
pstmt.setLong(1, order.getOrderid());
pstmt.setString(2, order.getUserid());
// utilDate转化为sql date
java.util.Date date = order.getOrderdate();
// java.sql.Date date = new java.sql.Date(now.getTime());
// Timestamp比Date更准确
pstmt.setDate(3, new java.sql.Date(date.getTime()));
pstmt.setTimestamp(3, new java.sql.Timestamp(date.getTime()));
pstmt.setInt(4, order.getStatus());
pstmt.setDouble(5, order.getAmount());
// 5.执行
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功插入%d数据.\n", affectedRows);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
return -1;
}
return 0;
}
@Override
public int modify(Order order) {
//根据订单ID更新订单金额。
String sql = "update order set amount = ? where orderid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setDouble(1, order.getAmount());
pstmt.setLong(2, order.getOrderid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功更新%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int remove(Order order) {
//removewjx(Orderwjx orderwjx)方法用于删除订单信息执行了一个带参数的SQL删除语句
//并打印出成功删除的数据条数。
String sql = "delete from order where orderid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setLong(1, order.getOrderid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
//try-with-resources语句确保了在方法结束时数据库连接、语句对象和结果集会被正确关闭从而避免了资源泄露问题。

@ -0,0 +1,160 @@
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.OrderDetailDao;
import flowershop.model.OrderDetail;
public class OrderDetailDaoImpl implements OrderDetailDao {
@Override
public List<OrderDetail> findAll() {
//通过执行SQL语句查询所有订单详细信息并将结果封装成OrderDetail对象的列表返回。
String sql = "select * from account";
List<OrderDetail> list = new ArrayList<OrderDetail>();
try( //2.创建数据库连接
Connection conn = Jdbc.getConnection();
//3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4.绑定参数
//5.执行查询
ResultSet rs = pstmt.executeQuery(); ){
//6.遍历结果集
while(rs.next()) {
OrderDetail detail = new OrderDetail();
detail.setOrderid(rs.getLong("orderid"));
detail.setProductid(rs.getString("productid"));
detail.setQuantity(rs.getInt("quantity"));
detail.setPrice(rs.getDouble("price"));
list.add(detail);
}
}catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public OrderDetail findByPk(int orderid, String productid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select orderid,productid,quantity,unitcost from ordersdatail where orderid=? and productid=?";
try {
//2.创建数据库连接
conn = Jdbc.getConnection();//1.加载驱动程序
//3.创建语句对象
pstmt= conn.prepareStatement(sql);
//4.绑定参数
pstmt.setInt(1, orderid);
pstmt.setString(2,productid);
//5.执行查询
rs = pstmt.executeQuery();
//6.遍历按主键查询
if(rs.next()) {
OrderDetail detail = new OrderDetail();
detail.setOrderid(rs.getLong("orderid"));
detail.setProductid(rs.getString("productid"));
detail.setQuantity(rs.getInt("quantity"));
detail.setPrice(rs.getDouble("price"));
return detail;
}
}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 int create(OrderDetail orderDetail) {
String sql = "insert into orderdetail(orderid,productid,quantity,price) values(?,?,?,?)";
try (// 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);) {
// 4.绑定参数
pstmt.setLong(1, orderDetail.getOrderid());
pstmt.setString(2, orderDetail.getProductid());
pstmt.setInt(3, orderDetail.getQuantity());
pstmt.setDouble(4, orderDetail.getPrice());
// 5.执行
int a = pstmt.executeUpdate();
System.out.printf("成功插入%d数据.\n",a);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return 0;
}
@Override
public int modify(OrderDetail orderDetail) {
String sql = "update ordersdetail set price = ? where orderid = ? and productid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setDouble(1, orderDetail.getPrice());
pstmt.setLong(2, orderDetail.getOrderid());
pstmt.setString(3, orderDetail.getProductid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功更新%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int remove(OrderDetail orderDetail) {
String sql = "delete from ordersdetail where orderid = ? and productid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setLong(1, orderDetail.getOrderid());
pstmt.setString(2, orderDetail.getProductid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}

@ -0,0 +1,217 @@
package flowershop.daoimpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import flowershop.dao.UserDao;
import flowershop.model.User;
public class UserDaoImpl implements UserDao {
//查询所有
@Override
public List<User> findAll() {
// TODO 自动生成的方法存根
ArrayList<User> users = new ArrayList<User>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/flowershop?userSSL=false&serverTimezone=Asia/Shanghai";
String user = "root";
String password = "Zhang13075099941";
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from user";
Statement sts = conn.createStatement();
ResultSet rs = sts.executeQuery(sql);
while (rs.next()) {
User user1 = new User();
user1.setUserid(rs.getString("userid"));
user1.setUsername(rs.getString("username"));
user1.setPassword(rs.getString("password"));
user1.setAddress(rs.getString("address"));
user1.setPhone(rs.getString("phone"));
users.add(user1);
System.out.println(user1.getUserid()+" " +user1.getUsername()
+" "+user1.getPassword()+" "+user1.getAddress()+" "
+user1.getPhone());
}
rs.close();
sts.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
//查询用户
@Override
public User findById(String userid) {
// TODO 自动生成的方法存根
User user = new User();
Connection conn ;
PreparedStatement pstmt ;
ResultSet rs ;
try {
conn = Jdbc.getConnection();
String sql = "select userid, username, password, address, phone from user where userid = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setUserid(rs.getString("userid"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setAddress(rs.getString("address"));
user.setPhone(rs.getString("phone"));
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
//创建
@Override
public int save(User user) {
// TODO 自动生成的方法存根
String sql = "insert into user (userid,password,username,address,phone) values (?,?,?,?,?,?,?)";
try( //2.创建数据库连接
Connection conn = Jdbc.getConnection();
//3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
){
//4.绑定参数
pstmt.setString(1, user.getUserid());
pstmt.setString(2, user.getPassword());
pstmt.setString(4, user.getUsername());
pstmt.setString(5, user.getAddress());
pstmt.setString(8, user.getPhone());
//5.执行
int a = pstmt.executeUpdate();
System.out.printf("成功创建%d数据\n",a);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
//插入
@Override
public int create(User user) {
String sql = "insert into user(userid,password,username,address,phone) values (?,?,?,?,?,?,?)";
try( //2.创建数据库连接
Connection conn = Jdbc.getConnection();
//3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
){
//4.绑定参数
pstmt.setString(1, user.getUserid());
pstmt.setString(2, user.getPassword());
pstmt.setString(4, user.getUsername());
pstmt.setString(5, user.getAddress());
pstmt.setString(8, user.getPhone());
//5.执行
int a = pstmt.executeUpdate();
System.out.printf("成功插入%d数据\n",a);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
//修改
@Override
public int modify(User user) {
String sql = "update user set password = ? where userid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getUserid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功更新%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//删除
@Override
public int remove(User user) {
String sql = "delete from user where userid = ?";
try ( // 2.创建数据库连接
Connection conn = Jdbc.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setString(1, user.getUserid());
// 5. 执行修改C、U、D
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public static void main(String[] args) {
UserDaoImpl user = new UserDaoImpl();
for (User al : user.findAll()) {
System.out.println(al);
}
System.out.println("-----------");
System.out.println(user.findById(""));
}
}
Loading…
Cancel
Save