Swing Application Assignment Help

Must Retrieve information from Oracle

Page 1 of 1

2 Replies - 2344 Views - Last Post: 26 March 2010 - 05:04 PM Rate Topic: -----

#1 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Swing Application Assignment Help

Posted 26 March 2010 - 02:28 PM

I've been working on this assignment for close to a week, and the deadline is fast approaching. I have to create a Swing application that takes information from a from an oracle database. I have 3 things written, but am getting many errors, and can't get it to fully work.

Here's the main code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Revised_Code extends JFrame{

  SelectData selectData = new SelectData();

  private JButton buttonPrev=new JButton("Prev");
  private JButton buttonreset=new JButton("Reset");
  private JButton buttonNext=new JButton("Next");

  private JLabel labelHeader=new JLabel("Database Browser",JLabel.CENTER);
  private JLabel labelName=new JLabel("Name");
  private JLabel labelAddress=new JLabel("Address");
  private JLabel labelCity=new JLabel("City");
  private JLabel labelState=new JLabel("State");
  private JLabel labelZip=new JLabel("Zip");

  private JTextField textFieldName=new JTextField();
  private JTextField textFieldAddress=new JTextField();
  private JTextField textFieldCity=new JTextField();
  private JTextField textFieldState=new JTextField();
  private JTextField textFieldZip=new JTextField();

  private ActionListener buttonListener = new ActionListener(){

    public void actionPerformed(ActionEvent e){

      String buttonselected = ((JButton)e.getSource()).getText();
    

      if(buttonselected.equals("Previous")){

        update(selectData.getPrev());
      }    

      if(buttonselected.equals("Next")){

        update(selectData.getNext());
      }    

      if(buttonselected.equals("Reset")){

        update(selectData.getReset());
      }    

     
      }    
    };
  

  public Revised_Code(){

    super("Revised_Code2");

    
    btnPrev = new JButton("Previous");
    btnNext = new JButton("Next");
    btnReset = new JButton("Reset");
    

    txtName = new JTextField("");
    txtAddress = new JTextField("");
    txtCity = new JTextField("");
    txtState = new JTextField("");
    txtZip = new JTextField("");

    lblName = new JLabel("Name");
    lblAddress = new JLabel("Address");
    lblCity = new JLabel("City");
    lblState = new JLabel("State");
    lblZip = new JLabel("Zip");

    txtName.setEditable(false);
    txtAddress.setEditable(false);
    txtCity.setEditable(false);
    txtState.setEditable(false);
    txtZip.setEditable(false);
  }

  public void launchJFrame(){

    // width - height
    setSize(500, 300);
    getContentPane().setLayout(null);
    getContentPane().setBackground(Color.white);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    
    getContentPane().add(btnPrev);
    getContentPane().add(btnNext);
    getContentPane().add(btnReset);
    

    getContentPane().add(txtName);
    getContentPane().add(txtAddress);
    getContentPane().add(txtCity);
    getContentPane().add(txtState);
    getContentPane().add(txtZip);

    getContentPane().add(lblName);
    getContentPane().add(lblAddress);
    getContentPane().add(lblCity);
    getContentPane().add(lblState);
    getContentPane().add(lblZip);

    lblName.setBounds(new Rectangle(65, 10, 100, 30));
    lblAddress.setBounds(new Rectangle(65, 45, 100, 30));
    lblCity.setBounds(new Rectangle(65, 80, 100, 30));
    lblState.setBounds(new Rectangle(65, 115, 100, 30));
    lblZip.setBounds(new Rectangle(65, 150, 100, 30));

    txtName.setBounds(new Rectangle(210, 10, 100, 30));
    txtAddress.setBounds(new Rectangle(210, 45, 100, 30));
    txtCity.setBounds(new Rectangle(210, 80, 100, 30));
    txtState.setBounds(new Rectangle(210, 115, 100, 30));
    txtZip.setBounds(new Rectangle(210, 150, 100, 30));

    
    btnNext.setBounds(new Rectangle(100, 200, 90, 30));
    btnPrev.setBounds(new Rectangle(190, 200, 90, 30));
    btnReset.setBounds(new Rectangle(280, 200, 90, 30));
    

    setVisible(true);

    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        shutDown();
      }
    });

    
    btnPrev.addActionListener(buttonListener);
    btnNext.addActionListener(buttonListener);
    btnReset.addActionListener(buttonListener);
   
  }

  private void update(MyRecord record){

    txtName.setText(record.getTeam());
    txtAddress.setText(record.getCity());
    txtCity.setText(record.getYear_T());
    txtState.setText(record.getLoserTeam());
    txtZip.setText(record.getLoserCity());
  }

  private void shutDown(){

    int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");

    if(returnVal == JOptionPane.YES_OPTION){

      System.exit(0);
    }
  }

  public static void main(String[] args){

    Revised_Code layout = new Revised_Code();
    layout.launchJFrame();
   
  }
}



Here's more

public class MyRecord{

  String Name;
  String Address;
  String City;
  String State;
  String Zip;

  public String getName(){

    return name;
  }

  public void setName(String name){

    this.name = new String(name);
  }

  public String getAddress(){

    return address;
  }

  public void setAddress(String address){

    this.address = new String(address);
  }

  public String getCity(){

    return City;
  }
 
  public void setCity(String city){

    this.city = new String(city);
  }

  public String getState(){

    return State;
  }

  public void setState(String state){

    this.state = new String(state);
  }

  public String getZip(){

    return loserZip;
  }

  public void setZip(String zip){

    this.Zip = new String(zip);
  }

  public String toString(){

    return new String(name.toString().trim() + ", " +
                      address.toString().trim() + ", " +
                      city.toString().trim() + ", " +
                      state.toString().trim() + ", " +
                      zip.toString().trim());
                  }
              }



Finally...

import java.net.URL;
import java.sql.*;
import oracle.jdbc.OracleResultSetMetaData;

public class SelectData{

  Connection con;

  Statement stmt;

  ResultSet resultSet;

  public SelectData(){

    try{

      DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

      con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student1","pass");

      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    }
    catch(Exception e){

      System.out.println("Error connection to database.");
      System.exit(0);
    }

    try{

      resultSet = stmt.executeQuery("SELECT * FROM Address ORDER BY Name");
    }
    catch(SQLException e){

      System.out.println(e);
      System.out.println("Result Request Failed");
    }
  }

  public MyRecord getPrev(){

    MyRecord myRecord = new MyRecord();

    try{
      resultSet.Prev();

      myRecord.setName(resultSet.getString(resultSet.findColumn("Name")));
      myRecord.setAddress(resultSet.getString(resultSet.findColumn("Address")));
      myRecord.setCity(resultSet.getString(resultSet.findColumn("City")));
      myRecord.setState(resultSet.getString(resultSet.findColumn("State")));
      myRecord.setZip(resultSet.getString(resultSet.findColumn("Zip")));
    }
    catch(Exception e){

      System.out.println(e);
    }

    return myRecord;
  }

  public MyRecord getNext(){

    MyRecord myRecord = new MyRecord();

    try{
      resultSet.next();

      myRecord.setName(resultSet.getString(resultSet.findColumn("Name")));
      myRecord.setAddress(resultSet.getString(resultSet.findColumn("Address")));
      myRecord.setCity(resultSet.getString(resultSet.findColumn("City")));
      myRecord.setState(resultSet.getString(resultSet.findColumn("State")));
      myRecord.setYear(resultSet.getString(resultSet.findColumn("Zip")));
    }
    catch(SQLException e){

      return this.getPrev();
    }
    catch(Exception e){

      System.out.println(e);
    }

    return myRecord;
  }

  public MyRecord getReset(){

    MyRecord myRecord = new MyRecord();

    try{
      resultSet.Reset();

      myRecord.setName(resultSet.getString(resultSet.findColumn("Name")));
      myRecord.setAddress(resultSet.getString(resultSet.findColumn("Address")));
      myRecord.setCity(resultSet.getString(resultSet.findColumn("City")));
      myRecord.setState(resultSet.getString(resultSet.findColumn("State")));
      myRecord.setZip(resultSet.getString(resultSet.findColumn("Zip")));
    }
   
    catch(Exception e){

      System.out.println(e);
    }

    return myRecord;
  }

  public void close(){

    try{
      stmt.close();
      con.close();
    }
    catch(SQLException e){

      System.out.println("Connection close failed");
    }
  }

  public static void main(String args[]) {

    SelectData selectMyData = new SelectData();

    System.out.println(selectMyData.getFirst());
    System.out.println(selectMyData.getLast());

    selectMyData.close();
  }
}    



Please, I really need help on this one. Any advice you can give me would be appreciated.

Thanks,
Scott

Is This A Good Question/Topic? 0
  • +

Replies To: Swing Application Assignment Help

#2 Guest_BasementArtScott*


Reputation:

Re: Swing Application Assignment Help

Posted 26 March 2010 - 04:59 PM

Here's the original code, that I had to base this code off of. It was meant to be a revision of this first assignment.

Please, seriously, I really need help with this one.

import javax.swing.*;
import java.awt.*;

class Assignment_02Source extends JFrame
{

    private JButton buttonPrev=new JButton("Prev");
    private JButton buttonreset=new JButton("Reset");
    private JButton buttonNext=new JButton("Next");

    private JLabel labelHeader=new JLabel("Database Browser",JLabel.CENTER);
    private JLabel labelName=new JLabel("Name");
    private JLabel labelAddress=new JLabel("Address");
    private JLabel labelCity=new JLabel("City");
    private JLabel labelState=new JLabel("State");
    private JLabel labelZip=new JLabel("Zip");

    private JTextField textFieldName=new JTextField();
    private JTextField textFieldAddress=new JTextField();
    private JTextField textFieldCity=new JTextField();
    private JTextField textFieldState=new JTextField();
    private JTextField textFieldZip=new JTextField();

    DataClass [] DataClassArray =
    {new DataClass("Fred", "Wayne", "101 Here", "NE", "55551"),
    new DataClass("George", "Thomas", "102 There", "ME", "55552"),
    new DataClass("Mike", "Johnson", "103 No Where", "OK", "55553")};

    int arrayPointer = 0;

    public Assignment_02Source(String title)
    {

        super(title);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        JPanel cp=(JPanel)getContentPane();

        labelHeader.setFont(new Font("TimesRoman",Font.BOLD,24));
        labelHeader.setBounds(40,10,300,50);

        buttonPrev.setBounds(30,250,80,25);
        buttonreset.setBounds(150,250,80,25);
        buttonNext.setBounds(270,250,80,25);

        labelName.setBounds(10,80,80,25);
        labelAddress.setBounds(10,110,80,25);
        labelCity.setBounds(10,140,80,25);
        labelState.setBounds(10,170,80,25);
        labelZip.setBounds(10,200,80,25);

        textFieldName.setBounds(120,80,250,25);
        textFieldAddress.setBounds(120,110,250,25);
        textFieldCity.setBounds(120,140,250,25);
        textFieldState.setBounds(120,170,250,25);
        textFieldZip.setBounds(120,200,250,25);

        cp.setLayout(null);
        cp.add(labelHeader);
        cp.add(buttonPrev);
        cp.add(buttonreset);
        cp.add(buttonNext);
        cp.add(labelName);
        cp.add(textFieldName);
        cp.add(labelAddress);
        cp.add(textFieldAddress);
        cp.add(labelCity);
        cp.add(textFieldCity);
        cp.add(labelState);
        cp.add(textFieldState);
        cp.add(labelZip);
        cp.add(textFieldZip);

        addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(java.awt.event.WindowEvent evt)
            {
                shutDown();
            }
        });

        // Action Listener for Previous Button Supplied for assignment.
        buttonPrev.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                // Check to see if array is not on the first element.
                if(arrayPointer > 0)
                {
                    // Subtract 1 from the array pointer.
                    --arrayPointer;
                }
                // If at the first element of the array, go to the last.
                else
                {
                    arrayPointer = DataClassArray.length - 1;
                }
            // Call set fields method (provided in assignment) to get data.
            setFields(arrayPointer);
            }
        });


        // Action Listener for Next Button Created for assignment.
        buttonNext.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                // Check to see if array is not on the last element.
                if (arrayPointer < DataClassArray.length -1)
                {
                    // Add 1 to the array pointer.
                    arrayPointer++;
                }
                else
                // Set array to the first element.
                {
                    arrayPointer = 0;
                }
            // Call set fields method (provided in assignment) to get data.
            setFields(arrayPointer);
            }
        });

        // Action Listener for Reset Button Created for assignment.
        buttonreset.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                // Set array to the first element.
                arrayPointer = 0;
                // Call set fields method (provided in assignment) to get data.
                setFields(arrayPointer);
            }
        });
    }

    private void setFields(int position)
    {
        textFieldName.setText(DataClassArray[position].getName());
        textFieldAddress.setText(DataClassArray[position].getAddress());
        textFieldCity.setText(DataClassArray[position].getCity());
        textFieldState.setText(DataClassArray[position].getState());
        textFieldZip.setText(DataClassArray[position].getZip());
    }

    private void shutDown()
    {
        int returnVal=JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");

        if(returnVal==JOptionPane.YES_OPTION)
        {
            System.exit(0);
        }
    }

    public static void main(String args[])
    {
        Assignment_02Source a2 = new Assignment_02Source("Database Browser");

        a2.setSize(400,350);
        a2.setVisible(true);
    }
}

class DataClass
{
    // To save space I declared all String objects on a single line
    String name, address, city, state, zipCode;

    DataClass(String name, String address, String city, String state, String zipCode)
    {
        this.name = name;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
    }

    // To save space I placed the methods on a single line
    String getName(){return this.name;}
    String getAddress(){return this.address;}
    String getCity(){return this.city;}
    String getState(){return this.state;}
    String getZip(){return this.zipCode;}
}


Was This Post Helpful? 0

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Swing Application Assignment Help

Posted 26 March 2010 - 05:04 PM

You just posted this a couple hours ago so please show some patience. I know this is an assignment and is due soon but you have to remember that everyone here are volunteers, we have lives outside here (lives, families, school, work etc) so it can take some time to get a reply.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1