Solved

Problems with declaration of type

Posted on 2007-11-20
25
530 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:Carter_Machinery
  • 9
  • 7
  • 7
  • +2
25 Comments
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 20322714
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20322749
You probably need to import ResultSetTableModel into that package
0
 

Author Comment

by:Carter_Machinery
ID: 20323095
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
 
LVL 92

Expert Comment

by:objects
ID: 20323111
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20323135
There are no imports there
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20323146
(of anything other than Sun classes)
0
 
LVL 92

Expert Comment

by:objects
ID: 20323251
> 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
 

Author Comment

by:Carter_Machinery
ID: 20323328
Im not sure I exactly follow you on adding ResultSetTabelModel class to my classpath and then import it specifying its location???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20323381
>>you haven't added the import statement for the ResultSetTableModel as I suggested above

You mean as *I* suggested above ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 20323384
if the class is in package x.y.z
then add to your class


import x.y.z.ResultSetTabelModel;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20323402
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
 

Author Comment

by:Carter_Machinery
ID: 20327170
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Carter_Machinery
ID: 20328407
Update! I have a ResultSetModelTable class built now, Im just a little confused now how I incorporate it with the above code???
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 20328422
Put it in the same package as the one you posted
0
 

Author Comment

by:Carter_Machinery
ID: 20330035
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
 
LVL 11

Expert Comment

by:spoxox
ID: 20331415
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
 
LVL 92

Expert Comment

by:objects
ID: 20331626
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20331630
You'll either have to supply it or change it to one that is supplied
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20331660
>>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
 
LVL 92

Expert Comment

by:objects
ID: 20331661
> 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
 
LVL 92

Expert Comment

by:objects
ID: 20331693
>                   tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL);

and chage tha to:

tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, null, null, DEFAULT_QUERY);
0
 

Author Comment

by:Carter_Machinery
ID: 20350861
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
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 20353611
     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
 

Author Comment

by:Carter_Machinery
ID: 20358038
Thanks for all the help, sorry if I was a pain. I learned alot from this and I appreciate it greatly!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20358084
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now