FinalExam - BAD

Run Settings
LanguageJava
Language Version
Run Command
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; import your.package.name.KueDAO; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MainApp extends Application { private Connection connection; private JenisKueDAO jenisKueDAO; private KueDAO kueDAO; private ObservableList<JenisKue> jenisKueList; private ObservableList<Kue> kueList; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { // Membuat koneksi ke database try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/nama_database", "username", "password"); jenisKueDAO = new JenisKueDAO(connection); kueDAO = new KueDAO(connection); } catch (SQLException e) { e.printStackTrace(); } // Menginisialisasi data awal try { jenisKueList = FXCollections.observableArrayList(jenisKueDAO.getAll()); kueList = FXCollections.observableArrayList(kueDAO.getAll()); } catch (SQLException e) { e.printStackTrace(); } // Membuat antarmuka pengguna BorderPane root = new BorderPane(); root.setPadding(new Insets(10)); TableView<JenisKue> jenisKueTableView = new TableView<>(jenisKueList); TableColumn<JenisKue, Integer> jenisKueIdColumn = new TableColumn<>("ID"); jenisKueIdColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty().asObject()); TableColumn<JenisKue, String> jenisKueNamaColumn = new TableColumn<>("Nama"); jenisKueNamaColumn.setCellValueFactory(cellData -> cellData.getValue().namaProperty()); jenisKueTableView.getColumns().addAll(jenisKueIdColumn, jenisKueNamaColumn); TableView<Kue> kueTableView = new TableView<>(kueList); TableColumn<Kue, Integer> kueIdColumn = new TableColumn<>("ID"); kueIdColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty().asObject()); TableColumn<Kue, String> kueNamaColumn = new TableColumn<>("Nama"); kueNamaColumn.setCellValueFactory(cellData -> cellData.getValue().namaProperty()); TableColumn<Kue, JenisKue> kueJenisColumn = new TableColumn<>("Jenis"); kueJenisColumn.setCellValueFactory(cellData -> cellData.getValue().jenisProperty()); TableColumn<Kue, Integer> kueQtyColumn = new TableColumn<>("Qty"); kueQtyColumn.setCellValueFactory(cellData -> cellData.getValue().qtyProperty().asObject()); TableColumn<Kue, Double> kueHargaJualColumn = new TableColumn<>("Harga Jual"); kueHargaJualColumn.setCellValueFactory(cellData -> cellData.getValue().hargaJualProperty().asObject()); kueTableView.getColumns().addAll(kueIdColumn, kueNamaColumn, kueJenisColumn, kueQtyColumn, kueHargaJualColumn); Button refreshButton = new Button("Refresh"); refreshButton.setOnAction(event -> { try { jenisKueList.setAll(jenisKueDAO.getAll()); kueList.setAll(kueDAO.getAll()); } catch (SQLException e) { e.printStackTrace(); } }); VBox vbox = new VBox(10); vbox.setPadding(new Insets(10)); vbox.getChildren().addAll(refreshButton, jenisKueTableView, kueTableView); root.setCenter(vbox); Scene scene = new Scene(root, 800, 600); primaryStage.setScene(scene); primaryStage.setTitle("Aplikasi CRUD"); primaryStage.show(); } @Override public void stop() { // Menutup koneksi database saat aplikasi berhenti try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
import java.sql.*; import java.util.ArrayList; import java.util.List; public class JenisKueDAO { private Connection connection; public JenisKueDAO(Connection connection) { this.connection = connection; } public void insert(JenisKue jenisKue) throws SQLException { String query = "INSERT INTO jenis_kue (nama) VALUES (?)"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setString(1, jenisKue.getNama()); statement.executeUpdate(); } } public void update(JenisKue jenisKue) throws SQLException { String query = "UPDATE jenis_kue SET nama = ? WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setString(1, jenisKue.getNama()); statement.setInt(2, jenisKue.getId()); statement.executeUpdate(); } } public void delete(int id) throws SQLException { String query = "DELETE FROM jenis_kue WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setInt(1, id); statement.executeUpdate(); } } public List<JenisKue> getAll() throws SQLException { List<JenisKue> jenisKueList = new ArrayList<>(); String query = "SELECT * FROM jenis_kue"; try (Statement statement = connection.createStatement()) { ResultSet resultSet = statement.executeQuery(query); while (resultSet.next()) { int id = resultSet.getInt("id"); String nama = resultSet.getString("nama"); JenisKue jenisKue = new JenisKue(id, nama); jenisKueList.add(jenisKue); } } return jenisKueList; } }
public class Kue { private int id; private String nama; private JenisKue jenis; private int qty; private double hargaJual; public Kue(int id, String nama, JenisKue jenis, int qty, double hargaJual) { this.id = id; this.nama = nama; this.jenis = jenis; this.qty = qty; this.hargaJual = hargaJual; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } public JenisKue getJenis() { return jenis; } public void setJenis(JenisKue jenis) { this.jenis = jenis; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public double getHargaJual() { return hargaJual; } public void setHargaJual(double hargaJual) { this.hargaJual = hargaJual; } }
public class JenisKue { private int id; private String nama; public JenisKue(int id, String nama) { this.id = id; this.nama = nama; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } }
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class KueDAO { private Connection connection; // Tambahkan konstruktor public void insert(Kue kue) { // Kode untuk menyimpan data Kue ke database } public void update(Kue kue) { // Kode untuk memperbarui data Kue di database } public void delete(int id) { // Kode untuk menghapus data Kue dari database berdasarkan ID } public List<Kue> getAllKue() { // Kode untuk mendapatkan semua data Kue dari database // dan mengembalikan hasil dalam bentuk List<Kue> } // Tambahkan metode lain sesuai kebutuhan }
Editor Settings
Theme
Key bindings
Full width
Lines