Created
May 9, 2018 18:16
-
-
Save seanghay/380d9456ce0029259c8662614f9ec3ec to your computer and use it in GitHub Desktop.
JavaForm
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
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Stack; | |
public class FormApp extends JFrame implements ActionListener { | |
private final JButton buttonDelete; | |
private final JLabel labelStatus; | |
private final JButton buttonInsert; | |
private final JButton getAllButton; | |
private final JButton buttonUpdate; | |
private static final String DATABASE = "C:\\Users\\Seanghay\\Documents\\Students.accdb"; | |
private final JList<String> listOfStudents; | |
private final Connection connection; | |
public FormApp(Connection connection) { | |
this.connection = connection; | |
setTitle("List of Students"); | |
setSize(590,500); | |
setResizable(false); | |
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
setLayout(null); | |
listOfStudents = new JList<>(); | |
getAllButton = new JButton("Get Students"); | |
buttonInsert = new JButton("Insert"); | |
buttonDelete = new JButton("Delete"); | |
buttonUpdate = new JButton("Update"); | |
labelStatus = new JLabel("Status"); | |
listOfStudents.setBounds(10,10,400, 300); | |
getAllButton.setBounds(430, 10, 130, 30); | |
buttonInsert.setBounds(430, 50, 130, 30); | |
buttonDelete.setBounds(430, 90, 130, 30); | |
buttonUpdate.setBounds(430, 130, 130, 30); | |
labelStatus.setBounds(10, 320, 400, 30); | |
JScrollPane scrollPane = new JScrollPane(); | |
scrollPane.setViewportView(listOfStudents); | |
add(scrollPane); | |
add(listOfStudents); | |
add(labelStatus); | |
add(buttonInsert); | |
add(buttonDelete); | |
add(buttonUpdate); | |
add(getAllButton); | |
setVisible(true); | |
buttonDelete.addActionListener(this); | |
buttonInsert.addActionListener(this); | |
buttonUpdate.addActionListener(this); | |
getAllButton.addActionListener(this); | |
} | |
private void setStatus(String text) { | |
labelStatus.setText(text); | |
} | |
public static void main(String[] args) { | |
try { | |
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); | |
try { | |
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://" + DATABASE); | |
new FormApp(connection); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (e.getSource() == buttonDelete) { | |
onDelete(); | |
} else if (e.getSource() == buttonInsert) { | |
onInsert(); | |
} else if (e.getSource() == buttonUpdate) { | |
onUpdate(); | |
} else if (e.getSource() == getAllButton) { | |
getAllStudents(); | |
} | |
} | |
private void getAllStudents() { | |
try { | |
DefaultListModel<String> students = new DefaultListModel<>(); | |
Statement statement = connection.createStatement(); | |
ResultSet rs = statement.executeQuery("Select * from TableStudent"); | |
while (rs.next()) { | |
students.addElement(rs.getString(2)); | |
} | |
rs.close(); | |
statement.close(); | |
listOfStudents.setModel(students); | |
listOfStudents.updateUI(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void onUpdate() { | |
} | |
private void onInsert() { | |
try { | |
Statement statement = connection.createStatement(); | |
statement.executeUpdate("INSERT INTO TableStudent(Name, Gender, DateOfBirth)" + | |
" VALUES ('INSERT NEW "+ System.currentTimeMillis() +"', 'MALE', #5/5/2000#)"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void onDelete() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment