parent
c650dab0ea
commit
717dddbeba
@ -0,0 +1,98 @@
|
|||||||
|
package imp;
|
||||||
|
|
||||||
|
import DAO.dishDAO;
|
||||||
|
import model.dish;
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class dishDAOimp implements dishDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insert(dish dish) {
|
||||||
|
String sql = "INSERT INTO dishes (dishID, dishName, description, price, image) VALUES (?, ?, ?, ?, ?)";
|
||||||
|
try (Connection conn = JDBCutil.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
pstmt.setInt(1, dish.getDishID());
|
||||||
|
pstmt.setString(2, dish.getDishName());
|
||||||
|
pstmt.setString(3, dish.getDescription());
|
||||||
|
pstmt.setInt(4, dish.getPrice());
|
||||||
|
pstmt.setString(5, dish.getImage());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(dish dish) {
|
||||||
|
String sql = "DELETE FROM dishes WHERE dishID = ?";
|
||||||
|
try (Connection conn = JDBCutil.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
pstmt.setInt(1, dish.getDishID());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(dish dish) {
|
||||||
|
String sql = "UPDATE dishes SET dishName = ?, description = ?, price = ?, image = ? WHERE dishID = ?";
|
||||||
|
try (Connection conn = JDBCutil.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
pstmt.setString(1, dish.getDishName());
|
||||||
|
pstmt.setString(2, dish.getDescription());
|
||||||
|
pstmt.setInt(3, dish.getPrice());
|
||||||
|
pstmt.setString(4, dish.getImage());
|
||||||
|
pstmt.setInt(5, dish.getDishID());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public dish findById(int id) {
|
||||||
|
String sql = "SELECT * FROM dishes WHERE dishID = ?";
|
||||||
|
try (Connection conn = JDBCutil.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
pstmt.setInt(1, id);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
dish newDish = new dish();
|
||||||
|
newDish.setDishID(rs.getInt("dishID"));
|
||||||
|
newDish.setDishName(rs.getString("dishName"));
|
||||||
|
newDish.setDescription(rs.getString("description"));
|
||||||
|
newDish.setPrice(rs.getInt("price"));
|
||||||
|
newDish.setImage(rs.getString("image"));
|
||||||
|
return newDish;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<dish> findAll() {
|
||||||
|
String sql = "SELECT * FROM dishes";
|
||||||
|
List<dish> dishes = new ArrayList<>();
|
||||||
|
try (Connection conn = JDBCutil.getConnection();
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql)) {
|
||||||
|
while (rs.next()) {
|
||||||
|
dish newDish = new dish();
|
||||||
|
newDish.setDishID(rs.getInt("dishID"));
|
||||||
|
newDish.setDishName(rs.getString("dishName"));
|
||||||
|
newDish.setDescription(rs.getString("description"));
|
||||||
|
newDish.setPrice(rs.getInt("price"));
|
||||||
|
newDish.setImage(rs.getString("image"));
|
||||||
|
dishes.add(newDish);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return dishes;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue