Compare commits

...

5 Commits
main ... impl

@ -0,0 +1,152 @@
package com.lsy.dao.impl;
import com.lsy.dao.AccountDao;
import com.lsy.model.Account;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class AccountDaoImp implements AccountDao {
@Override
public List<Account> findAll() {
Account account;
List<Account> list = new ArrayList<>();
String sql = "select * from accountlsy ";
try (
//1.创建数据库连接
Connection conn = DBHelper.getConnection();
//2.创建语句对象
PreparedStatement pstm = conn.prepareStatement(sql);
//3.执行查询
ResultSet rs = pstm.executeQuery();) {
// 6.遍历结果集
while (rs.next()) {
account = new Account();
account.setUserid(rs.getString("userid"));
account.setPassword(rs.getString("password"));
account.setIDcard(rs.getString("IDcard"));
account.setName(rs.getString("name"));
account.setPhone(rs.getString("phone"));
account.setRoomN(rs.getString("roomN"));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public Account findById(String userid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Account account = null;
try {
conn = DBHelper.getConnection();
String sql = "select * from accountlsy where userid = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
if (rs.next()) {
account = new Account();
account.setUserid(rs.getString("userid"));
account.setPassword(rs.getString("password"));
account.setIDcard(rs.getString("IDcard"));
account.setName(rs.getString("name"));
account.setPhone(rs.getString("phone"));
account.setRoomN(rs.getString("roomN"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return account;
}
@Override
public int create(Account account) {
String sql = "insert into accountlsy values(?,?,?,?,?,?)";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql);
) {
// 6.遍历结果集
pstm.setString(1, account.getUserid());
pstm.setString(2, account.getPassword());
pstm.setString(5, account.getRoomN());
pstm.setString(4, account.getName());
pstm.setString(6, account.getPhone());
pstm.setString(3, account.getIDcard());
int a = pstm.executeUpdate();
System.out.printf("成功插入%d数据\n", a);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public int delect(Account account) {
String sql = "DELETE from accountlsy where userid= ?";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql)) {
pstm.setString(1, account.getUserid());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}

@ -0,0 +1,158 @@
package jpestore.dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import jpestore.dao.OrderDao;
import jpestore.domain.Order;
import jpestore.domain.Product;
public class OrderDaolmp implements OrderDao {
@Override
public List<Order> findAll() {
String sql = "select orderid,userid,orderdate, from orders";
List<Order>list = new ArrayList<Order>();
try(
Connection conn = null;
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()){
{
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) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 2.创建数据库连接
conn = DBHelper.getConnection(); //1.加载驱动程序
String sql = "select orderid,userid,orderdate,status,amount from account 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 {
try {
DBHelper.close(rs, pstmt, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
public int create(Order order) {
String sql = "insert into orders (orderid,userid,orderdate,status,amount)values (?,?,?,?,?)";
List<Product>list = new ArrayList<Product>();
try(
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
){pstmt.setLong(1, order.getOrderid());
pstmt.setString(2, order.getUserid());
java.util.Date now = new java.util.Date();
java.sql.Date date = new java.sql.Date(now.getTime());
java.sql.Timestamp date1 = new java.sql.Timestamp(now.getTime());
pstmt.setTimestamp(3,date1);
pstmt.setInt(4, order.getStatus());
pstmt.setDouble(4, order.getStatus());
pstmt.setDouble(5, order.getAmout());
int a = pstmt.executeUpdate();
System.out.printf("成功输入%d数据.\n",a);
}catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return 0;
}
@Override
public int modify(Order order) {
// TODO Auto-generated method stub
String sql = "update orders set amount = ? where orderid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setDouble(1, order.getAmout());
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) {
String sql = "delete from orders where orderid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.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;
}
}

@ -0,0 +1,142 @@
package jpestore.dao.mysql;
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 jpestore.dao.OrdertailDao;
import jpestore.domain.OrderDetail;
import jpestore.domain.Product;
public class OrderDetailDaolmp implements OrdertailDao {
@Override
public List<OrderDetail> findAll() {
// TODO 自动生成的方法存根
return null;
}
@Override
public OrderDetail findByPl(int orderid, String productid) {
Connection conn = null;
PreparedStatement pstmt = null ;
ResultSet rs = null;
List<Product>list = new ArrayList<Product>();
String sql = "select orderid,productid,quantity,unitcost from ordersdetail where orderid= and productid=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, orderid);
pstmt.setString(2, productid);
rs = pstmt.executeQuery();
if(rs.next()) {
OrderDetail detail = new OrderDetail();
detail.setOrderid(rs.getInt("orderid"));
detail.setProductid(rs.getString("orderid"));
detail.setQuantity(rs.getInt("quantity"));
detail.setUnitcost(rs.getDouble("unitcost"));
}} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
// TODO 自动生成的方法存根
return null ;
}
@Override
public int create(OrderDetail orderDetail) {
String sql = "insert into ordersdetail (orderid,productid,quantity,unitcost)values (?,?,?,?)";
try(
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
){pstmt.setLong(1, orderDetail.getOrderid());
pstmt.setString(2, orderDetail.getProductid());
pstmt.setInt(3,orderDetail.getQuantity());
pstmt.setDouble(4, orderDetail.getUnitcost());
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) {
// TODO Auto-generated method stub
String sql = "update ordersdetail set unitcost = ? where orderid = ? and productid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setDouble(1, orderDetail.getUnitcost());
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) {
// TODO Auto-generated method stub
String sql = "delete from ordersdetail where orderid = ? and productid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.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,224 @@
package com.lsy.dao.impl;
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 com.lsy.dao.ProductDao;
import com.lsy.model.Product;
public class ProductDaoImp implements ProductDao {
@Override
public List<Product> findAll() {
Product product;
List<Product> list = new ArrayList<>();
String sql = "select * from productlsy ";
try (
//1.创建数据库连接
Connection conn = DBHelper.getConnection();
//2.创建语句对象
PreparedStatement pstm = conn.prepareStatement(sql);
//3.执行查询
ResultSet rs = pstm.executeQuery();) {
// 6.遍历结果集
while (rs.next()) {
product = new Product();
product.setProductid(rs.getString("productid"));
product.setCategory(rs.getString("category"));
product.setCname(rs.getString("cname"));
product.setEname(rs.getString("ename"));
product.setImage(rs.getString("image"));
product.setDescn(rs.getString("descn"));
product.setUnitcost(rs.getDouble("unitcost"));
product.setListprice(rs.getDouble("listprice"));
list.add(product);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public List<Product> findByCategory(String categorylsy) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Product> products = new ArrayList<>();
try {
conn = DBHelper.getConnection();
String sql = "select * from productlsy where category = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, categorylsy);
rs = pstmt.executeQuery();
while(rs.next()) {
Product p = new Product();
p.setProductid(rs.getString("productid"));
p.setCategory(rs.getString("category"));
p.setCname(rs.getString("cname"));
p.setEname(rs.getString("ename"));
p.setImage(rs.getString("image"));
p.setListprice(rs.getDouble("listprice"));
p.setUnitcost(rs.getDouble("unitcost"));
p.setDescn(rs.getString("descn"));
products.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return products;
}
@Override
public Product findById(String productid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Product product = null;
try {
conn = DBHelper.getConnection();
String sql = "select * from productlsy where productid = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, productid);
rs = pstmt.executeQuery();
if (rs.next()) {
product = new Product();
product.setProductid(rs.getString("productid"));
product.setCategory(rs.getString("category"));
product.setCname(rs.getString("cname"));
product.setEname(rs.getString("ename"));
product.setImage(rs.getString("image"));
product.setDescn(rs.getString("descn"));
product.setUnitcost(rs.getDouble("unitcost"));
product.setListprice(rs.getDouble("listprice"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return product;
}
@Override
public int create(Product product) {
String sql = "insert into productlsy values(?,?,?,?,?,?,?,?)";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql);
) {
// 6.遍历结果集
pstm.setString(1, product.getProductid());
pstm.setString(2, product.getCategory());
pstm.setString(3, product.getCname());
pstm.setString(4, product.getEname());
pstm.setString(5, product.getImage());
pstm.setString(6, product.getDescn());
pstm.setDouble(7, product.getUnitcost());
pstm.setDouble(8, product.getListprice());
int a = pstm.executeUpdate();
System.out.printf("成功插入%d数据\n", a);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public int delect(Product product) {
String sql = "DELETE from productlsy where productid = ?";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql)) {
pstm.setString(1, product.getProductid());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
// @Override
// public int modify(Productlsy productlsy) {
// return 0;
// }
public static void main(String[] args) {
ProductDaoImp dao = new ProductDaoImp();
// Productlsy productlsy = new Productlsy();
// Scanner scanner = new Scanner(System.in);
// productlsy.setProductidlsy(scanner.next());
// productlsy.setCategorylsy("admin");
// productlsy.setCnamelsy("admin");
// productlsy.setEnamelsy("admin");
// productlsy.setImagelsy("admin");
// productlsy.setDescnlsy("admin");
// productlsy.setUnitcostlsy(1);
// productlsy.setListpricelsy(1);
// dao.delect(productlsy);
// dao.create(productlsy);
// dao.findAll();
// System.out.println(dao.findById("qww"));
}
}

@ -0,0 +1,217 @@
package jpestore.dao.mysql;
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 jpestore.dao.productDAO;
import jpestore.domain.Product;
public class ProductDaolmp implements productDAO {
@Override
public List<Product> findAll() {
String sql = "select productid,category,cname,ename,image,listprice,unitcost,descn from product";
List<Product>list = new ArrayList<Product>();
try(
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()){
while(rs.next()) {
Product p = new Product();
p.setProductid(rs.getString("productid"));
p.setCategory(rs.getString("category"));
p.setCname(rs.getString("cname"));
p.setEname(rs.getString("Ename"));
p.setImage(rs.getString("Image"));
p.setListprice(rs.getDouble("listprice"));
p.setUnitcost(rs.getDouble("unitcost"));
p.setDescn(rs.getString("descn"));
list.add(p);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return list;
}
public Product findBYID(String productid) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select productid,category,cname,ename,image,listprice,unitcost,descn from product where productid = ?";
try {
// 2.创建数据库连接
conn = DBHelper.getConnection(); //1.加载驱动程序
// 3.创建语句对象
pstmt = conn.prepareStatement(sql);
// 4.绑定参数
pstmt.setString(1, productid);
// 5.执行查询
rs = pstmt.executeQuery();
// 6.遍历结果集
if (rs.next()) {
Product p = new Product();
p.setProductid(rs.getString("productid"));
p.setCategory(rs.getString("category"));
p.setCname(rs.getString("cname"));
p.setEname(rs.getString("ename"));
p.setImage(rs.getString("image"));
p.setListprice(rs.getDouble("listprice"));
p.setUnitcost(rs.getDouble("unitcost"));
p.setDescn(rs.getString("descn"));
return p;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBHelper.close(rs, pstmt, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
public List<Product> finByCategory(String category) {
Connection conn = null;
PreparedStatement pstmt = null ;
ResultSet rs = null;
List<Product>list = new ArrayList<Product>();
String sql = "select productid,category,cname,ename,image,listprice,unitcost,descn from product where category=?";
try {
// 2.创建数据库连接
conn = DBHelper.getConnection(); //1.加载驱动程序
// 3.创建语句对象
pstmt = conn.prepareStatement(sql);
// 4.绑定参数
pstmt.setString(1, category);
// 5.执行查询
rs = pstmt.executeQuery();
// 6.遍历结果集
while (rs.next()) {
Product p = new Product();
p.setProductid(rs.getString("productid"));
p.setCategory(rs.getString("category"));
p.setCname(rs.getString("cname"));
p.setEname(rs.getString("ename"));
p.setImage(rs.getString("image"));
p.setListprice(rs.getDouble("listprice"));
p.setUnitcost(rs.getDouble("unitcost"));
p.setDescn(rs.getString("descn"));
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBHelper.close(rs, pstmt, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
@Override
public int create(Product product) {
// TODO Auto-generated method stub
String sql = "insert into product (productid,category,cname,ename,image,listprice,unitcost,descn) values (?,?,?,?,?,?,?,?)";
try( //2.创建数据库连接
Connection conn = DBHelper.getConnection();
//3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
){
//4.绑定参数
pstmt.setString(1, product.getProductid());
pstmt.setString(2, product.getCategory());
pstmt.setString(3, product.getCname());
pstmt.setString(4, product.getEname());
pstmt.setString(5, product.getImage());
pstmt.setDouble(6, product.getListprice());
pstmt.setDouble(7, product.getUnitcost());
pstmt.setString(8, product.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(Product product) {
// TODO Auto-generated method stub
String sql = "update product set unitcost = ? where productid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt= conn.prepareStatement(sql)
) {
// 4. 绑定参数
pstmt.setDouble(1, product.getUnitcost());
pstmt.setString(2, product.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(Product product) {
// TODO Auto-generated method stub
String sql = "delete from product where productid = ?";
try ( // 2.创建数据库连接
Connection conn = DBHelper.getConnection();
// 3. 创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 4. 绑定参数
pstmt.setString(1, product.getProductid());
// 5. 执行修改C、U、D
int executeUpdate = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", executeUpdate);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}}
Loading…
Cancel
Save