Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
9831e58ddf | 1 year ago |
|
cd29f046db | 1 year ago |
|
fa97feaf22 | 1 year ago |
|
c64051d889 | 1 year ago |
|
4bafbc57c1 | 1 year ago |
@ -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,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"));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue