Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Dbweek3;
- import javax.swing.SwingUtilities;
- import java.awt.BorderLayout;
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- import java.awt.GridLayout;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- import java.sql.*;
- import javax.swing.JButton;
- public class Data extends JFrame {
- private static final long serialVersionUID = 1L;
- private JPanel jContentPane = null;
- private JPanel jPanel = null;
- private JLabel jLabel = null;
- private JLabel jLabel1 = null;
- private JLabel jLabel2 = null;
- private JLabel jLabel3 = null;
- private JTextField txtCustNo = null;
- private JTextField txtName = null;
- private JTextField txtPhone = null;
- private JTextField txtBDay = null;
- //Programmer declared variables
- static Connection connection;
- Statement stmt; // @jve:decl-index=0:
- String sqlQuery; // @jve:decl-index=0:
- ResultSet rs; // @jve:decl-index=0:
- private JPanel jPanel1 = null;
- private JButton btnFirst = null;
- private JButton btnNext = null;
- private JButton btnPrevious = null;
- private JButton btnLast = null;
- // Some constants
- private static final int RESULT_FIRST = 0;
- private static final int RESULT_LAST = 1;
- private static final int RESULT_NEXT = 2;
- private static final int RESULT_PREVIOUS = 3;
- /**
- * This method initializes jPanel
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJPanel() {
- if (jPanel == null) {
- jLabel3 = new JLabel();
- jLabel3.setText("Birthday");
- jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
- jLabel2 = new JLabel();
- jLabel2.setText("Phone Number");
- jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
- jLabel1 = new JLabel();
- jLabel1.setText("Name");
- jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
- jLabel = new JLabel();
- jLabel.setText("Customer No.");
- jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
- jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
- GridLayout gridLayout = new GridLayout();
- gridLayout.setRows(4);
- gridLayout.setColumns(2);
- jPanel = new JPanel();
- jPanel.setLayout(gridLayout);
- jPanel.add(jLabel, null);
- jPanel.add(getTxtCustNo(), null);
- jPanel.add(jLabel1, null);
- jPanel.add(getTxtName(), null);
- jPanel.add(jLabel2, null);
- jPanel.add(getTxtPhone(), null);
- jPanel.add(jLabel3, null);
- jPanel.add(getTxtBDay(), null);
- }
- return jPanel;
- }
- /**
- * This method initializes txtCustNo
- *
- * @return javax.swing.JTextField
- */
- private JTextField getTxtCustNo() {
- if (txtCustNo == null) {
- txtCustNo = new JTextField();
- }
- return txtCustNo;
- }
- /**
- * This method initializes txtName
- *
- * @return javax.swing.JTextField
- */
- private JTextField getTxtName() {
- if (txtName == null) {
- txtName = new JTextField();
- }
- return txtName;
- }
- /**
- * This method initializes txtPhone
- *
- * @return javax.swing.JTextField
- */
- private JTextField getTxtPhone() {
- if (txtPhone == null) {
- txtPhone = new JTextField();
- }
- return txtPhone;
- }
- /**
- * This method initializes txtBDay
- *
- * @return javax.swing.JTextField
- */
- private JTextField getTxtBDay() {
- if (txtBDay == null) {
- txtBDay = new JTextField();
- }
- return txtBDay;
- }
- /**
- * This method initializes jPanel1
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJPanel1() {
- if (jPanel1 == null) {
- GridLayout gridLayout1 = new GridLayout();
- gridLayout1.setRows(1);
- gridLayout1.setColumns(4);
- jPanel1 = new JPanel();
- jPanel1.setLayout(gridLayout1);
- jPanel1.add(getBtnFirst(), null);
- jPanel1.add(getBtnNext(), null);
- jPanel1.add(getBtnPrevious(), null);
- jPanel1.add(getBtnLast(), null);
- }
- return jPanel1;
- }
- /**
- * This method initializes btnFirst
- *
- * @return javax.swing.JButton
- */
- private JButton getBtnFirst() {
- if (btnFirst == null) {
- btnFirst = new JButton();
- btnFirst.setText("First");
- btnFirst.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- showResult(Data.RESULT_FIRST);
- }
- });
- }
- return btnFirst;
- }
- /**
- * This method initializes btnNext
- *
- * @return javax.swing.JButton
- */
- private JButton getBtnNext() {
- if (btnNext == null) {
- btnNext = new JButton();
- btnNext.setText("Next");
- btnNext.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- showResult(Data.RESULT_NEXT);
- }
- });
- }
- return btnNext;
- }
- /**
- * This method initializes btnPrevious
- *
- * @return javax.swing.JButton
- */
- private JButton getBtnPrevious() {
- if (btnPrevious == null) {
- btnPrevious = new JButton();
- btnPrevious.setText("Previous");
- btnPrevious.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- showResult(Data.RESULT_PREVIOUS);
- }
- });
- }
- return btnPrevious;
- }
- /**
- * This method initializes btnLast
- *
- * @return javax.swing.JButton
- */
- private JButton getBtnLast() {
- if (btnLast == null) {
- btnLast = new JButton();
- btnLast.setText("Last");
- btnLast.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- try{
- showResult(Data.RESULT_LAST);
- }
- catch(Exception ex){
- }
- }
- });
- }
- return btnLast;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- Data thisClass = new Data();
- thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- thisClass.setVisible(true);
- }
- });
- }
- /**
- * This is the default constructor
- */
- public Data() {
- super();
- initialize();
- getDBReady();
- }
- /**
- * This method initializes this
- *
- * @return void
- */
- private void initialize() {
- this.setSize(385, 309);
- this.setContentPane(getJContentPane());
- this.setTitle("Customer Database");
- }
- /**
- * This method initializes jContentPane
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJContentPane() {
- if (jContentPane == null) {
- jContentPane = new JPanel();
- jContentPane.setLayout(new BorderLayout());
- jContentPane.add(getJPanel(), BorderLayout.NORTH);
- jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
- }
- return jContentPane;
- }
- // @jve:decl-index=0:
- /**
- * Show the result for the current pos in the cursor
- *
- * @param pos - RESULT INFO
- */
- private void showResult(int pos) {
- try {
- // Check if its instantiated
- if(rs != null) {
- // Temp variable
- boolean ret = false;
- // LEts see how to move cursor
- switch(pos) {
- case RESULT_FIRST:
- ret = rs.first();
- break;
- case RESULT_LAST:
- ret = rs.last();
- break;
- case RESULT_NEXT:
- if(rs.isLast()) {
- ret = rs.first();
- }
- else {
- ret = rs.next();
- }
- break;
- case RESULT_PREVIOUS:
- if(rs.isFirst()) {
- ret = rs.last();
- }
- else {
- ret = rs.previous();
- }
- break;
- }
- // Fill in UI
- txtCustNo.setText(rs.getString(1));
- txtName.setText(rs.getString(2));
- txtPhone.setText(rs.getString(3));
- txtBDay.setText((rs.getDate(4)).toString());
- }
- }
- catch(Exception ex) {
- JOptionPane.showMessageDialog(this, ex.getMessage(), "Error Executing",JOptionPane.ERROR_MESSAGE);;
- }
- }
- public void getDBReady() {
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- connection = DriverManager.getConnection("jdbc:odbc:CIS355");
- //connection = DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);" +
- // "DBQ=C:\\CIS435\\CIS435_lab1_customers.mdb");
- stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_UPDATABLE);
- sqlQuery = "select Cust_no, Name, Phone, Bday from customers";
- rs = stmt.executeQuery(sqlQuery);
- // Get the first result and show it to the screen
- showResult(Data.RESULT_NEXT);
- }
- catch(Exception ex){
- JOptionPane.showMessageDialog(this, ex.getMessage(), "Error Initializing database",JOptionPane.ERROR_MESSAGE);;
- }
- }
- } // @jve:decl-index=0:visual-constraint="10,10"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement