Sick of this

For the life of me, I can not figure out how to get this to work.

I am trying to get this program to open a selected database which resides in a combobox. I now can not get the program to compile. I will post the code, and if you can figure out what is going on here I would appreciate it.

The error I am getting now is that "f" can not be declared as static. I have tried to position this line elsewhere in the code, but that just gives me 7 more errors.

If this is just plain crap code, and you suggest something different, that is great. I have only been working with Java for over a month, so go slow.

Thanks
berg


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

public class InteractiveSQL extends JFrame
                            implements ActionListener
{
 
  public class MyClass1
  {
    public static JFrame f;
      public MyClass1()
      {
          MyClass1 myClass1 = new MyClass1();
          f=new JFrame();
          f.show (true);
          f.setBounds (0,0,100,100);
          f.pack ();
          myClass1.invokeStandalone = true;
      }
  private boleen invokedStandalone = false;
  }
  public static void main(String[] args)
  {
   
   

    //the list of dbs
    String[] dbs = { "jdbc:odbc:osdconnection", "jdbc:odbc:shippingconnection", "jdbc:odbc:Employee", "jdbc:odbc:CustomerService" };

    //to initialize the combobox
    JComboBox dbList = new JComboBox(dbs);
   
    //Set the default values for the cammand line args
    String user  = "quest";
    String password  = "quest";
    String url  = (String)dbList.getSelectedItem();
    String driver  = "sun.jdbc.odbc.JdbcOdbcDriver";
   
   

    //Up to 4 arguments in the sequence database url, driver, user, password
    switch(args.length)
                 {
                   case 4:
                     password = args[3];
                     user = args[2];
                      driver=args[1];
                      url = args[0];    
                    break;
                   case 3:
                     user = args[2];
                     driver = args[1];
                     url = args[0];
                    break;
                   case 2:
                     driver = args[1];
                     url = args[0];
                    break;
                   case 1:
                     url = args[0];
                    break;
                 }
    InteractiveSQL theApp = new InteractiveSQL(driver, url, user, password);
  }
 
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if(source == command)
      executeSQL();

    else if(source == clearQueryItem)
      command.setText("");

     else if(source == exitItem)
     {
       dispose();
       System.exit(0);
     }
  }

  public void executeSQL()
  {
    String query = command.getText();
    if(query == null)
      return;
    try
    {
      model.setResultSet(statement.executeQuery(query));
      status.setText("ResultSet has " + model.getRowCount() + " rows");
    }
    catch (SQLException sqle)
    {
      status.setText(sqle.getMessage());
    }
  }

  public InteractiveSQL(String driver, String url, String user, String password)
  {
    super("InteractiveSQL");
    setBounds(0, 0, 400, 300);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    addWindowListener(new WindowHandler());
   
    //the list of dbs
    String[] dbs = { "jdbc:odbc:osdconnection", "jdbc:odbc:shippingconnection", "jdbc:odbc:employee", "jdbc:odbc:CustomerServiceConnection" };

    //to initialize the combobox
    //JComboBox dbList = new JComboBox(dbs);
    //dbList.addActionListener(this);
    //getContentPane().add(dbList, BorderLayout.WEST);

    //allow user to type in new db name
    //dbList.setEditable(true);
   
    //Add the input for the SQL statement at the top
    command.setToolTipText("Key SQL command and press Enter");
    command.addActionListener(this);
    getContentPane().add(command, BorderLayout.NORTH);

    //Add the status reporting area at the bottom
    status.setLineWrap(true);
    status.setWrapStyleWord(true);
    getContentPane().add(status, BorderLayout.SOUTH);

    //Create the menubar from the menu items
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');
    clearQueryItem.addActionListener(this);
    exitItem.addActionListener(this);
    fileMenu.add(clearQueryItem);
    fileMenu.add(exitItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);

    //Establish a database connection and set up the table
    try
    {      
      Class.forName(driver);
      connection = DriverManager.getConnection(url, user, password);
      statement = connection.createStatement();

      model = new ResultsModel();
      JTable table = new JTable(model);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      resultsPane = new JScrollPane(table);
      getContentPane().add(resultsPane, BorderLayout.CENTER);

    }
    catch(ClassNotFoundException cnfe)
    {
      System.err.println(cnfe);
    }
    catch(SQLException sqle)
    {
      System.err.println(sqle);
    }
    pack();
    setVisible(true);
  }
  class WindowHandler extends WindowAdapter

  {
    //Handler for window closing event
    public void WindowClosing(WindowEvent e)
    {
      dispose();
      System.exit(0);
    }
  }

  JTextField command = new JTextField();
  JTextArea status = new JTextArea(3,1);
  JScrollPane resultsPane;

  JMenuBar menuBar = new JMenuBar();
  JMenuItem clearQueryItem = new JMenuItem("clear query");
  JMenuItem exitItem = new JMenuItem("Exit");

  Connection connection;
  Statement statement;
  ResultsModel model;
}
LVL 4
berg1375Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sen_kumCommented:
  You are getting the error f must not be static because
only the members inside a interface or class in top level can be static. Here f is a inner class member , therefore it cannot be static.

   You can remove static from f , i don't find any need that it must be static .
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
berg1375Author Commented:
I am getting a new error, and I have never seen this before:

Note: InteractiveSQL.java uses or overrides a deprecated API. Recompile with "-deprecation" for details"

What the heck does this mean?
0
sen_kumCommented:
  When java release a new version it deprecates some of older methods. That means java can stop supporting those methods at any time.

   for example to show a frame is jdk1.0 the method is

   frame.show() ;

   that method is deprecated in jdk1.1 and new method is introduced.

   frame.setVisible( true ) ;

   You can you frame.show() also in jdk1.1 but this code will not work in java's later release. They can stop supporting frame.show() in later release. So it's adviced to use the new methods instead of deprecated methods, java has given equivalent methods for all the deprecated methods.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

berg1375Author Commented:
I see, where do I find these new methods?
0
berg1375Author Commented:
Okay, I figured that out, and it compiles, but there is no combobox, or frame, or anything in the window.

What is going on? More points if you can help me get this thing working.

Thanks
berg
0
sen_kumCommented:

  You have not added the combobox to frame
  You did not set the size of the frame
0
berg1375Author Commented:
I thought the f.setBounds(0,0,100,100) sets the size of the frame.

I have the code for the combobox commented out further down the code, but how do I get the code to put the combobox into the frame? I am guessing just uncommenting the combobox won't accomplish this. Will I leave the code where it is, and just refer to the Frame?
0
sen_kumCommented:
post the newly edited code
0
berg1375Author Commented:
Here it is, not much difference. The combobox is down in the middle commented out.


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

public class InteractiveSQL extends JFrame
                            implements ActionListener
{
 
  public class MyClass1
  {
    public JFrame f;
      public MyClass1()
      {
          MyClass1 myClass1 = new MyClass1();
          f=new JFrame();
          f.setVisible(true);
          f.setBounds (0,0,100,100);
          f.pack ();
          myClass1.invokedStandalone = true;
      }
  private boolean invokedStandalone = true;
  }
  public static void main(String[] args)
  {
   
   

    //the list of dbs
    String[] dbs = { "jdbc:odbc:osdconnection", "jdbc:odbc:shippingconnection", "jdbc:odbc:Employee", "jdbc:odbc:CustomerService" };

    //to initialize the combobox
    JComboBox dbList = new JComboBox(dbs);
   
    //Set the default values for the cammand line args
    String user  = "quest";
    String password  = "quest";
    String url  = (String)dbList.getSelectedItem();
    String driver  = "sun.jdbc.odbc.JdbcOdbcDriver";
   
   

    //Up to 4 arguments in the sequence database url, driver, user, password
    switch(args.length)
                 {
                   case 4:
                     password = args[3];
                     user = args[2];
                      driver=args[1];
                      url = args[0];    
                    break;
                   case 3:
                     user = args[2];
                     driver = args[1];
                     url = args[0];
                    break;
                   case 2:
                     driver = args[1];
                     url = args[0];
                    break;
                   case 1:
                     url = args[0];
                    break;
                 }
    InteractiveSQL theApp = new InteractiveSQL(driver, url, user, password);
  }
 
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if(source == command)
      executeSQL();

    else if(source == clearQueryItem)
      command.setText("");

     else if(source == exitItem)
     {
       dispose();
       System.exit(0);
     }
  }

  public void executeSQL()
  {
    String query = command.getText();
    if(query == null)
      return;
    try
    {
      model.setResultSet(statement.executeQuery(query));
      status.setText("ResultSet has " + model.getRowCount() + " rows");
    }
    catch (SQLException sqle)
    {
      status.setText(sqle.getMessage());
    }
  }

  public InteractiveSQL(String driver, String url, String user, String password)
  {
    super("InteractiveSQL");
    setBounds(0, 0, 400, 300);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    addWindowListener(new WindowHandler());
   
    //the list of dbs
    String[] dbs = { "jdbc:odbc:osdconnection", "jdbc:odbc:shippingconnection", "jdbc:odbc:employee", "jdbc:odbc:CustomerServiceConnection" };

    //to initialize the combobox
    //JComboBox dbList = new JComboBox(dbs);
    //dbList.addActionListener(this);
    //getContentPane().add(dbList, BorderLayout.WEST);

    //allow user to type in new db name
    //dbList.setEditable(true);
   
    //Add the input for the SQL statement at the top
    command.setToolTipText("Key SQL command and press Enter");
    command.addActionListener(this);
    getContentPane().add(command, BorderLayout.NORTH);

    //Add the status reporting area at the bottom
    status.setLineWrap(true);
    status.setWrapStyleWord(true);
    getContentPane().add(status, BorderLayout.SOUTH);

    //Create the menubar from the menu items
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');
    clearQueryItem.addActionListener(this);
    exitItem.addActionListener(this);
    fileMenu.add(clearQueryItem);
    fileMenu.add(exitItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);

    //Establish a database connection and set up the table
    try
    {      
      Class.forName(driver);
      connection = DriverManager.getConnection(url, user, password);
      statement = connection.createStatement();

      model = new ResultsModel();
      JTable table = new JTable(model);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      resultsPane = new JScrollPane(table);
      getContentPane().add(resultsPane, BorderLayout.CENTER);

    }
    catch(ClassNotFoundException cnfe)
    {
      System.err.println(cnfe);
    }
    catch(SQLException sqle)
    {
      System.err.println(sqle);
    }
    pack();
    setVisible(true);
  }
  class WindowHandler extends WindowAdapter

  {
    //Handler for window closing event
    public void WindowClosing(WindowEvent e)
    {
      dispose();
      System.exit(0);
    }
  }

  JTextField command = new JTextField();
  JTextArea status = new JTextArea(3,1);
  JScrollPane resultsPane;

  JMenuBar menuBar = new JMenuBar();
  JMenuItem clearQueryItem = new JMenuItem("clear query");
  JMenuItem exitItem = new JMenuItem("Exit");

  Connection connection;
  Statement statement;
  ResultsModel model;
}
0
sen_kumCommented:
ResultsModel model ;

In package does this class is present. what version of java are you using?
0
berg1375Author Commented:
jdk1.2

Yes, ResultsModel is a class, and it is present. I can post it for you if you would like.

berg
0
sen_kumCommented:
yes post that class , i not am able to compile your code
0
berg1375Author Commented:
import java.sql.*;
import javax.swing.table.*;
import java.util.*;

class ResultsModel extends AbstractTableModel
{
  String[] columnNames = new String[0];
 
  Vector dataRows = new Vector();
   
  public void setResultSet(ResultSet results)
  {
   
    try
    {
     
      ResultSetMetaData metadata = results.getMetaData();

      int columns = metadata.getColumnCount();
      columnNames = new String[columns];

      //Get the column names
      for(int i = 0; i < columns; i++)
        columnNames[i] = metadata.getColumnLabel(i+1);


      //Get all rows
      dataRows = new Vector();
      String[] rowData;
      while(results.next())
      {
        rowData = new String[columns];
        for(int i = 0; i < columns; i++)
          rowData[i] = results.getString(i+1);

        dataRows.addElement(rowData);
      }

      fireTableChanged(null);
    }
    catch (SQLException sqle)
    {
      System.err.println(sqle);
    }
  }
  public int getColumnCount()
  {
    return columnNames.length;
  }
  public int getRowCount()
  {
    if(dataRows == null)
      return 0;
    else
      return dataRows.size();
  }
  public Object getValueAt(int row, int column)
  {
    return ((String[])(dataRows.elementAt(row)))[column];
  }

  public String getColumnName(int column)
  {
    return columnNames[column] == null ? "No Name" : columnNames[column];
  }
}
0
sen_kumCommented:
 import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.table.*;
  import java.sql.*;
  import java.util.* ;

  public class InteractiveSQL extends JFrame
                              implements ActionListener
  {
      //the list of dbs
      static String[] dbs = { "jdbc:odbc:osdconnection", "jdbc:odbc:shippingconnection", "jdbc:odbc:Employee",
                       "jdbc:odbc:CustomerService" };

      static JComboBox dbList = new JComboBox( dbs );

    public InteractiveSQL(String driver, String url, String user, String  password)
    {
      super("InteractiveSQL");
      setBounds(0, 0, 400, 300);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      addWindowListener(new WindowHandler());

      //the list of dbs
      dbList.addActionListener(this);
      getContentPane().add(dbList, BorderLayout.WEST);

      //allow user to type in new db name
      dbList.setEditable(true);

      //Add the input for the SQL statement at the top
      command.setToolTipText("Key SQL command and press Enter");
      command.addActionListener(this);
      getContentPane().add(command, BorderLayout.NORTH);

      //Add the status reporting area at the bottom
      status.setLineWrap(true);
      status.setWrapStyleWord(true);
      getContentPane().add(status, BorderLayout.SOUTH);

      //Create the menubar from the menu items
      JMenu fileMenu = new JMenu("File");
      fileMenu.setMnemonic('F');
      clearQueryItem.addActionListener(this);
      exitItem.addActionListener(this);
      fileMenu.add(clearQueryItem);
      fileMenu.add(exitItem);
      menuBar.add(fileMenu);
      setJMenuBar(menuBar);

      //Establish a database connection and set up the table
      try
      {
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);

        statement = connection.createStatement();

        model = new ResultsModel();
        JTable table = new JTable(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        resultsPane = new JScrollPane(table);
        getContentPane().add(resultsPane, BorderLayout.CENTER);

      }
      catch(ClassNotFoundException cnfe)
      {
        System.err.println(cnfe);
      }
      catch(SQLException sqle)
      {
        System.err.println(sqle);
      }
      pack();
      setVisible(true);
    }

      public static void main(String[] args)
      {

      //to initialize the combobox

      //Set the default values for the cammand line args
      String user  = "quest";
      String password  = "quest";
      String url  = (String)dbList.getSelectedItem();
      String driver  = "sun.jdbc.odbc.JdbcOdbcDriver";



      //Up to 4 arguments in the sequence database url, driver, user, password
      switch(args.length)
                   {
                     case 4:
                       password = args[3];
                       user = args[2];
                        driver=args[1];
                        url = args[0];
                      break;
                     case 3:
                       user = args[2];
                       driver = args[1];
                       url = args[0];
                      break;
                     case 2:
                       driver = args[1];
                       url = args[0];
                      break;
                     case 1:
                       url = args[0];
                      break;
                   }
      InteractiveSQL theApp = new InteractiveSQL(driver, url, user,
  password);
    }

    public void actionPerformed(ActionEvent e)
    {
      Object source = e.getSource();
      if(source == command)
        executeSQL();

      else if(source == clearQueryItem)
        command.setText("");

       else if(source == exitItem)
       {
         dispose();
         System.exit(0);
       }
    }

    public void executeSQL()
    {
      String query = command.getText();
      if(query == null)
        return;
      try
      {
        model.setResultSet(statement.executeQuery(query));
        status.setText("ResultSet has " + model.getRowCount() + "rows");
      }
      catch (SQLException sqle)
      {
        status.setText(sqle.getMessage());
      }
    }

    class WindowHandler extends WindowAdapter

    {
      //Handler for window closing event
      public void WindowClosing(WindowEvent e)
      {
        dispose();
        System.exit(0);
      }
    }

    JTextField command = new JTextField();
    JTextArea status = new JTextArea(3,1);
    JScrollPane resultsPane;

    JMenuBar menuBar = new JMenuBar();
    JMenuItem clearQueryItem = new JMenuItem("clear query");
    JMenuItem exitItem = new JMenuItem("Exit");

    Connection connection;
    Statement statement;
    ResultsModel model;
  }
0
berg1375Author Commented:
Okay, it compiles and runs, but when I select a database and try to run a query, ti says the table doesn't exist. I know I am spelling it right, and the tables do exist. It seems the combobox isn't holding the value of the selected item. Is that the problem?

Thanks
berg
0
berg1375Author Commented:
I have figured out something.

It only lets me connect to the first database in the combobox. I have four listed(as you can see), but if I select the third database, it won't connect, if I select the first it connects and runs. Why is it not registering the other databases? I have DSN's setup for all of them.
0
sen_kumCommented:

  What you did not validate the answer i gave, i think i solved your problem.
0
berg1375Author Commented:
Adjusted points to 60
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.