import java.sql.*; import javax.swing.*; import javax.swing.border.*; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.FlowLayout; import java.awt.event.*; /* * This program relies on a database table defined as follows: * * create table emailnames * (id int not null unique primary key auto_increment, * first varchar(20), last varchar(32), * email1 varchar(128), email2 varchar(128)); * * With this definition the set of updater methods that were added to * the JDBC 2.0 interface will work and simplify inserting, deleting, and * updating a table. * */ public class SQLTestGUI extends JFrame implements ActionListener { private final static String USER = "root"; private final static String PWD = "setonhall"; private final static String DB = "myaddressbook"; private final static String URL = "jdbc:mysql://localhost:3306/"; private final static String DRIVER = "com.mysql.jdbc.Driver"; private JTextField firstField = new JTextField(20); private JTextField lastField = new JTextField(20); private JTextField emailField = new JTextField(20); private JTextField email2Field = new JTextField(20); private JButton nextButton = new JButton(">>"); private JButton prevButton = new JButton("<<"); private JButton firstButton = new JButton("|<<"); private JButton lastButton = new JButton(">>|"); private JButton addButton = new JButton("[ ]"); private JButton delButton = new JButton("X"); private Connection connection = null; private Statement stmt = null; private ResultSet results = null; private String password = null; private class Closer extends WindowAdapter { public void windowClosing(WindowEvent we) { try { disconnect(); } catch(Exception ex) { System.err.println("Closing error:\n\t" + ex); } setVisible(false); System.exit(0); } } public SQLTestGUI() { super("My Address Book"); setupGUI(); validate(); pack(); setVisible(true); try { connect(); getData(); getNext(); } catch(Exception ex) { connection = null; stmt = null; JOptionPane.showMessageDialog(this, "CONNECTION ERROR:\n" + ex); } } private void getPassword() { JPasswordField passField = new JPasswordField(); JLabel passLabel = new JLabel("Password: "); JPanel panel = new JPanel(new BorderLayout()); panel.add("West", passLabel); panel.add("Center", passField); int response = JOptionPane.showConfirmDialog( this, panel, "Enter password", JOptionPane.OK_CANCEL_OPTION); if (response == JOptionPane.OK_OPTION) password = String.valueOf(passField.getPassword()); } private void connect() throws Exception { } private void disconnect() throws Exception { } private void getData() throws Exception { } private void setData() throws SQLException { } public void getNext() throws Exception { } public void getPrev() throws Exception { } public void getFirst() throws Exception { } public void getLast() throws Exception { } public void handleAdd() throws Exception { } public void handleDel() throws Exception { } public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); try { if (source == nextButton) getNext(); else if (source == prevButton) getPrev(); else if (source == firstButton) getFirst(); else if (source == lastButton) getLast(); else if (source == addButton) handleAdd(); else if (source == delButton) handleDel(); } catch(Exception ex) { JOptionPane.showMessageDialog(this, "ERROR:\n" + ex); } } private void setupGUI() { JPanel labels = new JPanel(new GridLayout(4,1)); labels.add(new JLabel("First name: ", SwingUtilities.RIGHT)); labels.add(new JLabel("Last name: ", SwingUtilities.RIGHT)); labels.add(new JLabel("Email 1: ", SwingUtilities.RIGHT)); labels.add(new JLabel("Email 2: ", SwingUtilities.RIGHT)); JPanel fields = new JPanel(new GridLayout(4,1)); fields.add(firstField); fields.add(lastField); fields.add(emailField); fields.add(email2Field); JPanel buttonsMove = new JPanel(new FlowLayout()); buttonsMove.add(firstButton); buttonsMove.add(prevButton); buttonsMove.add(nextButton); buttonsMove.add(lastButton); buttonsMove.setBorder(new TitledBorder(new EtchedBorder(), "Movement")); JPanel buttonsAction = new JPanel(new FlowLayout()); buttonsAction.add(addButton); buttonsAction.add(delButton); buttonsAction.setBorder(new TitledBorder(new EtchedBorder(), "Action")); JPanel buttons = new JPanel(new FlowLayout()); buttons.add(buttonsMove); buttons.add(buttonsAction); JLabel title = new JLabel("

My Address Book

"); JPanel data = new JPanel(new BorderLayout()); data.add("North", title); data.add("West", labels); data.add("Center", fields); data.setBorder(new EtchedBorder()); setLayout(new BorderLayout()); add("Center", data); add("South", buttons); firstButton.setToolTipText("First record"); prevButton.setToolTipText("Previous record"); nextButton.setToolTipText("Next record"); lastButton.setToolTipText("Last record"); addButton.setToolTipText("Add a new record"); delButton.setToolTipText("Delete current record"); firstButton.addActionListener(this); prevButton.addActionListener(this); nextButton.addActionListener(this); lastButton.addActionListener(this); addButton.addActionListener(this); delButton.addActionListener(this); this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new Closer()); } public static void main(String args[]) { SQLTestGUI t = new SQLTestGUI(); } }