forked from alfa-yohannis/ikono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
107 lines (83 loc) · 3.83 KB
/
Main.java
File metadata and controls
107 lines (83 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main extends Application {
// Database credentials
final String DB_URL = "jdbc:mysql://localhost:3306/productdb";
final String USER = "root"; // Ganti dengan username MySQL Anda
final String PASS = ""; // Ganti dengan password MySQL Anda
@Override
public void start(Stage primaryStage) {
// Membuat Label judul
Label titleLabel = new Label("Product Management System");
// Membuat TextField untuk input data
TextField brandNameField = new TextField();
brandNameField.setPromptText("Enter Brand Name");
TextField productNameField = new TextField();
productNameField.setPromptText("Enter Product Name");
TextField priceField = new TextField();
priceField.setPromptText("Enter Price");
TextField categoryField = new TextField();
categoryField.setPromptText("Enter Category");
TextField featuresField = new TextField();
featuresField.setPromptText("Enter Features Count");
// Membuat Button untuk menambahkan produk
Button addButton = new Button("Add Product");
// Event handler untuk tombol
addButton.setOnAction(event -> {
try {
String brandName = brandNameField.getText();
String productName = productNameField.getText();
double price = Double.parseDouble(priceField.getText());
String category = categoryField.getText();
int features = Integer.parseInt(featuresField.getText());
// Menambahkan produk ke database
addProduct(brandName, productName, price, category, features);
// Reset input field setelah berhasil
brandNameField.clear();
productNameField.clear();
priceField.clear();
categoryField.clear();
featuresField.clear();
} catch (Exception e) {
e.printStackTrace();
}
});
// Membuat layout dengan VBox
VBox layout = new VBox(10, titleLabel, brandNameField, productNameField, priceField, categoryField, featuresField, addButton);
layout.setStyle("-fx-padding: 20; -fx-alignment: center;");
// Membuat Scene dan menampilkan stage
Scene scene = new Scene(layout, 400, 300);
primaryStage.setTitle("Product Management");
primaryStage.setScene(scene);
primaryStage.show();
}
// Metode untuk menambahkan produk ke database
private void addProduct(String brandName, String productName, double price, String category, int features) {
String insertSQL = "INSERT INTO products (brandname, productname, price, category, features) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
pstmt.setString(1, brandName);
pstmt.setString(2, productName);
pstmt.setDouble(3, price);
pstmt.setString(4, category);
pstmt.setInt(5, features);
pstmt.executeUpdate();
System.out.println("Product added successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}