Problems with declaration of type

Im trying to create a gui that will display the results of database query but when entering the code I receive the following message, ResultSetTableModel cannot be resolved to a type. When I try to run the application I get the following error, Exception in thread "main" java.lang.Error: Unresolved compilation problem: at catWeb.GUI.main(GUI.java:137). Line 137 reads, public static void main(String[] args).
package catWeb;
 
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.Box;
 
public class GUI extends JFrame
{
	// JDBC driver and database URL
	static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
	static final String DATABASE_URL = "jdbc:odbc:db1.mdb";
	
	static final String DEFAULT_QUERY = "SELECT * FROM USERIDS";
	
    private ResultSetTableModel tableModel;
	private JTextArea queryArea;
	
	public GUI()
	{
		super("Displaying Query Results");
		
		// create ResultSetTableModel and display database table
		try
		{
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);
			
			// set up JTextArea in which user types queries
			queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);
			queryArea.setWrapStyleWord(true);
			queryArea.setLineWrap(true);
			
			JScrollPane scrollPane = new JScrollPane(queryArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
					                                            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			
			// set up JButton for submitting queries
			JButton submitButton = new JButton("Submit Button");
			
			// create Box to manage placement of queryArea and submitButton in GUI
			Box box = Box.createHorizontalBox();
			box.add(scrollPane);
			box.add(submitButton);
			
			// create JTable delegate for tableModel
			JTable resultTable = new JTable(tableModel);
			
			// place GUI components on content pane
			add(box, BorderLayout.NORTH);
			add(new JScrollPane(resultTable), BorderLayout.CENTER);
			
			// create event listener for submitButton
			submitButton.addActionListener(
					
					new ActionListener()
					{
						// pass query to table
						public void actionPerformed(ActionEvent event)
						{
							// perform a new query
							try
							{
								tableModel.setQuery(queryArea.getText());
								
							} // end try
							catch (SQLException sqlException)
							{
								JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Database error",
										                      JOptionPane.ERROR_MESSAGE);
								
								// try to recover from invalid user query by executing default query
								try
								{
									tableModel.setQuery(DEFAULT_QUERY);
									queryArea.setText(DEFAULT_QUERY);
									
								} // end try
								catch (SQLException sqlException2)
								{
									JOptionPane.showMessageDialog(null, sqlException2.getMessage(), "Database erro",
											                      JOptionPane.ERROR_MESSAGE);
									
									// ensure database connection is closed
									tableModel.disconnectFromDatabase();
									
									System.exit(1); // terminate application
								} // end inner catch 
							} // end outer catch
						} // end actionPerformed
					} // end ActionListener inner class
		         ); // end call to addActionListener
			
			setSize(500, 250);  // set window size
			setVisible(true);
		} // end try
		
		catch(ClassNotFoundException classNotFound)
		{
			JOptionPane.showMessageDialog(null, "JDBC driver not found", "Driver not found", JOptionPane.ERROR_MESSAGE);
			
			// ensure database connection is closed
			tableModel.disconnectFromDatabase();
			
			System.exit(1);  // terminate application
			
		}  // end catch
		
		// dispose of window when user quits application (this overrides the default of HIDE_ON_CLOSE
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		// ensure database connection is closed when user quits application
		addWindowListener(
				
				new WindowAdapter()
				{
					// disconnect from database and exit when window has closed
					public void windowClosed(WindowEvent event)
					{
						tableModel.disconnectFromDatabase();
						System.exit(1);
					} // end method windowClosed
				} // end WindowAdapter inner class
			); // end call to addWindowListener
	} // end DisplayQueryResults constructor
	
 
	
	public static void main(String[] args) 
	{
		new GUI();
 
	} // end main
 
} // end class GUI

Open in new window

Carter_MachineryProgrammerAsked:
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.

brunoguimaraesCommented:
0
CEHJCommented:
You probably need to import ResultSetTableModel into that package
0
Carter_MachineryProgrammerAuthor Commented:
Ok, changed up the import code to make sure that I get everthing imported tried it again but still receive the same errors.
package catWeb;
 
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
 
public class GUI extends JFrame
{
	// JDBC driver and database URL
	static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
	static final String DATABASE_URL = "jdbc:odbc:db1.mdb";
	
	static final String DEFAULT_QUERY = "SELECT * FROM USERIDS";
	
    private ResultSetTableModel tableModel;
	private JTextArea queryArea;
	
	public GUI()
	{
		super("Displaying Query Results");
		
		// create ResultSetTableModel and display database table
		try
		{
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);
			
			// set up JTextArea in which user types queries
			queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);
			queryArea.setWrapStyleWord(true);
			queryArea.setLineWrap(true);
			
			JScrollPane scrollPane = new JScrollPane(queryArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
					                                            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			
			// set up JButton for submitting queries
			JButton submitButton = new JButton("Submit Button");
			
			// create Box to manage placement of queryArea and submitButton in GUI
			Box box = Box.createHorizontalBox();
			box.add(scrollPane);
			box.add(submitButton);
			
			// create JTable delegate for tableModel
			JTable resultTable = new JTable(tableModel);
			
			// place GUI components on content pane
			add(box, BorderLayout.NORTH);
			add(new JScrollPane(resultTable), BorderLayout.CENTER);
			
			// create event listener for submitButton
			submitButton.addActionListener(
					
					new ActionListener()
					{
						// pass query to table
						public void actionPerformed(ActionEvent event)
						{
							// perform a new query
							try
							{
								tableModel.setQuery(queryArea.getText());
								
							} // end try
							catch (SQLException sqlException)
							{
								JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Database error",
										                      JOptionPane.ERROR_MESSAGE);
								
								// try to recover from invalid user query by executing default query
								try
								{
									tableModel.setQuery(DEFAULT_QUERY);
									queryArea.setText(DEFAULT_QUERY);
									
								} // end try
								catch (SQLException sqlException2)
								{
									JOptionPane.showMessageDialog(null, sqlException2.getMessage(), "Database erro",
											                      JOptionPane.ERROR_MESSAGE);
									
									// ensure database connection is closed
									tableModel.disconnectFromDatabase();
									
									System.exit(1); // terminate application
								} // end inner catch 
							} // end outer catch
						} // end actionPerformed
					} // end ActionListener inner class
		         ); // end call to addActionListener
			
			setSize(500, 250);  // set window size
			setVisible(true);
		} // end try
		
		catch(ClassNotFoundException classNotFound)
		{
			JOptionPane.showMessageDialog(null, "JDBC driver not found", "Driver not found", JOptionPane.ERROR_MESSAGE);
			
			// ensure database connection is closed
			tableModel.disconnectFromDatabase();
			
			System.exit(1);  // terminate application
			
		}  // end catch
		
		// dispose of window when user quits application (this overrides the default of HIDE_ON_CLOSE
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		// ensure database connection is closed when user quits application
		addWindowListener(
				
				new WindowAdapter()
				{
					// disconnect from database and exit when window has closed
					public void windowClosed(WindowEvent event)
					{
						tableModel.disconnectFromDatabase();
						System.exit(1);
					} // end method windowClosed
				} // end WindowAdapter inner class
			); // end call to addWindowListener
	} // end DisplayQueryResults constructor
	
 
	
	public static void main(String[] args) 
	{
		new GUI();
 
	} // end main
 
} // end class GUI

Open in new window

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.

objectsCommented:
you need to include the ResultSetTableModel class in your classpath, and add an import statement specifying its location.

Let me know if you have any questions.
0
CEHJCommented:
There are no imports there
0
CEHJCommented:
(of anything other than Sun classes)
0
objectsCommented:
> changed up the import code to make sure that I get everthing imported

you haven't added the import statement for the ResultSetTableModel as I suggested above
0
Carter_MachineryProgrammerAuthor Commented:
Im not sure I exactly follow you on adding ResultSetTabelModel class to my classpath and then import it specifying its location???
0
CEHJCommented:
>>you haven't added the import statement for the ResultSetTableModel as I suggested above

You mean as *I* suggested above ;-)
0
objectsCommented:
if the class is in package x.y.z
then add to your class


import x.y.z.ResultSetTabelModel;
0
CEHJCommented:
If you really meant to have ResultSetTableModel in package catWeb (packages names should be all lower case) then make sure it's really in there
0
Carter_MachineryProgrammerAuthor Commented:
Checked in the package and I do not have ResultSetTableModel as a class listed under javax.swing.table package. How do I find or create the class within the package javax.swing.table.
0
Carter_MachineryProgrammerAuthor Commented:
Update! I have a ResultSetModelTable class built now, Im just a little confused now how I incorporate it with the above code???
0
CEHJCommented:
Put it in the same package as the one you posted
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
Carter_MachineryProgrammerAuthor Commented:
Alright, got the class in the same package and Im now trying to make an instance of it but Im still receiving the the same error. I included a snippet of the class and at the bottom an example of how Im trying to make an instance of the class in the main.
package catWeb;
 
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
 
public class ResultSetTableModel extends AbstractTableModel
{
	private Connection connection;
	private Statement statement;
	private ResultSet resultSet;
	private ResultSetMetaData metaData;
	private int numberOfRows;
	
	private boolean connectedToDatabase = false;
	
	public ResultSetTableModel(String driver, String url, String username, String password,
			                   String query) throws SQLException, ClassNotFoundException
	{
		// load database driver class
		Class.forName(driver);
		
		// connect to database
		connection = DriverManager.getConnection(url);
		
		// create statement to query database
		statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, resultSet.CONCUR_READ_ONLY);
		
		// update database connection status
		connectedToDatabase = true;
		
		setQuery(query);
		
	}
	
	// get class that represents column type
	public Class getColumnClass( int column) throws IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		// determine Java class of column
		try
		{
			String className = metaData.getColumnClassName(column + 1);
			
			return Class.forName(className);
			
		}
		catch (Exception exception)
		{
			exception.printStackTrace();
			
		}
		
		return Object.class;
	}
	
	public int getColumnCount() throws IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to database");
		
		// determine number of columns
		try
		{
			return metaData.getColumnCount();
			
		}
		catch (SQLException sqlException)
		{
			sqlException.printStackTrace();
			
		}
		return 0;
	}
	
	public String getColumnName(int column) throws IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to database");
		
		// determine column name
		try
		{
			return metaData.getColumnName(column + 1);
			
		}
		catch (SQLException sqlException)
		{
			sqlException.printStackTrace();
			
		}
		
		return "";
	}
	
	public int getRowCount() throws IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to database");
		
		return numberOfRows;
	}
	
	public Object getValueAt(int row, int column)
		throws IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to database");
		
		// obtain a value at specified ResultSet row and column
		try
		{
			resultSet.absolute(row + 1);
			return resultSet.getObject(column + 1);
			
		} // end try
		catch (SQLException sqlException)
		{
			sqlException.printStackTrace();
			
		} // end catch
		
		return "";
	}
	
	public void setQuery(String query) throws SQLException, IllegalStateException
	{
		// ensure database connection is available
		if (!connectedToDatabase)
			throw new IllegalStateException("Not connected to database");
		
		// specify query and execute it
		resultSet = statement.executeQuery(query);
		
		// obtain meta data for ResultSet
		metaData = resultSet.getMetaData();
		
		// determine number of rows in ResultSet
		resultSet.last();
		numberOfRows = resultSet.getRow();
		
		// notify JTable that model has changed
		fireTableStructureChanged();
		
	} // end method SetQuery
 
	public void disconnectFromDatabase()
	{
		if (!connectedToDatabase)
			return;
		
		// close Statement and Connection
		try
		{
			statement.close();
			connection.close();
			
		} // end try
		catch (SQLException sqlException)
		{
			sqlException.printStackTrace();
			
		} // end catch
		
		finally // update database connection status
		{
			connectedToDatabase = false;
			
		} // end finally
		
	} // end method disconnectFromDatabase
	
} // end class ResultSetTableModel
 
// Below is the code Im using to make and instance of the above class in the main GUI.java
 
private ResultSetTableModel tableModel = new ResultSetTableModel();
	private JTextArea queryArea;
	
	public GUI()
	{
		super("Displaying Query Results");
		
		// create ResultSetTableModel and display database table
		try
		{
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);

Open in new window

0
spoxoxCommented:
There's a compile problem:

private ResultSetTableModel tableModel = new ResultSetTableModel();
and
tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);

both fail to compile as the only supplied constructor's signature is

ResultSetTableModel(String driver, String url, String username, String password, String query)

0
objectsCommented:
> Alright, got the class in the same package

you shouldn't do that, its unecessary (and undesirable).
you just need to add the import as I mentioned earlier.
Putting everything in the one package defeats the purpse of having packages.
0
CEHJCommented:
You'll either have to supply it or change it to one that is supplied
0
CEHJCommented:
>>you just need to add the import as I mentioned earlier.

You mean as *I* mentioned earlier

>>Putting everything in the one package defeats the purpse of having packages.

No it doesn't. That's how packages work. Often classes are in the same package as other classes
0
objectsCommented:
> How do I find or create the class within the package javax.swing.table.

you don't, you put it in its own package
eg.

package catweb.swing;
0
objectsCommented:
>                   tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);

and chage tha to:

tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, null, null, DEFAULT_QUERY);
0
Carter_MachineryProgrammerAuthor Commented:
Ok, I got the class working now and have only one error preventing from compiling now. With code
tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, null, null, DEFAULT_QUERY);
Im getting the error Unhandled exception type SQLException and down in the console its giving me the message, Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      Unhandled exception type SQLException

      at catWeb.GUI.<init>(GUI.java:26)
      at catWeb.GUI.main(GUI.java:129)
0
objectsCommented:
     public GUI()
      {
            super("Displaying Query Results");
            
            // create ResultSetTableModel and display database table
            try
            {
                  tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, null, null, DEFAULT_QUERY);



                 }
                 catch (SQLException ex)
                  {
                        ex.printStackTrace();
                  }
0
Carter_MachineryProgrammerAuthor Commented:
Thanks for all the help, sorry if I was a pain. I learned alot from this and I appreciate it greatly!!
0
CEHJCommented:
:-)
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.