Untitled
unknown
plain_text
a year ago
4.5 kB
5
Indexable
import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; class PersonModel { public ResultSet View() { ResultSet rs = null; try { SingletonFile s = SingletonFile.getInstance(); Statement stmt = s.connection.createStatement(); rs = stmt.executeQuery("SELECT * FROM person_table"); } catch (Exception e) { e.printStackTrace(); } return rs; } public boolean Insert(String pid, String fname, String lname) { boolean isSuccess = false; try{ SingletonFile s = SingletonFile.getInstance(); PreparedStatement preparedStatement = s.connection.prepareStatement("INSERT INTO person_table (PerID, PerFname, PerLname) VALUES (?, ?, ?)"); preparedStatement.setString(1,pid); preparedStatement.setString(2,fname); preparedStatement.setString(3,lname); preparedStatement.executeUpdate(); isSuccess = true; } catch (Exception e) { e.printStackTrace(); } return isSuccess; } public boolean Delete(String pid) { boolean isSuccess = false; try{ SingletonFile s = SingletonFile.getInstance(); PreparedStatement preparedStatement = s.connection.prepareStatement("DELETE FROM person_table WHERE PerID = ?"); preparedStatement.setString(1,pid); preparedStatement.executeUpdate(); isSuccess = true; } catch (Exception e) { e.printStackTrace(); } return isSuccess; } } class PersonView extends Frame implements ActionListener { Button view = null; Button delete = null; Button insert = null; TextField id_input; TextField fname_input; TextField lname_input; PersonView() { view = new Button("View"); delete = new Button("Delete"); insert = new Button("Insert"); view.setBounds(60, 510, 80, 50); add(view); delete.setBounds(160, 510, 80, 50); add(delete); insert.setBounds(260, 510, 80, 50); add(insert); id_input = new TextField(); id_input.setBounds(100, 100, 200, 30); add(id_input); fname_input = new TextField(); fname_input.setBounds(100, 150, 200, 30); add(fname_input); lname_input = new TextField(); lname_input.setBounds(100, 200, 200, 30); add(lname_input); view.addActionListener(this); delete.addActionListener(this); insert.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource().equals(view)){ ResultSet rs = new PersonModel().View(); try { StringBuilder records = new StringBuilder(); while(rs.next()) { records.append("ID:").append(rs.getString("PerID")).append(",").append("Firstname: ").append(rs.getString("PerFname")).append(",").append("Lastname: ").append(rs.getString("PerLname")).append("\n"); } JOptionPane.showMessageDialog(this, records.toString(),"Records",JOptionPane.INFORMATION_MESSAGE); } catch (SQLException ex) { ex.printStackTrace(); } } else if (e.getSource().equals(delete)) { //Delete Record String pid = id_input.getText(); // assuming id_input is defined elsewhere boolean isSuccess = new PersonModel().Delete(pid); if (isSuccess) { JOptionPane.showMessageDialog(this, "Record deleted Successfully.", "Delete", JOptionPane.INFORMATION_MESSAGE); } } else if (e.getSource().equals(insert)) { // Insert Record String pid = id_input.getText(); String fname = fname_input.getText(); String lname = lname_input.getText(); boolean isSuccess = new PersonModel().Insert(pid, fname, lname); if (isSuccess) { JOptionPane.showMessageDialog(this, "Record inserted Successfully.", "Insert", JOptionPane.INFORMATION_MESSAGE); } } } // Main method public static void main(String[] args) { // Create and configure the frame PersonView personView = new PersonView(); personView.setSize(400, 600); personView.setLayout(null); personView.setVisible(true); } }
Editor is loading...
Leave a Comment