Thêm order mới
Câu lệnh SQL
INSERT INTO USERS(ID, EMAIL, PASSWORD, ROLE) VALUES(null, ?, ?, ?)
Thực thi
public boolean insert(Order order) { String sql = "INSERT INTO ORDERS(ID, CODE, STATUS, USER_ID) VALUES(NULL, ?, ?, ?)"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, "order Code"); stmt.setString(2, "order Status"); stmt.setInt(3, 1); //1 là id của user stmt.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
Cập nhật order
Câu lệnh SQL
UPDATE ORDERS SET code = ?, status = ?, user_id = ?, created_at = ? WHERE id = ?
Thực thi
public boolean update() { String sql = "UPDATE ORDERS SET code = ?, status = ?, user_id = ? WHERE id = ?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, "order Code"); stmt.setString(2, "order Status"); stmt.setInt(3, 1); //1 là id của user stmt.setInt(4, 2); //2 là id của order cần cập nhật return stmt.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
Xoá order
Câu lệnh SQL
DELETE FROM ORDERS WHERE ID = ?
Thực thi
public boolean delete(int id) { // TODO Auto-generated method stub String sql = "DELETE FROM ORDERS WHERE ID = ?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, id); return stmt.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
Tìm kiếm order theo id
Câu lệnh SQL
SELECT * FROM ORDERS WHERE ID = ?
Thực thi
public void find(int id) { String sql = "SELECT * FROM ORDERS WHERE ID = ?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String code = rs.getString("code"); String status = rs.getString("status"); int userId = rs.getInt("user_id"); Timestamp created_at = rs.getTimestamp("created_at"); System.out.format("%d:%s:%s:%d:%s", id, code, status, userId, createdAt).println(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Lấy toàn bộ orders
Câu lệnh SQL
SELECT * FROM ORDERS
Thực thi
public void findAll() { // TODO Auto-generated method stub String sql = "SELECT * FROM ORDERS"; try { PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String code = rs.getString("code"); String status = rs.getString("status"); int userId = rs.getInt("user_id"); Timestamp created_at = rs.getTimestamp("created_at"); System.out.format("%d:%s:%s:%d:%s", id, code, status, userId, createdAt).println(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Tìm kiếm order theo user
Câu lệnh SQL
SELECT * FROM ORDERS WHERE user_id = ?
Thực thi
public void findByUser(int userId) { String sql = "SELECT * FROM ORDERS WHERE user_id = ?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String code = rs.getString("code"); String status = rs.getString("status"); Timestamp createdAt = rs.getTimestamp("created_at"); System.out.format("%d:%s:%s:%d:%s", id, code, status, userId, createdAt).println(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Tìm kiếm order theo status
Câu lệnh SQL
SELECT * FROM ORDERS WHERE STATUS = ?
Thực thi
public void findByStatus(String status) { try { String sql = "SELECT * FROM ORDERS WHERE STATUS = ?"; PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, status); ResultSet rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String code = rs.getString("code"); int userId = rs.getInt("user_id"); Timestamp createdAt = rs.getTimestamp("created_at"); System.out.format("%d:%s:%s:%d:%s", id, code, status, userId, createdAt).println(); } } catch (SQLException ex) { } }
Tìm kiếm order theo code
Câu lệnh SQL
SELECT * FROM ORDERS WHERE CODE = ?
Thực thi
public void findByCode(String code) { String sql = "SELECT * FROM ORDERS WHERE CODE= ?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, code); ResultSet rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String status = rs.getString("status"); int userId = rs.getInt("user_id"); Timestamp createdAt = rs.getTimestamp("created_at"); System.out.format("%d:%s:%s:%d:%s", id, code, status, userId, createdAt).println(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Tính số lượng order theo ngày
Câu lệnh SQL
SELECT COUNT(*) AS count FROM orders where date(created_at)=?
Thực thi
public int countOrderByDay(String date) { int count = 0; String sql = "SELECT COUNT(*) AS count FROM orders where date(created_at)=?"; try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, date); ResultSet rs = stmt.executeQuery(); while (rs.next()) { count = rs.getInt("count"); } } catch (SQLException ex) { Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex); } return count; }