Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems with declaration of type

Posted on 2007-11-20
25
560 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

765 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