Last active
June 10, 2020 03:11
-
-
Save derysudrajat/277b2ae3ef8788212356ff979ba2d542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derysudrajat.jdbc; | |
public class Data { | |
private int id; | |
private String nama; | |
private String alamat; | |
private String phone; | |
public Data(int id, String nama, String alamat, String phone) { | |
this.id = id; | |
this.nama = nama; | |
this.alamat = alamat; | |
this.phone = phone; | |
} | |
public Data(String nama, String alamat, String phone) { | |
this.nama = nama; | |
this.alamat = alamat; | |
this.phone = phone; | |
} | |
public String getNama() { | |
return nama; | |
} | |
public void setNama(String nama) { | |
this.nama = nama; | |
} | |
public String getAlamat() { | |
return alamat; | |
} | |
public void setAlamat(String alamat) { | |
this.alamat = alamat; | |
} | |
public String getPhone() { | |
return phone; | |
} | |
public void setPhone(String phone) { | |
this.phone = phone; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derysudrajat.jdbc; | |
import javax.swing.*; | |
import javax.swing.table.DefaultTableModel; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Main { | |
private static List<Data> dataList = new ArrayList<>(); | |
private static Connection connection; | |
private static DefaultTableModel tableModel = new DefaultTableModel(); | |
private static MainActivity mainActivity = new MainActivity(); | |
private static int currentIndex; | |
public static void main(String[] args) { | |
currentIndex = -1; | |
initView(); | |
getConnection(); | |
getData(); | |
mainActivity.getAddButton().addActionListener(e -> { | |
if (mainActivity.getAddButton().getText().equals("Save")) { | |
if (isValidate()) { | |
String query = "insert into mahasiswa (nama, alamat, telepon) VALUES (?,?,?)"; | |
try { | |
PreparedStatement statement = connection.prepareStatement(query); | |
statement.setString(1, mainActivity.getTfNama().getText()); | |
statement.setString(2, mainActivity.getTfAlamat().getText()); | |
statement.setString(3, mainActivity.getTfTelepon().getText()); | |
statement.executeUpdate(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
mainActivity.getTfNama().setText(""); | |
mainActivity.getTfAlamat().setText(""); | |
mainActivity.getTfTelepon().setText(""); | |
setEnableText(false); | |
} | |
} else if (mainActivity.getAddButton().getText().equals("Add")) { | |
mainActivity.getAddButton().setText("Save"); | |
setEnableText(true); | |
} else { | |
if (isValidate()) { | |
String query = "update mahasiswa set nama = ?, alamat = ?, telepon = ? where id = ?"; | |
try { | |
PreparedStatement statement = connection.prepareStatement(query); | |
statement.setString(1, mainActivity.getTfNama().getText()); | |
statement.setString(2, mainActivity.getTfAlamat().getText()); | |
statement.setString(3, mainActivity.getTfTelepon().getText()); | |
statement.setInt(4, dataList.get(currentIndex).getId()); | |
statement.executeUpdate(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
mainActivity.getEditButton().setText("Edit"); | |
mainActivity.getAddButton().setText("Add"); | |
mainActivity.getTfNama().setText(""); | |
mainActivity.getTfAlamat().setText(""); | |
mainActivity.getTfTelepon().setText(""); | |
setEnableText(false); | |
} | |
} | |
getData(); | |
}); | |
mainActivity.getEditButton().addActionListener(e -> { | |
if (mainActivity.getEditButton().getText().equals("Edit")) { | |
mainActivity.getEditButton().setText("Cancel"); | |
mainActivity.getAddButton().setText("Update"); | |
} else { | |
mainActivity.getEditButton().setText("Edit"); | |
mainActivity.getAddButton().setText("Add"); | |
} | |
setEnableText(true); | |
}); | |
mainActivity.getDeleteButton().addActionListener(e -> { | |
if (currentIndex != -1) { | |
String query = "delete from mahasiswa where id = ?"; | |
try { | |
PreparedStatement statement = connection.prepareStatement(query); | |
statement.setInt(1, dataList.get(currentIndex).getId()); | |
statement.executeUpdate(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
getData(); | |
} | |
}); | |
mainActivity.getTable1().addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
super.mouseClicked(e); | |
int index = mainActivity.getTable1().getSelectedRow(); | |
currentIndex = index; | |
System.out.println("Current Index: " + currentIndex); | |
if (index == -1) { | |
return; | |
} | |
String nama = String.valueOf(tableModel.getValueAt(index, 0)); | |
String alamat = String.valueOf(tableModel.getValueAt(index, 1)); | |
String telepon = String.valueOf(tableModel.getValueAt(index, 2)); | |
mainActivity.getTfNama().setText(nama); | |
mainActivity.getTfAlamat().setText(alamat); | |
mainActivity.getTfTelepon().setText(telepon); | |
} | |
}); | |
mainActivity.getTable1().setModel(tableModel); | |
} | |
private static void initView() { | |
JFrame frame = new JFrame("Coba"); | |
frame.setContentPane(mainActivity.getPanel1()); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
tableModel.addColumn("Nama"); | |
tableModel.addColumn("Alamat"); | |
tableModel.addColumn("Telepon"); | |
setEnableText(false); | |
} | |
private static void getConnection() { | |
String user = "root"; | |
String pw = ""; | |
try { | |
Class.forName("com.mysql.cj.jdbc.Driver"); | |
String stringConnection = "jdbc:mysql://localhost:3306/db_mahasiswa"; | |
connection = DriverManager.getConnection(stringConnection, user, pw); | |
} catch (Exception e) { | |
System.out.println("error: " + e); | |
} | |
} | |
private static void getData() { | |
tableModel.getDataVector().removeAllElements(); | |
tableModel.fireTableDataChanged(); | |
String query = "select * from mahasiswa"; | |
try { | |
PreparedStatement statement = connection.prepareStatement(query); | |
ResultSet resultSet = statement.executeQuery(); | |
while (resultSet.next()) { | |
Data data = new Data( | |
resultSet.getInt("id"), | |
resultSet.getString("nama"), | |
resultSet.getString("alamat"), | |
resultSet.getString("telepon") | |
); | |
dataList.add(data); | |
Object[] objects = {data.getNama(), data.getAlamat(), data.getPhone()}; | |
tableModel.addRow(objects); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static boolean isValidate() { | |
return !mainActivity.getTfNama().getText().trim().isEmpty() | |
&& !mainActivity.getTfAlamat().getText().trim().isEmpty() | |
&& !mainActivity.getTfTelepon().getText().trim().isEmpty(); | |
} | |
private static void setEnableText(boolean state) { | |
mainActivity.getTfNama().setEnabled(state); | |
mainActivity.getTfAlamat().setEnabled(state); | |
mainActivity.getTfTelepon().setEnabled(state); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derysudrajat.jdbc; | |
import javax.swing.*; | |
public class MainActivity { | |
private JPanel panel1; | |
private JTextField tfNama; | |
private JLabel namaLabel; | |
private JTable table1; | |
private JButton addButton; | |
private JButton editButton; | |
private JButton deleteButton; | |
private JButton refreshButton; | |
private JTextField tfAlamat; | |
private JTextField tfTelepon; | |
private JScrollPane mTable; | |
public JPanel getPanel1() { | |
return panel1; | |
} | |
public JButton getAddButton() { | |
return addButton; | |
} | |
public JButton getEditButton() { | |
return editButton; | |
} | |
public JButton getDeleteButton() { | |
return deleteButton; | |
} | |
public JButton getRefreshButton() { | |
return refreshButton; | |
} | |
public JTextField getTfNama() { | |
return tfNama; | |
} | |
public JLabel getNamaLabel() { | |
return namaLabel; | |
} | |
public JTable getTable1() { | |
return table1; | |
} | |
public void setTable1(JTable table1) { | |
this.table1 = table1; | |
} | |
public JTextField getTfAlamat() { | |
return tfAlamat; | |
} | |
public JTextField getTfTelepon() { | |
return tfTelepon; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment