0% found this document useful (0 votes)
7 views31 pages

Listing Program

The document outlines a Java application for managing student data, including a database connection class (DB), a login interface (LoginFrame), user registration (SeedUser), user management (UserDAO), a main data entry panel (PendataanFrame), and a student data management panel (DataSiswaPanel). It provides methods for connecting to a MySQL database, handling user login and registration, and displaying user interfaces for data entry. The application is structured into multiple classes, each serving a specific purpose in the overall functionality.

Uploaded by

shindeiru30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views31 pages

Listing Program

The document outlines a Java application for managing student data, including a database connection class (DB), a login interface (LoginFrame), user registration (SeedUser), user management (UserDAO), a main data entry panel (PendataanFrame), and a student data management panel (DataSiswaPanel). It provides methods for connecting to a MySQL database, handling user login and registration, and displaying user interfaces for data entry. The application is structured into multiple classes, each serving a specific purpose in the overall functionality.

Uploaded by

shindeiru30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LISTING PROGRAM

A. Koneksi

1. package kkp;
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. public class DB {
8. private static final String DEFAULT_HOST = "[Link]";
9. private static final String DEFAULT_PORT = "3306";
10. private static final String DEFAULT_NAME = "kkp";
11. private static final String DEFAULT_USER = "root";
12. private static final String DEFAULT_PASS = "";
13. private static final String DEFAULT_PARAMS =
14. "useUnicode=true&characterEncoding=utf8" +
15. "&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
";
16. private static volatile Properties cachedProps = null;
17. public static Connection getConnection() throws Exception {
18. [Link]("[Link]");
19. Properties p = loadConfig();
20. String host = get(p, "[Link]", DEFAULT_HOST);
21. String port = get(p, "[Link]", DEFAULT_PORT);
22. String name = get(p, "[Link]", DEFAULT_NAME);
23. String user = get(p, "[Link]", DEFAULT_USER);
24. String pass = get(p, "[Link]", DEFAULT_PASS);
25. String params = get(p, "[Link]", DEFAULT_PARAMS);
26. String url = "jdbc:mysql://" + host + ":" + port + "/" + name + "?" + params;
27. return [Link](url, user, pass);
28. }
29. private static Properties loadConfig() {
30. if (cachedProps != null) return cachedProps;
31. synchronized ([Link]) {
32. if (cachedProps != null) return cachedProps;
33. Properties p = new Properties();
34. File f = new File("[Link]");
35. if ([Link]() && [Link]()) {
36. try (FileInputStream fis = new FileInputStream(f)) {
37. [Link](fis);
38. } catch (Exception e) {
39. [Link]("Gagal baca [Link], pakai default.
Detail: " + [Link]());
40. }
41. } else {
42. [Link]("[Link] tidak ditemukan, pakai
default.");
43. }
44. cachedProps = p;
45. return p;
46. }
47. }
48. private static String get(Properties p, String key, String def) {
49. String v = [Link](key);
50. if (v == null) return def;
51. v = [Link]();
52. return [Link]() ? def : v;
53. }
54. }

B. Halaman Login

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link].*;
5. import [Link];
6. import [Link];
7. public class LoginFrame extends JFrame {
8. private JTextField tfUser;
9. private JPasswordField pfPass;
10. private JLabel lblMsg;
11. public LoginFrame() {
12. setTitle("Login - KKP");
13. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14. setSize(1280, 720);
15. setLocationRelativeTo(null);
16. BackgroundPanel bg = new BackgroundPanel("/kkp/assets/image/[Link]");
17. [Link](null);
18. setContentPane(bg);
19. RoundedPanel card = new RoundedPanel();
20. [Link](120, 170, 520, 360);
21. [Link](card);
22. JLabel title = new JLabel("<html><div style='text-align:center;'>"
23. + "APLIKASI<br/>PENGELOLA DATA<br/>SISWA BERPRESTASI"
24. + "</div></html>", [Link]);
25. [Link](new Color(160, 0, 0));
26. [Link](new Font("SansSerif", [Link], 26));
27. [Link](30, 25, 460, 110);
28. [Link](title);
29. JLabel lUser = label("Username", 95, 155);
30. [Link](lUser);
31. tfUser = roundedField();
32. [Link](95, 178, 330, 44);
33. [Link](tfUser);
34. JLabel lPass = label("Password", 95, 232);
35. [Link](lPass);
36. pfPass = roundedPassword();
37. [Link](95, 255, 330, 44);
38. [Link](pfPass);
39. lblMsg = new JLabel(" ", [Link]);
40. [Link](new Color(180, 20, 20));
41. [Link](60, 310, 400, 20);
42. [Link](lblMsg);
43. RoundedButton btn = new RoundedButton("Login");
44. [Link](170, 560, 420, 52);
45. [Link](e -> doLogin(btn));
46. [Link](btn);
47. JLabel link = new JLabel("Contact Admin For Register", [Link]);
48. [Link](170, 615, 420, 22);
49. [Link](new Color(160, 0, 0));
50. [Link](new Font("SansSerif", [Link], 12));
51. [Link](link);
52. }
53. private JLabel label(String text, int x, int y) {
54. JLabel l = new JLabel(text);
55. [Link](new Font("SansSerif", [Link], 13));
56. [Link](new Color(60, 60, 60));
57. [Link](x, y, 200, 18);
58. return l;
59. }
60. private JTextField roundedField() {
61. JTextField f = new JTextField();
62. [Link](new Font("SansSerif", [Link], 14));
63. [Link](new EmptyBorder(10, 16, 10, 16));
64. [Link](false);
65. return f;
66. }
67. private JPasswordField roundedPassword() {
68. JPasswordField f = new JPasswordField();
69. [Link](new Font("SansSerif", [Link], 14));
70. [Link](new EmptyBorder(10, 16, 10, 16));
71. [Link](false);
72. return f;
73. }
74. private void doLogin(RoundedButton btn) {
75. [Link](" ");
76. String user = [Link]().trim();
77. char[] pass = [Link]();
78. if ([Link]() || [Link] == 0) {
79. [Link]("Username dan password wajib diisi.");
80. return;
81. }
82. [Link](false);
83. [Link]("Checking...");
84. SwingWorker<Boolean, Void> w;
85. w = new SwingWorker<Boolean, Void>() {
86. @Override protected Boolean doInBackground() throws Exception {
87. return [Link](user, pass);
88. }
89. @Override protected void done() {
90. try {
91. boolean ok = get();
92. if (ok) {
93. dispose();
94. [Link](() -> new PendataanFrame(user).setVisible(true));
95. } else {
96. [Link]("Username atau password salah.");
97. }
98. } catch (InterruptedException | ExecutionException ex) {
99. [Link]("Error DB: " + [Link]());
100. } finally {
101. [Link](true);
102. [Link]("Login");
103. [Link](pass, '\0');
104. }
105. }
106. }; [Link]();
107. }
108. static class BackgroundPanel extends JPanel {
109. private final Image img;
110. BackgroundPanel(String resourcePath) {
111. ImageIcon icon = new ImageIcon(getClass().getResource(resourcePath));
112. img = [Link]();
113. }
114. @Override protected void paintComponent(Graphics g) {
115. [Link](g);
116. Graphics2D g2 = (Graphics2D) [Link]();
117. [Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
118. [Link](img, 0, 0, getWidth(), getHeight(), this);
119. GradientPaint gp = new GradientPaint(
120. 0, 0, new Color(0, 0, 0, 220),
121. getWidth(), 0, new Color(0, 0, 0, 80)
122. );
123. [Link](gp);
124. [Link](0, 0, getWidth(), getHeight());
125. [Link]();
126. }
127. }
128. static class RoundedPanel extends JPanel {
129. RoundedPanel() { setOpaque(false); setLayout(null); }
130. @Override protected void paintComponent(Graphics g) {
131. Graphics2D g2 = (Graphics2D) [Link]();
132. [Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
133. int r = 26;
134. [Link](new Color(0,0,0,60));
135. [Link](6, 6, getWidth()-12, getHeight()-12, r, r);
136. [Link]([Link]);
137. [Link](0, 0, getWidth()-12, getHeight()-12, r, r);
138. [Link](new Color(235,235,235));
139. [Link](95, 178, 330, 44, 22, 22);
140. [Link](95, 255, 330, 44, 22, 22);
141. [Link]();
142. [Link](g);
143. }
144. }
145. static class RoundedButton extends JButton {
146. RoundedButton(String text) {
147. super(text);
148. setForeground([Link]);
149. setBackground(new Color(160, 0, 0));
150. setFont(new Font("SansSerif", [Link], 18));
151. setFocusPainted(false);
152. setBorderPainted(false);
153. setContentAreaFilled(false);
154. setOpaque(false);
155. setCursor([Link](Cursor.HAND_CURSOR));
156. }
157. @Override protected void paintComponent(Graphics g) {
158. Graphics2D g2 = (Graphics2D) [Link]();
159. [Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
160. [Link](getBackground());
161. [Link](0, 0, getWidth(), getHeight(), 24, 24);
162. [Link]();
163. [Link](g);
164. }
165. }
166. }

C. Registrasi User

1. package kkp;
2. public class SeedUser {
3. public static void main(String[] args) {
4. try {
5. [Link]("user", "12345".toCharArray());
6. [Link]("User admin dibuat.");
7. } catch (Exception e) {
8. [Link]();
9. }
10. }
11. }

D. Kelola Database User

1. package kkp;
2. package kkp;
3. import [Link];
4. import [Link];
5. import [Link];
6. public class UserDAO {
7. public static boolean login(String username, char[] password) throws Exception {
8. String sql = "SELECT password_hash FROM users WHERE username = ? LIMIT 1";
9. try (Connection c = [Link]();
10. PreparedStatement ps = [Link](sql)) {
11. [Link](1, username);
12. try (ResultSet rs = [Link]()) {
13. if (![Link]()) return false;
14. String stored = [Link]("password_hash");
15. return [Link](password, stored);
16. }
17. }
18. }
19. public static void createUser(String username, char[] password) throws Exception {
20. String sql = "INSERT INTO users(username, password_hash) VALUES(?, ?)";
21. String hash = [Link](password);
22. try (Connection c = [Link]();
23. PreparedStatement ps = [Link](sql)) {
24. [Link](1, username);
25. [Link](2, hash);
26. [Link]();
27. }
28. }
29. }

E. Panel Utama

1. package kkp;
2. import [Link].*;
3. import [Link].*;
4. public class PendataanFrame extends JFrame {
5. public PendataanFrame(String username) {
6. setTitle("Sistem Pendataan Siswa Berprestasi - Login: " + username);
7. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8. setSize(1366, 768);
9. setLocationRelativeTo(null);
10. JTabbedPane tabs = new JTabbedPane();
11. [Link]("Data Siswa", new DataSiswaPanel());
12. [Link]("Data Prestasi", new DataPrestasiPanel());
13. [Link]("Periode", new PeriodePanel());
14. JPanel header = new JPanel(new BorderLayout());
15. [Link]([Link]);
16. [Link]([Link]);
17. [Link]([Link]);
18. [Link](tabs, [Link]);
19. JPanel right = new JPanel(new FlowLayout([Link], 10, 8));
20. [Link](false);
21. JLabel lblUser = new JLabel("Login: " + username);
22. [Link]([Link]);
23. JButton btnLogout = new JButton("Logout");
24. [Link](e -> {
25. dispose();
26. new LoginFrame().setVisible(true);
27. });
28. [Link](lblUser);
29. [Link](btnLogout);
30. [Link](right, [Link]);
31. getContentPane().setLayout(new BorderLayout());
32. getContentPane().add(header, [Link]);
33. getContentPane().add(tabs, [Link]);
34. }
35. }

F. Menu Pendataan Siswa

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. import [Link].*;
6. import [Link];
7. public class DataSiswaPanel extends JPanel {
8. private DefaultTableModel model;
9. private JTable table;
10. private JTextField tfNis = new JTextField();
11. private JTextField tfNama = new JTextField();
12. private JTextField tfKelas = new JTextField();
13. private JTextField tfJurusan = new JTextField();
14. private JTextField tfCari = new JTextField(18);
15. private JLabel lblTotal = new JLabel("Total siswa: 0");
16. private Long selectedId = null;
17. public DataSiswaPanel() {
18. setLayout(new BorderLayout());
19. setBackground([Link]);
20. JPanel strip = new JPanel(new BorderLayout());
21. [Link]([Link]);
22. [Link](new EmptyBorder(10, 12, 10, 12));
23. JLabel title = new JLabel("Data Siswa");
24. [Link](UITheme.TITLE_PAGE);
25. [Link](title, [Link]);
26. [Link](new Font("SansSerif", [Link], 12));
27. [Link](lblTotal, [Link]);
28. add(strip, [Link]);
29. JPanel body = new JPanel(new BorderLayout());
30. [Link]([Link]);
31. add(body, [Link]);
32. JPanel header = new JPanel(new BorderLayout());
33. [Link](UITheme.BIRU_TUA);
34. [Link](new EmptyBorder(18, 18, 18, 18));
35. JLabel logo = [Link](130, 130);
36. [Link](logo, [Link]);
37. JLabel big = new JLabel("<html><div style='text-align:center;'>"
38. + "Sistem Pendataan Siswa Berprestasi<br/>"
39. + "SMK Karya Muda Depok"
40. + "</div></html>", [Link]);
41. [Link](UITheme.TITLE_BIG);
42. [Link]([Link]);
43. [Link](big, [Link]);
44. [Link](header, [Link]);
45. JPanel main = new JPanel(new BorderLayout());
46. [Link]([Link]);
47. [Link](new EmptyBorder(0, 0, 0, 0));
48. [Link](main, [Link]);
49. JPanel topBar = new JPanel(new BorderLayout());
50. [Link](false);
51. [Link](new EmptyBorder(10, 18, 6, 18));
52. JPanel btns = new JPanel(new FlowLayout([Link], 8, 0));
53. [Link](false);
54. JButton btnTambah = new JButton("Tambah Baru");
55. JButton btnHapus = new JButton("Hapus");
56. JButton btnRefresh = new JButton("Refresh");
57. JButton btnResetForm = new JButton("Reset Form");
58. [Link](false);
59. [Link](new Color(19, 160, 70));
60. [Link]([Link]);
61. [Link](new Color(255, 120, 0));
62. [Link]([Link]);
63. [Link](btnTambah);
64. [Link](btnHapus);
65. [Link](btnRefresh);
66. [Link](btnResetForm);
67. JPanel search = new JPanel(new FlowLayout([Link], 8, 0));
68. [Link](false);
69. [Link](new JLabel("Cari:"));
70. [Link](tfCari);
71. JButton btnSearch = new JButton("Search");
72. [Link](btnSearch);
73. [Link](btns, [Link]);
74. [Link](search, [Link]);
75. model = new DefaultTableModel(new Object[]{"ID", "NIS", "Nama", "Kelas",
"Jurusan"}, 0) {
76. public boolean isCellEditable(int r, int c) { return false; }
77. };
78. table = new JTable(model);
79. [Link](26);
80. [Link]().getColumn(0).setMinWidth(0);
81. [Link]().getColumn(0).setMaxWidth(0);
82. JScrollPane sp = new JScrollPane(table);
83. [Link](new Dimension(100, 420)); // bikin tabel naik (tanpa ruang kosong)
84. JPanel spWrap = new JPanel(new BorderLayout());
85. [Link](false);
86. [Link](new EmptyBorder(0, 18, 18, 18));
87. [Link](sp, [Link]);
88. JPanel left = new JPanel(new BorderLayout());
89. [Link](false);
90. [Link](topBar, [Link]);
91. [Link](spWrap, [Link]);
92. JPanel right = new JPanel(new BorderLayout());
93. [Link](new Dimension(360, 0));
94. [Link]([Link]);
95. [Link](new EmptyBorder(20, 18, 18, 18));
96. JLabel rightTitle = new JLabel("<html><div style='text-align:center;'>Input Data
Siswa</div></html>", [Link]);
97. [Link](new Font("SansSerif", [Link], 26));
98. [Link](rightTitle, [Link]);
99. JPanel formCard = new JPanel(new GridBagLayout());
100. [Link]([Link]);
101. [Link](new EmptyBorder(14, 14, 14, 14));
102. GridBagConstraints c = new GridBagConstraints();
103. [Link] = [Link];
104. [Link] = new Insets(8, 0, 8, 0);
105. [Link] = 0; [Link] = 0;
106. [Link](new JLabel("Form Siswa"), c); [Link]++;
107. [Link](new JLabel("NIS"), c); [Link]++;
108. [Link](tfNis, c); [Link]++;
109. [Link](new JLabel("Nama"), c); [Link]++;
110. [Link](tfNama, c); [Link]++;
111. [Link](new JLabel("Kelas"), c); [Link]++;
112. [Link](tfKelas, c); [Link]++;
113. [Link](new JLabel("Jurusan"), c); [Link]++;
114. [Link](tfJurusan, c); [Link]++;
115. JPanel formBtn = new JPanel(new FlowLayout([Link], 10, 0));
116. [Link](false);
117. JButton btnSimpan = new JButton("Simpan");
118. [Link](new Color(19, 160, 70));
119. [Link]([Link]);
120. JButton btnReset = new JButton("Reset");
121. [Link](btnSimpan);
122. [Link](btnReset);
123. [Link] = new Insets(16, 0, 0, 0);
124. [Link](formBtn, c);
125. JScrollPane spForm = new JScrollPane(formCard,
126. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
127. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
128. [Link](null);
129. [Link]().setBackground([Link]);
130. [Link]().setUnitIncrement(16);
131. [Link](spForm, [Link]);
132. JPanel content = new JPanel(new BorderLayout(14, 0));
133. [Link](false);
134. [Link](left, [Link]);
135. [Link](right, [Link]);
136. [Link](content, [Link]);
137. Runnable reload = () -> load("");
138. [Link](e -> load(""));
139. [Link](e -> load([Link]()));
140. [Link](e -> load([Link]()));
141. [Link](e -> resetForm(btnHapus));
142. [Link](e -> resetForm(btnHapus));
143. [Link](e -> resetForm(btnHapus));
144. [Link](e -> save(btnHapus));
145. [Link](e -> deleteSelected(btnHapus));
146. [Link]().addListSelectionListener(e -> {
147. if ([Link]()) return;
148. int r = [Link]();
149. if (r >= 0) {
150. selectedId = (Long) [Link](r, 0);
151. [Link]([Link]([Link](r, 1)));
152. [Link]([Link]([Link](r, 2)));
153. [Link]([Link]([Link](r, 3)));
154. [Link]([Link]([Link](r, 4)));
155. [Link](true);
156. }
157. });
158. [Link]();
159. }
160. private void load(String keyword) {
161. try {
162. [Link](0);
163. List<[Link]> list = [Link](keyword);
164. for ([Link] s : list) {
165. [Link](new Object[]{[Link], [Link], [Link], [Link], [Link]});
166. }
167. [Link]("Total siswa: " + [Link]());
168. } catch (Exception e) {
169. [Link]();
170. [Link](this, [Link](), "Error",
JOptionPane.ERROR_MESSAGE);
171. }
172. }
173. private void save(JButton btnHapus) {
174. String nis = [Link]().trim();
175. String nama = [Link]().trim();
176. String kelas = [Link]().trim();
177. String jurusan = [Link]().trim();
178. if ([Link]() || [Link]() || [Link]() || [Link]()) {
179. [Link](this, "Semua field wajib diisi.", "Validasi",
JOptionPane.WARNING_MESSAGE);
180. return;
181. } try {
182. if (selectedId == null) [Link](nis, nama, kelas, jurusan);
183. else [Link](selectedId, nis, nama, kelas, jurusan);
184. resetForm(btnHapus);
185. load([Link]());
186. } catch (Exception e) {
187. [Link]();
188. [Link](this, [Link](), "Error",
JOptionPane.ERROR_MESSAGE);
189. }
190. }
191. private void deleteSelected(JButton btnHapus) {
192. if (selectedId == null) return;
193. int ok = [Link](this, "Yakin hapus siswa ini?", "Konfirmasi",
JOptionPane.YES_NO_OPTION);
194. if (ok != JOptionPane.YES_OPTION) return;
195. try {
196. [Link](selectedId);
197. resetForm(btnHapus);
198. load([Link]());
199. } catch (Exception e) {
200. [Link]();
201. [Link](this, [Link](), "Error",
JOptionPane.ERROR_MESSAGE);
202. }
203. }
204. private void resetForm(JButton btnHapus) {
205. selectedId = null;
206. [Link]("");
207. [Link]("");
208. [Link]("");
209. [Link]("");
210. [Link]();
211. [Link](false);
212. }
213.}
G. Kelola Database Siswa

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. public class SiswaDAO {
6. public static class SiswaRow {
7. public long id;
8. public String nis;
9. public String nama;
10. public String kelas;
11. public String jurusan;
12. public SiswaRow(long id, String nis, String nama, String kelas, String jurusan) {
13. [Link] = id;
14. [Link] = nis;
15. [Link] = nama;
16. [Link] = kelas;
17. [Link] = jurusan;
18. }
19. }
20. public static List<SiswaRow> findAll(String keyword) throws Exception {
21. String base = "SELECT id, nis, nama, kelas, jurusan FROM siswa";
22. String where = "";
23. boolean hasKw = keyword != null && ![Link]().isEmpty();
24. if (hasKw) {
25. where = " WHERE nis LIKE ? OR nama LIKE ? OR kelas LIKE ? OR jurusan
LIKE ?";
26. }
27. String order = " ORDER BY id DESC";
28. try (Connection c = [Link]();
29. PreparedStatement ps = [Link](base + where + order)) {
30. if (hasKw) {
31. String kw = "%" + [Link]() + "%";
32. [Link](1, kw);
33. [Link](2, kw);
34. [Link](3, kw);
35. [Link](4, kw);
36. }
37. List<SiswaRow> list = new ArrayList<>();
38. try (ResultSet rs = [Link]()) {
39. while ([Link]()) {
40. [Link](new SiswaRow(
41. [Link]("id"),
42. [Link]("nis"),
43. [Link]("nama"),
44. [Link]("kelas"),
45. [Link]("jurusan")
46. ));
47. }
48. }
49. return list;
50. }
51. }
52. public static void insert(String nis, String nama, String kelas, String jurusan) throws
Exception {
53. String sql = "INSERT INTO siswa(nis, nama, kelas, jurusan) VALUES(?,?,?,?)";
54. try (Connection c = [Link]();
55. PreparedStatement ps = [Link](sql)) {
56. [Link](1, nis);
57. [Link](2, nama);
58. [Link](3, kelas);
59. [Link](4, jurusan);
60. [Link]();
61. }
62. }
63. public static void update(long id, String nis, String nama, String kelas, String jurusan)
throws Exception {
64. String sql = "UPDATE siswa SET nis=?, nama=?, kelas=?, jurusan=? WHERE id=?";
65. try (Connection c = [Link]();
66. PreparedStatement ps = [Link](sql)) {
67. [Link](1, nis);
68. [Link](2, nama);
69. [Link](3, kelas);
70. [Link](4, jurusan);
71. [Link](5, id);
72. [Link]();
73. }
74. }
75. public static void delete(long id) throws Exception {
76. String sql = "DELETE FROM siswa WHERE id=?";
77. try (Connection c = [Link]();
78. PreparedStatement ps = [Link](sql)) {
79. [Link](1, id);
80. [Link]();
81. }
82. }
83. }

H. Menu Pencarian Data Siswa

1. package kkp;
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. public class SiswaRefDAO {
8. public static class SiswaRef {
9. public long id;
10. public String nis;
11. public String nama;
12. public SiswaRef(long id, String nis, String nama) {
13. [Link] = id;
14. [Link] = nis;
15. [Link] = nama;
16. }
17. @Override
18. public String toString() {
19. return nis + " - " + nama;
20. }
21. }
22. public static List<SiswaRef> search(String keyword) throws Exception {
23. String kw = (keyword == null) ? "" : [Link]();
24. String sql =
25. "SELECT id, nis, nama " +
26. "FROM siswa " +
27. "WHERE nis LIKE ? OR nama LIKE ? " +
28. "ORDER BY nama ASC " +
29. "LIMIT 100";
30. try ([Link] c = [Link]();
31. [Link] ps = [Link](sql)) {
32. String like = "%" + kw + "%";
33. [Link](1, like);
34. [Link](2, like);
35. List<SiswaRef> list = new [Link]<>();
36. try ([Link] rs = [Link]()) {
37. while ([Link]()) {
38. [Link](new SiswaRef(
39. [Link]("id"),
40. [Link]("nis"),
41. [Link]("nama")
42. ));
43. }
44. }
45. return list;
46. }
47. }
48. public static List<SiswaRef> getAll() throws Exception {
49. String sql = "SELECT id, nis, nama FROM siswa ORDER BY nama ASC";
50. try (Connection c = [Link]();
51. PreparedStatement ps = [Link](sql);
52. ResultSet rs = [Link]()) {
53. List<SiswaRef> list = new ArrayList<>();
54. while ([Link]()) {
55. [Link](new SiswaRef(
56. [Link]("id"),
57. [Link]("nis"),
58. [Link]("nama")
59. ));
60. }
61. return list;
62. }
63. }
64. }

I. Menu Pendataan Prestasi Siswa

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. import [Link].*;
6. import [Link];
7. import [Link];
8. import [Link];
9. public class DataPrestasiPanel extends JPanel {
10. private static final Color ORANGE = new Color(245, 160, 70);
11. private static final Color BLUE_DARK = new Color(35, 63, 110);
12. private static final Color BLUE_LIGHT = new Color(210, 225, 245);
13. private static final Color WHITE = [Link];
14. private final DefaultTableModel model;
15. private final JTable table;
16. private final JComboBox<[Link]> cbSiswa = new JComboBox<>();
17. private final JComboBox<[Link]> cbPeriode = new JComboBox<>();
18. private final JTextField tfLomba = new JTextField();
19. private final JComboBox<String> cbTingkat = new JComboBox<>(new String[]{
20. "Sekolah","Kecamatan","Kabupaten/Kota","Provinsi","Nasional","Internasional"
21. });
22. private final JTextField tfPeringkat = new JTextField();
23. private final JComboBox<String> cbKategori = new JComboBox<>(new String[]
{"Akademik","Non-Akademik"});
24. private final JTextField tfTanggal = new JTextField(); // YYYY-MM-DD
25. private final JTextField tfKet = new JTextField();
26. private final JTextField tfCari = new JTextField(18);
27. private final JTextField tfCariSiswa = new JTextField();
28. private final JButton btnCariSiswa = new JButton("Cari");
29. private Long selectedId = null;
30. private JButton btnHapus;
31. private JLabel lblStatus;
32. public DataPrestasiPanel() {
33. setLayout(new BorderLayout());
34. setBackground(WHITE);
35. JPanel strip = new JPanel(new BorderLayout());
36. [Link](ORANGE);
37. [Link](new EmptyBorder(10, 12, 10, 12));
38. JLabel title = new JLabel("Data Prestasi");
39. [Link](new Font("SansSerif", [Link], 22));
40. [Link](title, [Link]);
41. lblStatus = new JLabel(" ");
42. [Link](new Font("SansSerif", [Link], 12));
43. [Link](lblStatus, [Link]);
44. add(strip, [Link]);
45. JPanel body = new JPanel(new BorderLayout());
46. [Link](WHITE);
47. add(body, [Link]);
48. JPanel header = new JPanel(new BorderLayout());
49. [Link](UITheme.BIRU_TUA);
50. [Link](new EmptyBorder(18, 18, 18, 18));
51. JLabel logo = [Link](130, 130);
52. [Link](logo, [Link]);
53. JLabel big = new JLabel(
54. "<html><div style='text-align:center;'>"
55. + "Sistem Pendataan Siswa Berprestasi<br/>"
56. + "SMK Karya Muda Depok"
57. + "</div></html>",
58. [Link]
59. );
60. [Link](UITheme.TITLE_BIG);
61. [Link]([Link]);
62. [Link](big, [Link]);
63. [Link](header, [Link]);
64. JPanel main = new JPanel(new BorderLayout());
65. [Link](BLUE_LIGHT);
66. [Link](main, [Link]);
67. JPanel topBar = new JPanel(new BorderLayout());
68. [Link](false);
69. [Link](new EmptyBorder(10, 18, 6, 18));
70. JPanel leftBar = new JPanel(new FlowLayout([Link], 8, 0));
71. [Link](false);
72. JButton btnTambah = new JButton("Tambah Baru");
73. btnHapus = new JButton("Hapus");
74. JButton btnRefresh = new JButton("Refresh");
75. JButton btnReloadSiswa = new JButton("Reload Siswa");
76. JButton btnReloadPeriode = new JButton("Reload Periode");
77. [Link](new Color(19, 160, 70));
78. [Link]([Link]);
79. [Link](new Color(255, 120, 0));
80. [Link]([Link]);
81. [Link](new Color(255, 120, 0));
82. [Link]([Link]);
83. [Link](false);
84. [Link](btnTambah);
85. [Link](btnHapus);
86. [Link](btnRefresh);
87. [Link](btnReloadSiswa);
88. [Link](btnReloadPeriode);
89. JPanel rightBar = new JPanel(new FlowLayout([Link], 8, 0));
90. [Link](false);
91. [Link](new JLabel("Cari:"));
92. [Link](tfCari);
93. JButton btnSearch = new JButton("Search");
94. [Link](btnSearch);
95. [Link](leftBar, [Link]);
96. [Link](rightBar, [Link]);
97. model = new DefaultTableModel(new Object[]{
98. "ID","SISWA_ID","PERIODE_ID","NIS","Nama","Periode","Lomba","Tingkat","P
eringkat","Kategori","Tanggal","Keterangan"
99. }, 0) {
100. public boolean isCellEditable(int r, int c) { return false; }
101. };
102. table = new JTable(model);
103. [Link](26);
104. hideCol(0);
105. hideCol(1);
106. hideCol(2);
107. JScrollPane sp = new JScrollPane(table);
108. [Link](new Dimension(100, 420));
109. JPanel spWrap = new JPanel(new BorderLayout());
110. [Link](false);
111. [Link](new EmptyBorder(0, 18, 18, 18));
112. [Link](sp, [Link]);
113. JPanel leftArea = new JPanel(new BorderLayout());
114. [Link](false);
115. [Link](topBar, [Link]);
116. [Link](spWrap, [Link]);
117. JPanel right = new JPanel(new BorderLayout());
118. [Link](new Dimension(380, 0));
119. [Link](ORANGE);
120. [Link](new EmptyBorder(20, 18, 18, 18));
121. JLabel rightTitle = new JLabel("<html><div style='text-align:center;'>Input Data
Prestasi</div></html>", [Link]);
122. [Link](new Font("SansSerif", [Link], 26));
123. [Link](rightTitle, [Link]);
124. JPanel formCard = new JPanel(new GridBagLayout());
125. [Link](WHITE);
126. [Link](new EmptyBorder(14, 14, 14, 14));
127. GridBagConstraints c = new GridBagConstraints();
128. [Link] = [Link];
129. [Link] = new Insets(8, 0, 8, 0);
130. [Link] = 0; [Link] = 0;
131. [Link](new JLabel("Form Prestasi"), c); [Link]++;
132. [Link](new JLabel("Cari Siswa (NIS/Nama)"), c); [Link]++;
133. JPanel rowCari = new JPanel(new BorderLayout(8, 0));
134. [Link]([Link]);
135. [Link](tfCariSiswa, [Link]);
136. [Link](btnCariSiswa, [Link]);
137. [Link](rowCari, c); [Link]++;
138. [Link](new JLabel("Siswa"), c); [Link]++;
139. [Link](cbSiswa, c); [Link]++;
140. [Link](new JLabel("Periode (Aktif)"), c); [Link]++;
141. [Link](false);
142. [Link](cbPeriode, c); [Link]++;
143. [Link](new JLabel("Jenis Lomba"), c); [Link]++;
144. [Link](tfLomba, c); [Link]++;
145. [Link](new JLabel("Tingkat"), c); [Link]++;
146. [Link](cbTingkat, c); [Link]++;
147. [Link](new JLabel("Peringkat"), c); [Link]++;
148. [Link](tfPeringkat, c); [Link]++;
149. [Link](new JLabel("Kategori"), c); [Link]++;
150. [Link](cbKategori, c); [Link]++;
151. [Link](new JLabel("Tanggal (YYYY-MM-DD) opsional"), c); [Link]++;
152. [Link](tfTanggal, c); [Link]++;
153. [Link](new JLabel("Keterangan (opsional)"), c); [Link]++;
154. [Link](tfKet, c); [Link]++;
155. JPanel btnForm = new JPanel(new FlowLayout([Link], 10, 0));
156. [Link](false);
157. JButton btnSimpan = new JButton("Simpan");
158. [Link](new Color(19, 160, 70));
159. [Link]([Link]);
160. JButton btnReset = new JButton("Reset");
161. [Link](btnSimpan);
162. [Link](btnReset);
163. [Link] = new Insets(16, 0, 0, 0);
164. [Link](btnForm, c);
165. JScrollPane spForm = new JScrollPane(formCard,
166. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
167. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
168. [Link](null);
169. [Link]().setBackground([Link]);
170. [Link]().setUnitIncrement(16);
171. [Link](spForm, [Link]);
172. JPanel center = new JPanel(new BorderLayout(14, 0));
173. [Link](false);
174. [Link](leftArea, [Link]);
175. [Link](right, [Link]);
176. [Link](center, [Link]);
177. [Link](e -> load(""));
178. [Link](e -> load([Link]()));
179. [Link](e -> load([Link]()));
180. [Link](e ->
loadSiswaComboByKeyword([Link]()));
181. [Link](e ->
loadSiswaComboByKeyword([Link]()));
182. [Link](e -> [Link](""));
183. [Link](e -> loadPeriodeCombo());
184. [Link](e -> resetForm());
185. [Link](e -> resetForm());
186. [Link](e -> save());
187. [Link](e -> deleteSelected());
188. [Link]().addListSelectionListener(e -> {
189. if ([Link]()) return;
190. int r = [Link]();
191. if (r >= 0) {
192. selectedId = (Long) [Link](r, 0);
193. long siswaId = (Long) [Link](r, 1);
194. long periodeId = (Long) [Link](r, 2);
195. selectSiswaById(siswaId);
196. selectPeriodeById(periodeId);
197. [Link]([Link]([Link](r, 6)));
198. [Link]([Link]([Link](r, 7)));
199. [Link]([Link]([Link](r, 8)));
200. [Link]([Link]([Link](r, 9)));
201. Object tgl = [Link](r, 10);
202. [Link](tgl == null ? "" : [Link](tgl));
203. Object ket = [Link](r, 11);
204. [Link](ket == null ? "" : [Link](ket));
205. [Link](true);
206. }
207. });
208. loadSiswaCombo();
209. loadPeriodeCombo();
210. load("");
211. }
212. private void loadSiswaCombo() {
213. loadSiswaComboByKeyword("");
214.}
215. private void loadSiswaComboByKeyword(String keyword) {
216. try {
217. [Link]();
218. List<[Link]> list;
219. if (keyword != null && ![Link]().isEmpty()) {
220. list = [Link](keyword); // QUERY DB
221. } else {
222. list = [Link](); // yang lama
223. }
224. for ([Link] s : list) [Link](s);
225. if ([Link]() == 0) {
226. [Link]("Siswa tidak ditemukan untuk: " + keyword);
227. }
228. } catch (Exception e) {
229. showErr("Gagal load siswa: " + [Link](), e);
230. }
231.}
232. private void loadPeriodeCombo() {
233. try {
234. [Link]();
235. List<[Link]> list = [Link]();
236. for ([Link] p : list) [Link](p);
237. [Link](false);
238. [Link] aktif = [Link]();
239. if (aktif == null) {
240. [Link]("Tidak ada periode aktif. Aktifkan di menu Periode.");
241. } else {
242. selectPeriodeById([Link]);
243. [Link]("Periode aktif: " + [Link] + " - " + [Link]);
244. }
245. } catch (Exception e) {
246. showErr("Gagal load periode: " + [Link](), e);
247. }
248. }
249. private void load(String keyword) {
250. try {
251. [Link](0);
252. List<[Link]> list = [Link](keyword);
253. for ([Link] p : list) {
254. String periodeLabel = [Link] + " - " + [Link];
255. [Link](new Object[]{
256. [Link], [Link], [Link],
257. [Link], [Link], periodeLabel,
258. [Link], [Link], [Link], [Link],
259. [Link], [Link]
260. });
261. }
262. } catch (Exception e) {
263. showErr("Gagal load prestasi: " + [Link](), e);
264. }
265. }
266. private void save() {
267. try {
268. [Link] siswa = ([Link])
[Link]();
269. if (siswa == null) {
270. [Link](this, "Siswa belum dipilih / data siswa kosong.",
"Validasi", JOptionPane.WARNING_MESSAGE);
271. return;
272. }
273. [Link] periode;
274. try {
275. periode = [Link]();
276. } catch (Exception ex) {
277. showErr("Gagal ambil periode aktif: " + [Link](), ex);
278. return;
279. }
280. if (periode == null) {
281. [Link](this,
282. "Tidak ada PERIODE AKTIF.\nBuka tab Periode → pilih periode → klik
'Jadikan Aktif'.",
283. "Periode belum aktif", JOptionPane.WARNING_MESSAGE);
284. return;
285. }
286. String lomba = [Link]().trim();
287. String tingkat = (String) [Link]();
288. String peringkat = [Link]().trim();
289. String kategori = (String) [Link]();
290. String tglStr = [Link]().trim();
291. String ket = [Link]().trim();
292. if ([Link]() || [Link]()) {
293. [Link](this, "Jenis lomba dan peringkat wajib diisi.",
"Validasi", JOptionPane.WARNING_MESSAGE);
294. return;
295. }
296. Date tgl = null;
297. if (![Link]()) {
298. try { tgl = [Link](tglStr); }
299. catch (Exception ex) {
300. [Link](this, "Format tanggal salah. Pakai YYYY-MM-
DD.", "Validasi", JOptionPane.WARNING_MESSAGE);
301. return;
302. }
303. }
304. if (selectedId == null) {
305. [Link]([Link], [Link], lomba, tingkat, peringkat, kategori, tgl, ket);
306. } else {
307. [Link](selectedId, [Link], [Link], lomba, tingkat, peringkat,
kategori, tgl, ket);
308. }
309. resetForm();
310. load([Link]());
311. [Link]("Simpan berhasil.");
312. } catch (Exception e) {
313. showErr("Gagal simpan prestasi: " + [Link](), e);
314. }
315. }
316. private void deleteSelected() {
317. if (selectedId == null) return;
318. int ok = [Link](this, "Yakin hapus data prestasi ini?",
"Konfirmasi",
319. JOptionPane.YES_NO_OPTION);
320. if (ok != JOptionPane.YES_OPTION) return;
321. try {
322. [Link](selectedId);
323. resetForm();
324. load([Link]());
325. } catch (Exception e) {
326. showErr("Gagal hapus prestasi: " + [Link](), e);
327. }
328. }
329. private void resetForm() {
330. selectedId = null;
331. if ([Link]() > 0) [Link](0);
332. if ([Link]() > 0) [Link](0);
333. [Link]("");
334. [Link](0);
335. [Link]("");
336. [Link](0);
337. [Link]("");
338. [Link]("");
339. [Link]();
340. [Link](false);
341. }
342. private void selectSiswaById(long id) {
343. for (int i = 0; i < [Link](); i++) {
344. [Link] it = [Link](i);
345. if (it != null && [Link] == id) {
346. [Link](i);
347. return;
348. }
349. }
350. }
351. private void selectPeriodeById(long id) {
352. for (int i = 0; i < [Link](); i++) {
353. [Link] it = [Link](i);
354. if (it != null && [Link] == id) {
355. [Link](i);
356. return;
357. }
358. }
359. }
360. private void hideCol(int idx) {
361. [Link]().getColumn(idx).setMinWidth(0);
362. [Link]().getColumn(idx).setMaxWidth(0);
363. }
364. private void showErr(String msg, Exception e) {
365. [Link]();
366. [Link](this, msg, "Error", JOptionPane.ERROR_MESSAGE);
367. [Link]("Error: " + msg);
368. }
369. private JLabel makeLogo(int w, int h) {
370. URL url = getClass().getResource("/kkp/assets/[Link]");
371. JLabel lbl = new JLabel();
372. if (url != null) {
373. ImageIcon icon = new ImageIcon(url);
374. Image scaled = [Link]().getScaledInstance(w, h, Image.SCALE_SMOOTH);
375. [Link](new ImageIcon(scaled));
376. }
377. return lbl;
378. }
379.}

J. Kelola Database Prestasi Siswa

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. public class PrestasiDAO {
6. public static class PrestasiRow {
7. public long id;
8. public long siswaId;
9. public long periodeId;
10. public String nis;
11. public String nama;
12. public String tahunAjaran;
13. public String semester;
14. public String jenisLomba;
15. public String tingkat;
16. public String peringkat;
17. public String kategori;
18. public Date tanggal;
19. public String keterangan;
20. public PrestasiRow(long id, long siswaId, long periodeId,
21. String nis, String nama,
22. String tahunAjaran, String semester,
23. String jenisLomba, String tingkat, String peringkat, String kategori,
24. Date tanggal, String keterangan) {
25. [Link] = id;
26. [Link] = siswaId;
27. [Link] = periodeId;
28. [Link] = nis;
29. [Link] = nama;
30. [Link] = tahunAjaran;
31. [Link] = semester;
32. [Link] = jenisLomba;
33. [Link] = tingkat;
34. [Link] = peringkat;
35. [Link] = kategori;
36. [Link] = tanggal;
37. [Link] = keterangan;
38. }
39. }
40. public static List<PrestasiRow> findByPeriodeAktif(String keyword) throws Exception {
41. [Link] aktif = [Link]();
42. if (aktif == null) return new [Link]<>();
43. boolean has = keyword != null && ![Link]().isEmpty();
44. String sql =
45. "SELECT [Link], p.siswa_id, p.periode_id, " +
46. "[Link], [Link], " +
47. "pr.tahun_ajaran, [Link], " +
48. "p.jenis_lomba, [Link], [Link], [Link], [Link], [Link] " +
49. "FROM prestasi p " +
50. "JOIN siswa s ON [Link] = p.siswa_id " +
51. "JOIN periode pr ON [Link] = p.periode_id " +
52. "WHERE p.periode_id=? " +
53. (has ? "AND ([Link] LIKE ? OR [Link] LIKE ? OR p.jenis_lomba LIKE ? OR [Link]
LIKE ? OR [Link] LIKE ? OR [Link] LIKE ?) " : "") +
54. "ORDER BY [Link] DESC";
55. try ([Link] c = [Link]();
56. [Link] ps = [Link](sql)) {
57. [Link](1, [Link]);
58. if (has) {
59. String kw = "%" + [Link]() + "%";
60. [Link](2, kw);
61. [Link](3, kw);
62. [Link](4, kw);
63. [Link](5, kw);
64. [Link](6, kw);
65. [Link](7, kw);
66. }
67. [Link]<PrestasiRow> list = new [Link]<>();
68. try ([Link] rs = [Link]()) {
69. while ([Link]()) {
70. [Link](new PrestasiRow(
71. [Link]("id"),
72. [Link]("siswa_id"),
73. [Link]("periode_id"),
74. [Link]("nis"),
75. [Link]("nama"),
76. [Link]("tahun_ajaran"),
77. [Link]("semester"),
78. [Link]("jenis_lomba"),
79. [Link]("tingkat"),
80. [Link]("peringkat"),
81. [Link]("kategori"),
82. [Link]("tanggal"),
83. [Link]("keterangan")
84. ));
85. }
86. }
87. return list;
88. }
89. }
90. public static List<PrestasiRow> findAll(String keyword) throws Exception {
91. boolean has = keyword != null && ![Link]().isEmpty();
92. String sql =
93. "SELECT [Link], p.siswa_id, p.periode_id, " +
94. "[Link], [Link], " +
95. "pr.tahun_ajaran, [Link], " +
96. "p.jenis_lomba, [Link], [Link], [Link], [Link], [Link] " +
97. "FROM prestasi p " +
98. "JOIN siswa s ON [Link] = p.siswa_id " +
99. "JOIN periode pr ON [Link] = p.periode_id " +
100. (has ? "WHERE [Link] LIKE ? OR [Link] LIKE ? OR p.jenis_lomba LIKE ? OR
[Link] LIKE ? OR [Link] LIKE ? OR [Link] LIKE ? OR pr.tahun_ajaran LIKE ?
OR [Link] LIKE ? " : "") +
101. "ORDER BY [Link] DESC";
102. try (Connection c = [Link]();
103. PreparedStatement ps = [Link](sql)) {
104. if (has) {
105. String kw = "%" + [Link]() + "%";
106. [Link](1, kw);
107. [Link](2, kw);
108. [Link](3, kw);
109. [Link](4, kw);
110. [Link](5, kw);
111. [Link](6, kw);
112. [Link](7, kw);
113. [Link](8, kw);
114. }
115. List<PrestasiRow> list = new ArrayList<>();
116. try (ResultSet rs = [Link]()) {
117. while ([Link]()) {
118. [Link](new PrestasiRow(
119. [Link]("id"),
120. [Link]("siswa_id"),
121. [Link]("periode_id"),
122. [Link]("nis"),
123. [Link]("nama"),
124. [Link]("tahun_ajaran"),
125. [Link]("semester"),
126. [Link]("jenis_lomba"),
127. [Link]("tingkat"),
128. [Link]("peringkat"),
129. [Link]("kategori"),
130. [Link]("tanggal"),
131. [Link]("keterangan")
132. ));
133. }
134. }
135. return list;
136. }
137. }
138. public static void insert(long siswaId, long periodeId,
139. String jenisLomba, String tingkat, String peringkat,
140. String kategori, Date tanggal, String keterangan) throws Exception {
141.
142. String sql = "INSERT INTO prestasi(siswa_id, periode_id, jenis_lomba, tingkat,
peringkat, kategori, tanggal, keterangan) " +
143. "VALUES(?,?,?,?,?,?,?,?)";
144. try (Connection c = [Link]();
145. PreparedStatement ps = [Link](sql)) {
146. [Link](1, siswaId);
147. [Link](2, periodeId);
148. [Link](3, jenisLomba);
149. [Link](4, tingkat);
150. [Link](5, peringkat);
151. [Link](6, kategori);
152. if (tanggal != null) [Link](7, tanggal);
153. else [Link](7, [Link]);
154. if (keterangan != null && ![Link]().isEmpty()) [Link](8,
[Link]());
155. else [Link](8, [Link]);
156. [Link]();
157. }
158. }
159. public static void update(long id, long siswaId, long periodeId,
160. String jenisLomba, String tingkat, String peringkat,
161. String kategori, Date tanggal, String keterangan) throws Exception {
162. String sql = "UPDATE prestasi SET siswa_id=?, periode_id=?, jenis_lomba=?, tingkat=?,
peringkat=?, kategori=?, tanggal=?, keterangan=? WHERE id=?";
163. try (Connection c = [Link]();
164. PreparedStatement ps = [Link](sql)) {
165. [Link](1, siswaId);
166. [Link](2, periodeId);
167. [Link](3, jenisLomba);
168. [Link](4, tingkat);
169. [Link](5, peringkat);
170. [Link](6, kategori);
171. if (tanggal != null) [Link](7, tanggal);
172. else [Link](7, [Link]);
173. if (keterangan != null && ![Link]().isEmpty()) [Link](8,
[Link]());
174. else [Link](8, [Link]);
175. [Link](9, id);
176. [Link]();
177. }
178. }
179. public static void delete(long id) throws Exception {
180. String sql = "DELETE FROM prestasi WHERE id=?";
181. try (Connection c = [Link]();
182. PreparedStatement ps = [Link](sql)) {
183. [Link](1, id);
184. [Link]();
185. }
186. }
187.}

K. Menu Kelola Periode

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. import [Link].*;
6. import [Link];
7. public class PeriodePanel extends JPanel {
8. private DefaultTableModel model;
9. private JTable table;
10. private JTextField tfTahun = new JTextField();
11. private JComboBox<String> cbSemester = new JComboBox<>(new String[]
{"Ganjil","Genap"});
12. private JTextField tfCari = new JTextField(18);
13. private Long selectedId = null;
14. public PeriodePanel() {
15. setLayout(new BorderLayout());
16. setBackground([Link]);
17. JPanel strip = new JPanel(new BorderLayout());
18. [Link](new Color(245,160,70));
19. [Link](new EmptyBorder(10,12,10,12));
20. JLabel title = new JLabel("Tahun Ajaran & Semester");
21. [Link](new Font("SansSerif", [Link], 22));
22. [Link](title, [Link]);
23. add(strip, [Link]);
24. JPanel body = new JPanel(new BorderLayout());
25. add(body, [Link]);
26. JPanel header = new JPanel(new BorderLayout());
27. [Link](new Color(35,63,110));
28. [Link](new EmptyBorder(18,18,18,18));
29. JLabel logo = [Link](130, 130);
30. [Link](logo, [Link]);
31. JLabel big = new JLabel("<html><center>Sistem Pendataan Siswa Berprestasi<br/>SMK
Karya Muda Depok</center></html>", [Link]);
32. [Link](UITheme.TITLE_BIG);
33. [Link]([Link]);
34. [Link](big, [Link]);
35. [Link](header, [Link]);
36. JPanel main = new JPanel(new BorderLayout());
37. [Link](new Color(210,225,245));
38. [Link](main, [Link]);
39. JPanel topBar = new JPanel(new BorderLayout());
40. [Link](false);
41. [Link](new EmptyBorder(10,18,6,18));
42. JPanel left = new JPanel(new FlowLayout([Link],8,0));
43. [Link](false);
44. JButton btnTambah = new JButton("Tambah Baru");
45. JButton btnHapus = new JButton("Hapus");
46. JButton btnAktif = new JButton("Jadikan Aktif");
47. JButton btnRefresh = new JButton("Refresh");
48. [Link](btnTambah);
49. [Link](btnHapus);
50. [Link](btnAktif);
51. [Link](btnRefresh);
52. JPanel right = new JPanel(new FlowLayout([Link],8,0));
53. [Link](false);
54. [Link](new JLabel("Cari:"));
55. [Link](tfCari);
56. JButton btnSearch = new JButton("Search");
57. [Link](btnSearch);
58. [Link](left, [Link]);
59. [Link](right, [Link]);
60. model = new DefaultTableModel(new Object[]{"ID","Tahun
Ajaran","Semester","Aktif"},0){
61. public boolean isCellEditable(int r,int c){return false;}
62. };
63. table = new JTable(model);
64. [Link](26);
65. [Link]().getColumn(0).setMinWidth(0);
66. [Link]().getColumn(0).setMaxWidth(0);
67. JScrollPane sp = new JScrollPane(table);
68. [Link](new Dimension(100,420));
69. JPanel tableWrap = new JPanel(new BorderLayout());
70. [Link](false);
71. [Link](new EmptyBorder(0,18,18,18));
72. [Link](sp, [Link]);
73. JPanel leftMain = new JPanel(new BorderLayout());
74. [Link](false);
75. [Link](topBar, [Link]);
76. [Link](tableWrap, [Link]);
77. JPanel rightForm = new JPanel(new BorderLayout());
78. [Link](new Dimension(360,0));
79. [Link](new Color(245,160,70));
80. [Link](new EmptyBorder(18,18,18,18));
81. JLabel formTitle = new JLabel("Input Periode");
82. [Link](new Font("SansSerif", [Link], 26));
83. [Link](formTitle, [Link]);
84. JPanel form = new JPanel(new GridLayout(0,1,0,10));
85. [Link]([Link]);
86. [Link](new EmptyBorder(14,14,14,14));
87. [Link](new JLabel("Tahun Ajaran"));
88. [Link](tfTahun);
89. [Link](new JLabel("Semester"));
90. [Link](cbSemester);
91. JButton btnSimpan = new JButton("Simpan");
92. JButton btnReset = new JButton("Reset");
93. [Link](btnSimpan);
94. [Link](btnReset);
95. [Link](form, [Link]);
96. JPanel center = new JPanel(new BorderLayout());
97. [Link](false);
98. [Link](leftMain, [Link]);
99. [Link](rightForm, [Link]);
100. [Link](center, [Link]);
101. [Link](e -> load(""));
102. [Link](e -> load([Link]()));
103. [Link](e -> load([Link]()));
104. [Link](e -> reset());
105. [Link](e -> reset());
106. [Link](e -> save());
107. [Link](e -> delete());
108. [Link](e -> aktifkan());
109. [Link]().addListSelectionListener(e -> {
110. int r = [Link]();
111. if (r >= 0) {
112. selectedId = (Long) [Link](r,0);
113. [Link]([Link](r,1).toString());
114. [Link]([Link](r,2));
115. }
116. });
117. load("");
118. }
119. private void load(String k){
120. try{
121. [Link](0);
122. List<[Link]> list = [Link](k);
123. [Link]((p) -> {
124. [Link](new Object[]
{[Link],[Link],[Link],[Link]?"YA":"TIDAK"});
125. });
126. }catch(Exception e){
127. [Link](this,[Link]());
128. }
129. }
130. private void save(){
131. try{
132. if(selectedId==null) [Link]([Link](),
[Link]().toString());
133. else [Link](selectedId, [Link](),
[Link]().toString());
134. reset(); load("");
135. }catch(Exception e){
136. [Link](this,[Link]());
137. }
138. }
139. private void delete(){
140. try{
141. if(selectedId!=null){ [Link](selectedId); reset(); load(""); }
142. }catch(Exception e){ [Link](this,[Link]()); }
143. }
144. private void aktifkan(){
145. try{
146. if(selectedId!=null){ [Link](selectedId); load(""); }
147. }catch(Exception e){ [Link](this,[Link]()); }
148. }
149. private void reset(){
150. selectedId=null;
151. [Link]("");
152. [Link](0);
153. [Link]();
154. }
155.}

L. Kelola Database Periode

1. package kkp;
2. import [Link].*;
3. import [Link];
4. import [Link];
5. public class PeriodeDAO {
6. public static class PeriodeRow {
7. public long id;
8. public String tahunAjaran;
9. public String semester;
10. public boolean aktif;
11. public PeriodeRow(long id, String tahunAjaran, String semester, boolean aktif) {
12. [Link] = id;
13. [Link] = tahunAjaran;
14. [Link] = semester;
15. [Link] = aktif;
16. }
17. }
18. public static List<PeriodeRow> findAll(String keyword) throws Exception {
19. boolean has = keyword != null && ![Link]().isEmpty();
20. String sql = "SELECT id, tahun_ajaran, semester, aktif FROM periode " +
21. (has ? "WHERE tahun_ajaran LIKE ? OR semester LIKE ? " : "") +
22. "ORDER BY aktif DESC, tahun_ajaran DESC, semester ASC";
23. try (Connection c = [Link]();
24. PreparedStatement ps = [Link](sql)) {
25. if (has) {
26. String kw = "%" + [Link]() + "%";
27. [Link](1, kw);
28. [Link](2, kw);
29. }
30. List<PeriodeRow> list = new ArrayList<>();
31. try (ResultSet rs = [Link]()) {
32. while ([Link]()) {
33. [Link](new PeriodeRow(
34. [Link]("id"),
35. [Link]("tahun_ajaran"),
36. [Link]("semester"),
37. [Link]("aktif") == 1
38. ));
39. }
40. }
41. return list;
42. }
43. }
44. public static void insert(String tahunAjaran, String semester) throws Exception {
45. String sql = "INSERT INTO periode(tahun_ajaran, semester, aktif) VALUES(?,?,0)";
46. try (Connection c = [Link]();
47. PreparedStatement ps = [Link](sql)) {
48. [Link](1, tahunAjaran);
49. [Link](2, semester);
50. [Link]();
51. }
52. }
53. public static void update(long id, String tahunAjaran, String semester) throws Exception {
54. String sql = "UPDATE periode SET tahun_ajaran=?, semester=? WHERE id=?";
55. try (Connection c = [Link]();
56. PreparedStatement ps = [Link](sql)) {
57. [Link](1, tahunAjaran);
58. [Link](2, semester);
59. [Link](3, id);
60. [Link]();
61. }
62. }
63. public static void delete(long id) throws Exception {
64. String sql = "DELETE FROM periode WHERE id=?";
65. try (Connection c = [Link]();
66. PreparedStatement ps = [Link](sql)) {
67. [Link](1, id);
68. [Link]();
69. }
70. }
71. public static void setAktif(long id) throws Exception {
72. try (Connection c = [Link]()) {
73. [Link](false);
74. try (PreparedStatement off = [Link]("UPDATE periode SET aktif=0");
75. PreparedStatement on = [Link]("UPDATE periode SET aktif=1
WHERE id=?")) {
76. [Link]();
77. [Link](1, id);
78. [Link]();
79. [Link]();
80. } catch (Exception e) {
81. [Link]();
82. throw e;
83. } finally {
84. [Link](true);
85. }
86. }
87. }
88. }

M. Integrasi Database

1. package kkp;
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. public class PeriodeRefDAO {
8. public static class PeriodeRef {
9. public long id;
10. public String tahunAjaran;
11. public String semester;
12. public boolean aktif;
13. public PeriodeRef(long id, String tahunAjaran, String semester, boolean aktif) {
14. [Link] = id;
15. [Link] = tahunAjaran;
16. [Link] = semester;
17. [Link] = aktif;
18. }
19. @Override
20. public String toString() {
21. return tahunAjaran + " - " + semester + (aktif ? " (AKTIF)" : "");
22. }
23. }
24. public static PeriodeRef getAktif() throws Exception {
25. String sql = "SELECT id, tahun_ajaran, semester, aktif FROM periode WHERE aktif=1
LIMIT 1";
26. try ([Link] c = [Link]();
27. [Link] ps = [Link](sql);
28. [Link] rs = [Link]()) {
29. if ([Link]()) {
30. return new PeriodeRef(
31. [Link]("id"),
32. [Link]("tahun_ajaran"),
33. [Link]("semester"),
34. [Link]("aktif") == 1
35. );
36. }
37. return null;
38. }
39. }
40. public static List<PeriodeRef> getAll() throws Exception {
41. String sql = "SELECT id, tahun_ajaran, semester, aktif FROM periode ORDER BY aktif
DESC, tahun_ajaran DESC, semester ASC";
42. try (Connection c = [Link]();
43. PreparedStatement ps = [Link](sql);
44. ResultSet rs = [Link]()) {
45. List<PeriodeRef> list = new ArrayList<>();
46. while ([Link]()) {
47. [Link](new PeriodeRef(
48. [Link]("id"),
49. [Link]("tahun_ajaran"),
50. [Link]("semester"),
51. [Link]("aktif") == 1
52. ));
53. }
54. return list;
55. }
56. }
57. }

N. Main Class Running

1. package kkp;
2. import [Link];
3. public class App {
4. public static void main(String[] args) {
5. [Link](() -> new LoginFrame().setVisible(true));
6. }
7. }

You might also like