Need help with program

States that Editpanel is a deprecated API. Compile error.

// DisplayQueryResults.java

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

public class DisplayQueryResults extends JFrame
{
      private Connection connection;
      private Statement statement;
      private ResultSet resultSet;
      private ResultSetMetaData rsMetaData;
      private JTable table;
      private JComboBox inputQuery;
      private JButton submitInputQuery;
      private JTextField inputQueryTF;
      private EditPanel editPanel;
      
      public DisplayQueryResults()
      {
            super( "Select Query. Click Submit to See Results." );
            
            // The URL specifying the books database to which
            // this program connects to
            String url = "jdbc:db2j:books";
      
            // Load the driver to allow connection to the database
            try
            {
                  Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
                  
                  connection = DriverManager.getConnection( url );
            } // end try
            
            catch ( ClassNotFoundException cnfex )
            {
                  System.err.println( "Failed to load JDBC/ODBC driver." );
                  cnfex.printStackTrace();
                  System.exit( 1 ); // terminate program
            } // end catch
            
            catch ( SQLException sqlex )
            {
                  System.err.println( "Unable to connect" );
                  sqlex.printStackTrace();
                  System.exit( 1 ); // terminate program
            } // end catch
            
            // If connected to database, set up GUI
            String queryNames[] =
            {
                  "All authors", "All publishers",
                  "All books", "A specific author", "A specific publisher" };
                  inputQuery = new JComboBox( queryNames );
                  JLabel inputLabel = new JLabel("Query");
                  submitInputQuery = new JButton( "Submit Query" );
                  submitInputQuery.addActionListener(
                  
                  new ActionListener()
                  {
                        public void actionPerformed( ActionEvent e )
                        {
                              getTable();
                        }
                  }
      );
            inputQueryTF = new JTextField( 30 );
            inputQueryTF.addActionListener(
            new ActionListener()
            {
                  public void actionPerformed( ActionEvent e )
                  {
                        try
                        {
                              String query = inputQueryTF.getText();
                              statement = connection.createStatement();
                              resultSet = statement.executeQuery( query );
                              displayResultSet( resultSet );
                        } // end try
                        catch ( SQLException sqlex )
                        {
                              sqlex.printStackTrace();
                        } // end catch
                  }
            }
      );
      JPanel inputPanel = new JPanel();
      inputPanel.setLayout( new GridLayout( 5, 1 ) );
      inputPanel.add( inputLabel );
      inputPanel.add( inputQuery);
      inputPanel.add( new JLabel( "Enter query, author or publisher:" ) );
      inputPanel.add( inputQueryTF );
      inputPanel.add( submitInputQuery);
      editPanel = new EditPanel( connection );
      JPanel instructionPanel = new JPanel();
      instructionPanel.setLayout( new GridLayout( 2, 1 ) );
      instructionPanel.add( inputPanel );
      instructionPanel.add( editPanel.getThePanel() );
      JPanel topPanel = new JPanel();
      topPanel.setLayout( new BorderLayout() );
      topPanel.add( instructionPanel, BorderLayout.NORTH );
      table = new JTable( 4, 4 );
      Container c = getContentPane();
      c.setLayout( new BorderLayout() );
      c.add( topPanel, BorderLayout.NORTH );
      c.add( table, BorderLayout.CENTER );
      getTable();
      setSize( 500, 500 );
      show();
} // end constructor DisplayQueryResults

public Connection getConnection() { return connection; }
private void getTable()
{
      try
      {
            int selection = inputQuery.getSelectedIndex();
            String query = null;
            switch ( selection )
            {
                  case 0:
                  query = "SELECT * FROM Authors";
                  break;
                  case 1:
                  query = "SELECT * FROM Publishers";
                  break;
                  case 2:
                  query = "SELECT * FROM TITLES";
                  break;
                  case 3:
                  query = "SELECT Authors.LastName, Authors.FirstName, "+
                  "Titles.Title, Titles.Price, " + "Titles.ISBN FROM " +
                  "Titles INNER JOIN (AuthorISBN INNER JOIN Authors ON " +
                  "AuthorISBN.AuthorID = Authors.AuthorID) ON " +
                  "Titles.ISBN = AuthorISBN.ISBN WHERE Authors.LastName" +
                  " = '" + inputQueryTF.getText() +
                  "' ORDER BY Authors.LastName, Authors.FirstName ASC";
                  break;
                  case 4:
                  query = "SELECT Publishers.PublisherName, Titles.Title, " +
                  "Titles.Price, Titles.ISBN FROM Titles INNER JOIN " +
                  "Publishers ON Publishers.PublisherID = " +
                  "Titles.PublisherID WHERE Publishers.PublisherName = '"
                  + inputQueryTF.getText() + "' ORDER BY Titles.Title ASC";
                  break;
                  } // end switch

                  statement = connection.createStatement();
                  resultSet = statement.executeQuery( query );
                  displayResultSet( resultSet );
                  } // end try
            catch ( SQLException sqlex )
            {
                  sqlex.printStackTrace();
            } // end catch
} // end method getTable
public void displayResultSet( ResultSet rs ) throws SQLException
{
      // position to first record
      boolean moreRecords = rs.next();
      // If there are no records, display a message
      if ( !moreRecords )
      {
            JOptionPane.showMessageDialog( this,
            "ResultSet contained no records" );
            setTitle( "No records to display" );
            return;
      }
            Vector columnHeads = new Vector();
            Vector rows = new Vector();
            try
            {
                  // get column heads
                  ResultSetMetaData rsmd = rs.getMetaData();
                  for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
                  columnHeads.addElement( rsmd.getColumnName( i ) );
                  // get row data
                  do
                  {
                        rows.addElement( getNextRow( rs, rsmd ) );
                  } while ( rs.next() );
                  // display table with ResultSet contents
                  table = new JTable( rows, columnHeads );
                  JScrollPane scroller = new JScrollPane( table );
                  Container c = getContentPane();
                  c.remove( 1 );
                  c.add( scroller, BorderLayout.CENTER );
                  c.validate();
            } // end try
            catch ( SQLException sqlex )
            {
                  sqlex.printStackTrace();
            }
} // end method displayResultSet
      private Vector getNextRow( ResultSet rs,
      ResultSetMetaData rsmd ) throws SQLException
      {
            Vector currentRow = new Vector();
            
            for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
            switch( rsmd.getColumnType( i ) )
            {
                  case Types.VARCHAR:
                  case Types.LONGVARCHAR:
                  currentRow.addElement( rs.getString( i ) );
                  break;
                  case Types.INTEGER:
                  currentRow.addElement( new Long( rs.getLong( i ) ) );
                  break;
                  case Types.REAL:
                  currentRow.addElement( new Float( rs.getDouble( i ) ) );
                  break;
                  default:
                  System.out.println( "Type was: " +
                  rsmd.getColumnTypeName( i ) );
            } // end switch
            return currentRow;
} // end method getNextRow
      public void shutDown()
      {
            try
            {
                  connection.close();
            } // end catch
            catch ( SQLException sqlex )
            {
                  System.err.println( "Unable to disconnect" );
                  sqlex.printStackTrace();
            } // end catch
      } // end method shutDown
      public static void main( String args[] )
      {
            final DisplayQueryResults app = new DisplayQueryResults();
            app.addWindowListener(new WindowAdapter() {
            
            public void windowClosing( WindowEvent e )
            {
                  app.shutDown();
                  System.exit( 0 );
            } // end method windowClosing
} // end method main
);
}
} // end class DisplayQueryResults
assaultkittyAsked:
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.

for_yanCommented:
What is EditPanel ?

Use JTextArea perhaps
0
for_yanCommented:
My IDE does not reciognize  any EditPanel at all.
Does it come form some library that you are using?
0
assaultkittyAuthor Commented:
JTextArea is not working.  Can you help?  I
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

assaultkittyAuthor Commented:
These are the changes. Still compilation errors

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

public class DisplayQueryResults extends JFrame
{
      private Connection connection;
      private Statement statement;
      private ResultSet resultSet;
      private ResultSetMetaData rsMetaData;
      private JTable table;
      private JComboBox inputQuery;
      private JButton submitInputQuery;
      private JTextField inputQueryTF;
      private JTextArea queryArea;
      
      public DisplayQueryResults()
      {
            super( "Select Query. Click Submit to See Results." );
            
            // The URL specifying the books database to which
            // this program connects to
            String url = "jdbc:db2j:books";
      
            // Load the driver to allow connection to the database
            try
            {
                  Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
                  
                  connection = DriverManager.getConnection( url );
            } // end try
            
            catch ( ClassNotFoundException cnfex )
            {
                  System.err.println( "Failed to load JDBC/ODBC driver." );
                  cnfex.printStackTrace();
                  System.exit( 1 ); // terminate program
            } // end catch
            
            catch ( SQLException sqlex )
            {
                  System.err.println( "Unable to connect" );
                  sqlex.printStackTrace();
                  System.exit( 1 ); // terminate program
            } // end catch
            
            // If connected to database, set up GUI
            String queryNames[] =
            {
                  "All authors", "All publishers",
                  "All books", "A specific author", "A specific publisher" };
                  inputQuery = new JComboBox( queryNames );
                  JLabel inputLabel = new JLabel("Query");
                  submitInputQuery = new JButton( "Submit Query" );
                  submitInputQuery.addActionListener(
                  
                  new ActionListener()
                  {
                        public void actionPerformed( ActionEvent e )
                        {
                              getTable();
                        }
                  }
      );
            inputQueryTF = new JTextField( 30 );
            inputQueryTF.addActionListener(
            new ActionListener()
            {
                  public void actionPerformed( ActionEvent e )
                  {
                        try
                        {
                              String query = inputQueryTF.getText();
                              statement = connection.createStatement();
                              resultSet = statement.executeQuery( query );
                              displayResultSet( resultSet );
                        } // end try
                        catch ( SQLException sqlex )
                        {
                              sqlex.printStackTrace();
                        } // end catch
                  }
            }
      );
      JPanel inputPanel = new JPanel();
      inputPanel.setLayout( new GridLayout( 5, 1 ) );
      inputPanel.add( inputLabel );
      inputPanel.add( inputQuery);
      inputPanel.add( new JLabel( "Enter query, author or publisher:" ) );
      inputPanel.add( inputQueryTF );
      inputPanel.add( submitInputQuery);
      queryArea = new JTextArea( connection );
      JPanel instructionPanel = new JPanel();
      instructionPanel.setLayout( new GridLayout( 2, 1 ) );
      instructionPanel.add( inputPanel );
      instructionPanel.add( queryArea.getTheArea() );
      JPanel topPanel = new JPanel();
      topPanel.setLayout( new BorderLayout() );
      topPanel.add( instructionPanel, BorderLayout.NORTH );
      table = new JTable( 4, 4 );
      Container c = getContentPane();
      c.setLayout( new BorderLayout() );
      c.add( topPanel, BorderLayout.NORTH );
      c.add( table, BorderLayout.CENTER );
      getTable();
      setSize( 500, 500 );
      show();
} // end constructor DisplayQueryResults

public Connection getConnection() { return connection; }
private void getTable()
{
      try
      {
            int selection = inputQuery.getSelectedIndex();
            String query = null;
            switch ( selection )
            {
                  case 0:
                  query = "SELECT * FROM Authors";
                  break;
                  case 1:
                  query = "SELECT * FROM Publishers";
                  break;
                  case 2:
                  query = "SELECT * FROM TITLES";
                  break;
                  case 3:
                  query = "SELECT Authors.LastName, Authors.FirstName, "+
                  "Titles.Title, Titles.Price, " + "Titles.ISBN FROM " +
                  "Titles INNER JOIN (AuthorISBN INNER JOIN Authors ON " +
                  "AuthorISBN.AuthorID = Authors.AuthorID) ON " +
                  "Titles.ISBN = AuthorISBN.ISBN WHERE Authors.LastName" +
                  " = '" + inputQueryTF.getText() +
                  "' ORDER BY Authors.LastName, Authors.FirstName ASC";
                  break;
                  case 4:
                  query = "SELECT Publishers.PublisherName, Titles.Title, " +
                  "Titles.Price, Titles.ISBN FROM Titles INNER JOIN " +
                  "Publishers ON Publishers.PublisherID = " +
                  "Titles.PublisherID WHERE Publishers.PublisherName = '"
                  + inputQueryTF.getText() + "' ORDER BY Titles.Title ASC";
                  break;
                  } // end switch

                  statement = connection.createStatement();
                  resultSet = statement.executeQuery( query );
                  displayResultSet( resultSet );
                  } // end try
            catch ( SQLException sqlex )
            {
                  sqlex.printStackTrace();
            } // end catch
} // end method getTable
public void displayResultSet( ResultSet rs ) throws SQLException
{
      // position to first record
      boolean moreRecords = rs.next();
      // If there are no records, display a message
      if ( !moreRecords )
      {
            JOptionPane.showMessageDialog( this,
            "ResultSet contained no records" );
            setTitle( "No records to display" );
            return;
      }
            Vector columnHeads = new Vector();
            Vector rows = new Vector();
            try
            {
                  // get column heads
                  ResultSetMetaData rsmd = rs.getMetaData();
                  for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
                  columnHeads.addElement( rsmd.getColumnName( i ) );
                  // get row data
                  do
                  {
                        rows.addElement( getNextRow( rs, rsmd ) );
                  } while ( rs.next() );
                  // display table with ResultSet contents
                  table = new JTable( rows, columnHeads );
                  JScrollPane scroller = new JScrollPane( table );
                  Container c = getContentPane();
                  c.remove( 1 );
                  c.add( scroller, BorderLayout.CENTER );
                  c.validate();
            } // end try
            catch ( SQLException sqlex )
            {
                  sqlex.printStackTrace();
            }
} // end method displayResultSet
      private Vector getNextRow( ResultSet rs,
      ResultSetMetaData rsmd ) throws SQLException
      {
            Vector currentRow = new Vector();
            
            for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
            switch( rsmd.getColumnType( i ) )
            {
                  case Types.VARCHAR:
                  case Types.LONGVARCHAR:
                  currentRow.addElement( rs.getString( i ) );
                  break;
                  case Types.INTEGER:
                  currentRow.addElement( new Long( rs.getLong( i ) ) );
                  break;
                  case Types.REAL:
                  currentRow.addElement( new Float( rs.getDouble( i ) ) );
                  break;
                  default:
                  System.out.println( "Type was: " +
                  rsmd.getColumnTypeName( i ) );
            } // end switch
            return currentRow;
} // end method getNextRow
      public void shutDown()
      {
            try
            {
                  connection.close();
            } // end catch
            catch ( SQLException sqlex )
            {
                  System.err.println( "Unable to disconnect" );
                  sqlex.printStackTrace();
            } // end catch
      } // end method shutDown
      public static void main( String args[] )
      {
            final DisplayQueryResults app = new DisplayQueryResults();
            app.addWindowListener(new WindowAdapter() {
            
            public void windowClosing( WindowEvent e )
            {
                  app.shutDown();
                  System.exit( 0 );
            } // end method windowClosing
} // end method main
);
}
} // end class DisplayQueryResults
0
for_yanCommented:
>JTextArea is not working
in waht sense?

I cannot compile your code and I don't have databses ,etc.

If you coudl make a shorter class which still exhibits your prioblem, I can try ti help you.

Try to remove the parts not relevant to your error in a copy of this code
and then post mauch smaller self-sufficient snippet which I can try to compile and execute
0
assaultkittyAuthor Commented:
Here is the error message

 ----jGRASP exec: javac -g DisplayQueryResults.java

DisplayQueryResults.java:101: cannot find symbol
symbol  : constructor JTextArea(java.sql.Connection)
location: class javax.swing.JTextArea
      queryArea = new JTextArea( connection );
                  ^
DisplayQueryResults.java:105: cannot find symbol
symbol  : method getTheArea()
location: class javax.swing.JTextArea
      instructionPanel.add( queryArea.getTheArea() );
                                     ^
Note: DisplayQueryResults.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: DisplayQueryResults.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Books.sql
0
CEHJCommented:
Try the following. The deprecation stuff is just warnings
import java.awt.*;
import java.awt.event.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;


public class DisplayQueryResults extends JFrame {
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private ResultSetMetaData rsMetaData;
    private JTable table;
    private JComboBox inputQuery;
    private JButton submitInputQuery;
    private JTextField inputQueryTF;
    private JTextArea queryArea;

    public DisplayQueryResults() {
        super("Select Query. Click Submit to See Results.");

        // The URL specifying the books database to which
        // this program connects to
        String url = "jdbc:db2j:books";

        // Load the driver to allow connection to the database
        try {
            Class.forName("com.ibm.db2j.jdbc.DB2jDriver");

            connection = DriverManager.getConnection(url);
        } // end try

        catch (ClassNotFoundException cnfex) {
            System.err.println("Failed to load JDBC/ODBC driver.");
            cnfex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        catch (SQLException sqlex) {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        // If connected to database, set up GUI
        String[] queryNames = {
                "All authors", "All publishers", "All books",
                "A specific author", "A specific publisher"
            };
        inputQuery = new JComboBox(queryNames);

        JLabel inputLabel = new JLabel("Query");
        submitInputQuery = new JButton("Submit Query");
        submitInputQuery.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    getTable();
                }
            });
        inputQueryTF = new JTextField(30);
        inputQueryTF.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        String query = inputQueryTF.getText();
                        statement = connection.createStatement();
                        resultSet = statement.executeQuery(query);
                        displayResultSet(resultSet);
                    } // end try
                    catch (SQLException sqlex) {
                        sqlex.printStackTrace();
                    } // end catch
                }
            });

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(5, 1));
        inputPanel.add(inputLabel);
        inputPanel.add(inputQuery);
        inputPanel.add(new JLabel("Enter query, author or publisher:"));
        inputPanel.add(inputQueryTF);
        inputPanel.add(submitInputQuery);
        //queryArea = new JTextArea(connection);
	//CEHJ
        queryArea = new JTextArea();

        JPanel instructionPanel = new JPanel();
        instructionPanel.setLayout(new GridLayout(2, 1));
        instructionPanel.add(inputPanel);
        instructionPanel.add(queryArea);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(instructionPanel, BorderLayout.NORTH);
        table = new JTable(4, 4);

        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(topPanel, BorderLayout.NORTH);
        c.add(table, BorderLayout.CENTER);
        getTable();
        setSize(500, 500);
        show();
    } // end constructor DisplayQueryResults

    public Connection getConnection() {
        return connection;
    }

    private void getTable() {
        try {
            int selection = inputQuery.getSelectedIndex();
            String query = null;

            switch (selection) {
                case 0:
                    query = "SELECT * FROM Authors";

                    break;

                case 1:
                    query = "SELECT * FROM Publishers";

                    break;

                case 2:
                    query = "SELECT * FROM TITLES";

                    break;

                case 3:
                    query = "SELECT Authors.LastName, Authors.FirstName, " +
                        "Titles.Title, Titles.Price, " + "Titles.ISBN FROM " +
                        "Titles INNER JOIN (AuthorISBN INNER JOIN Authors ON " +
                        "AuthorISBN.AuthorID = Authors.AuthorID) ON " +
                        "Titles.ISBN = AuthorISBN.ISBN WHERE Authors.LastName" +
                        " = '" + inputQueryTF.getText() +
                        "' ORDER BY Authors.LastName, Authors.FirstName ASC";

                    break;

                case 4:
                    query = "SELECT Publishers.PublisherName, Titles.Title, " +
                        "Titles.Price, Titles.ISBN FROM Titles INNER JOIN " +
                        "Publishers ON Publishers.PublisherID = " +
                        "Titles.PublisherID WHERE Publishers.PublisherName = '" +
                        inputQueryTF.getText() + "' ORDER BY Titles.Title ASC";

                    break;
            } // end switch

            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);
            displayResultSet(resultSet);
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        } // end catch
    } // end method getTable

    public void displayResultSet(ResultSet rs) throws SQLException {
        // position to first record
        boolean moreRecords = rs.next();

        // If there are no records, display a message
        if (!moreRecords) {
            JOptionPane.showMessageDialog(this, "ResultSet contained no records");
            setTitle("No records to display");

            return;
        }

        Vector columnHeads = new Vector();
        Vector rows = new Vector();

        try {
            // get column heads
            ResultSetMetaData rsmd = rs.getMetaData();

            for (int i = 1; i <= rsmd.getColumnCount(); ++i)
                columnHeads.addElement(rsmd.getColumnName(i));

            // get row data
            do {
                rows.addElement(getNextRow(rs, rsmd));
            } while (rs.next());

            // display table with ResultSet contents
            table = new JTable(rows, columnHeads);

            JScrollPane scroller = new JScrollPane(table);
            Container c = getContentPane();
            c.remove(1);
            c.add(scroller, BorderLayout.CENTER);
            c.validate();
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    } // end method displayResultSet

    private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)
        throws SQLException {
        Vector currentRow = new Vector();

        for (int i = 1; i <= rsmd.getColumnCount(); ++i)
            switch (rsmd.getColumnType(i)) {
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                    currentRow.addElement(rs.getString(i));

                    break;

                case Types.INTEGER:
                    currentRow.addElement(new Long(rs.getLong(i)));

                    break;

                case Types.REAL:
                    currentRow.addElement(new Float(rs.getDouble(i)));

                    break;

                default:
                    System.out.println("Type was: " +
                        rsmd.getColumnTypeName(i));
            } // end switch

        return currentRow;
    } // end method getNextRow

    public void shutDown() {
        try {
            connection.close();
        } // end catch
        catch (SQLException sqlex) {
            System.err.println("Unable to disconnect");
            sqlex.printStackTrace();
        } // end catch
    } // end method shutDown

    public static void main(String[] args) {
        final DisplayQueryResults app = new DisplayQueryResults();
        app.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    app.shutDown();
                    System.exit(0);
                } // end method windowClosing
            } // end method main
        );
    }
} // end class DisplayQueryResults

Open in new window

0
for_yanCommented:
This should not have warnings, although as CEHJ
correctly mentioned, warnings are not very important

import java.awt.*;
import java.awt.event.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;


public class DisplayQueryResults extends JFrame {
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    //private ResultSetMetaData rsMetaData;
    private JTable table;
    private JComboBox inputQuery;
    private JButton submitInputQuery;
    private JTextField inputQueryTF;
    private JTextArea queryArea;

    public DisplayQueryResults() {
        super("Select Query. Click Submit to See Results.");

        // The URL specifying the books database to which
        // this program connects to
        String url = "jdbc:db2j:books";

        // Load the driver to allow connection to the database
        try {
            Class.forName("com.ibm.db2j.jdbc.DB2jDriver");

            connection = DriverManager.getConnection(url);
        } // end try

        catch (ClassNotFoundException cnfex) {
            System.err.println("Failed to load JDBC/ODBC driver.");
            cnfex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        catch (SQLException sqlex) {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        // If connected to database, set up GUI
        String[] queryNames = {
                "All authors", "All publishers", "All books",
                "A specific author", "A specific publisher"
            };
        inputQuery = new JComboBox(queryNames);

        JLabel inputLabel = new JLabel("Query");
        submitInputQuery = new JButton("Submit Query");
        submitInputQuery.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    getTable();
                }
            });
        inputQueryTF = new JTextField(30);
        inputQueryTF.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        String query = inputQueryTF.getText();
                        statement = connection.createStatement();
                        resultSet = statement.executeQuery(query);
                        displayResultSet(resultSet);
                    } // end try
                    catch (SQLException sqlex) {
                        sqlex.printStackTrace();
                    } // end catch
                }
            });

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(5, 1));
        inputPanel.add(inputLabel);
        inputPanel.add(inputQuery);
        inputPanel.add(new JLabel("Enter query, author or publisher:"));
        inputPanel.add(inputQueryTF);
        inputPanel.add(submitInputQuery);
             queryArea = new JTextArea();

        JPanel instructionPanel = new JPanel();
        instructionPanel.setLayout(new GridLayout(2, 1));
        instructionPanel.add(inputPanel);
        instructionPanel.add(queryArea);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(instructionPanel, BorderLayout.NORTH);
        table = new JTable(4, 4);

        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(topPanel, BorderLayout.NORTH);
        c.add(table, BorderLayout.CENTER);
        getTable();
        setSize(500, 500);
        this.setVisible(true);
    } // end constructor DisplayQueryResults

    public Connection getConnection() {
        return connection;
    }

    private void getTable() {
        try {
            int selection = inputQuery.getSelectedIndex();
            String query = null;

            switch (selection) {
                case 0:
                    query = "SELECT * FROM Authors";

                    break;

                case 1:
                    query = "SELECT * FROM Publishers";

                    break;

                case 2:
                    query = "SELECT * FROM TITLES";

                    break;

                case 3:
                    query = "SELECT Authors.LastName, Authors.FirstName, " +
                        "Titles.Title, Titles.Price, " + "Titles.ISBN FROM " +
                        "Titles INNER JOIN (AuthorISBN INNER JOIN Authors ON " +
                        "AuthorISBN.AuthorID = Authors.AuthorID) ON " +
                        "Titles.ISBN = AuthorISBN.ISBN WHERE Authors.LastName" +
                        " = '" + inputQueryTF.getText() +
                        "' ORDER BY Authors.LastName, Authors.FirstName ASC";

                    break;

                case 4:
                    query = "SELECT Publishers.PublisherName, Titles.Title, " +
                        "Titles.Price, Titles.ISBN FROM Titles INNER JOIN " +
                        "Publishers ON Publishers.PublisherID = " +
                        "Titles.PublisherID WHERE Publishers.PublisherName = '" +
                        inputQueryTF.getText() + "' ORDER BY Titles.Title ASC";

                    break;
            } // end switch

            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);
            displayResultSet(resultSet);
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        } // end catch
    } // end method getTable

    public void displayResultSet(ResultSet rs) throws SQLException {
        // position to first record
        boolean moreRecords = rs.next();

        // If there are no records, display a message
        if (!moreRecords) {
            JOptionPane.showMessageDialog(this, "ResultSet contained no records");
            setTitle("No records to display");

            return;
        }

        Vector <String>columnHeads = new Vector<String>();
        Vector <Vector>rows = new Vector<Vector>();

        try {
            // get column heads
            ResultSetMetaData rsmd = rs.getMetaData();

            for (int i = 1; i <= rsmd.getColumnCount(); ++i)
                columnHeads.addElement(rsmd.getColumnName(i));

            // get row data
            do {
                rows.addElement(getNextRow(rs, rsmd));
            } while (rs.next());

            // display table with ResultSet contents
            table = new JTable(rows, columnHeads);

            JScrollPane scroller = new JScrollPane(table);
            Container c = getContentPane();
            c.remove(1);
            c.add(scroller, BorderLayout.CENTER);
            c.validate();
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    } // end method displayResultSet

    private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)
        throws SQLException {
        Vector<Object> currentRow = new Vector<Object>();

        for (int i = 1; i <= rsmd.getColumnCount(); ++i)
            switch (rsmd.getColumnType(i)) {
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                    currentRow.addElement(rs.getString(i));

                    break;

                case Types.INTEGER:
                    currentRow.addElement(new Long(rs.getLong(i)));

                    break;

                case Types.REAL:
                    currentRow.addElement(new Float(rs.getDouble(i)));

                    break;

                default:
                    System.out.println("Type was: " +
                        rsmd.getColumnTypeName(i));
            } // end switch

        return currentRow;
    } // end method getNextRow

    public void shutDown() {
        try {
            connection.close();
        } // end catch
        catch (SQLException sqlex) {
            System.err.println("Unable to disconnect");
            sqlex.printStackTrace();
        } // end catch
    } // end method shutDown

    public static void main(String[] args) {
        final DisplayQueryResults app = new DisplayQueryResults();
        app.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    app.shutDown();
                    System.exit(0);
                } // end method windowClosing
            } // end method main
        );
    }
} // end class DisplayQueryResults

Open in new window

0
assaultkittyAuthor Commented:
This is the error message after trying to connect to the database.

 ----jGRASP exec: javac -g DisplayQueryResults.java

DisplayQueryResults.java:42: unreported exception java.lang.InstantiationException; must be caught or declared to be thrown
                   Class.forName(driver).newInstance();
                                                    ^
Note: DisplayQueryResults.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: DisplayQueryResults.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

 ----jGRASP wedge2: exit code f


import java.awt.*;
import java.awt.event.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;


public class DisplayQueryResults extends JFrame {
   
       static final String DATABASE_URL = "jdbc:mysql://localhost:3306/books";
   
       private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private ResultSetMetaData rsMetaData;
    private JTable table;
    private JComboBox inputQuery;
    private JButton submitInputQuery;
    private JTextField inputQueryTF;
    private JTextArea queryArea;
   
      
    public DisplayQueryResults() {
        super("Select Query. Click Submit to See Results.");

        // The driver specifying the books database to which
        // this program connects to
          String driver = "com.mysql.jdbc.Driver";

        // Load the driver to allow connection to the database
        try {
                   Class.forName(driver).newInstance();
                        
                              connection = DriverManager.getConnection(
                               DATABASE_URL, "deitel", "deitel" );

        } // end try

        catch (ClassNotFoundException cnfex) {
            System.err.println("Failed to load JDBC/ODBC driver.");
            cnfex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        catch (SQLException sqlex) {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        // If connected to database, set up GUI
        String[] queryNames = {
                "All authors", "All publishers", "All books",
                "A specific author", "A specific publisher"
            };
        inputQuery = new JComboBox(queryNames);

        JLabel inputLabel = new JLabel("Query");
        submitInputQuery = new JButton("Submit Query");
        submitInputQuery.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    getTable();
                }
            });
        inputQueryTF = new JTextField(30);
        inputQueryTF.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        String query = inputQueryTF.getText();
                        statement = connection.createStatement();
                        resultSet = statement.executeQuery(query);
                        displayResultSet(resultSet);
                    } // end try
                    catch (SQLException sqlex) {
                        sqlex.printStackTrace();
                    } // end catch
                }
            });

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(5, 1));
        inputPanel.add(inputLabel);
        inputPanel.add(inputQuery);
        inputPanel.add(new JLabel("Enter query, author or publisher:"));
        inputPanel.add(inputQueryTF);
        inputPanel.add(submitInputQuery);
        //queryArea = new JTextArea(connection);
      //CEHJ
        queryArea = new JTextArea();

        JPanel instructionPanel = new JPanel();
        instructionPanel.setLayout(new GridLayout(2, 1));
        instructionPanel.add(inputPanel);
        instructionPanel.add(queryArea);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(instructionPanel, BorderLayout.NORTH);
        table = new JTable(4, 4);

        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(topPanel, BorderLayout.NORTH);
        c.add(table, BorderLayout.CENTER);
        getTable();
        setSize(500, 500);
        show();
    } // end constructor DisplayQueryResults

    public Connection getConnection() {
        return connection;
    }

    private void getTable() {
        try {
            int selection = inputQuery.getSelectedIndex();
            String query = null;

            switch (selection) {
                case 0:
                    query = "SELECT * FROM Authors";

                    break;

                case 1:
                    query = "SELECT * FROM Publishers";

                    break;

                case 2:
                    query = "SELECT * FROM TITLES";

                    break;

                case 3:
                    query = "SELECT Authors.LastName, Authors.FirstName, " +
                        "Titles.Title, Titles.Price, " + "Titles.ISBN FROM " +
                        "Titles INNER JOIN (AuthorISBN INNER JOIN Authors ON " +
                        "AuthorISBN.AuthorID = Authors.AuthorID) ON " +
                        "Titles.ISBN = AuthorISBN.ISBN WHERE Authors.LastName" +
                        " = '" + inputQueryTF.getText() +
                        "' ORDER BY Authors.LastName, Authors.FirstName ASC";

                    break;

                case 4:
                    query = "SELECT Publishers.PublisherName, Titles.Title, " +
                        "Titles.Price, Titles.ISBN FROM Titles INNER JOIN " +
                        "Publishers ON Publishers.PublisherID = " +
                        "Titles.PublisherID WHERE Publishers.PublisherName = '" +
                        inputQueryTF.getText() + "' ORDER BY Titles.Title ASC";

                    break;
            } // end switch

            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);
            displayResultSet(resultSet);
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        } // end catch
    } // end method getTable

    public void displayResultSet(ResultSet rs) throws SQLException {
        // position to first record
        boolean moreRecords = rs.next();

        // If there are no records, display a message
        if (!moreRecords) {
            JOptionPane.showMessageDialog(this, "ResultSet contained no records");
            setTitle("No records to display");

            return;
        }

        Vector columnHeads = new Vector();
        Vector rows = new Vector();

        try {
            // get column heads
            ResultSetMetaData rsmd = rs.getMetaData();

            for (int i = 1; i <= rsmd.getColumnCount(); ++i)
                columnHeads.addElement(rsmd.getColumnName(i));

            // get row data
            do {
                rows.addElement(getNextRow(rs, rsmd));
            } while (rs.next());

            // display table with ResultSet contents
            table = new JTable(rows, columnHeads);

            JScrollPane scroller = new JScrollPane(table);
            Container c = getContentPane();
            c.remove(1);
            c.add(scroller, BorderLayout.CENTER);
            c.validate();
        } // end try
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    } // end method displayResultSet

    private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)
        throws SQLException {
        Vector currentRow = new Vector();

        for (int i = 1; i <= rsmd.getColumnCount(); ++i)
            switch (rsmd.getColumnType(i)) {
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                    currentRow.addElement(rs.getString(i));

                    break;

                case Types.INTEGER:
                    currentRow.addElement(new Long(rs.getLong(i)));

                    break;

                case Types.REAL:
                    currentRow.addElement(new Float(rs.getDouble(i)));

                    break;

                default:
                    System.out.println("Type was: " +
                        rsmd.getColumnTypeName(i));
            } // end switch

        return currentRow;
    } // end method getNextRow

    public void shutDown() {
        try {
            connection.close();
        } // end catch
        catch (SQLException sqlex) {
            System.err.println("Unable to disconnect");
            sqlex.printStackTrace();
        } // end catch
    } // end method shutDown

    public static void main(String[] args) {
        final DisplayQueryResults app = new DisplayQueryResults();
        app.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    app.shutDown();
                    System.exit(0);
                } // end method windowClosing
            } // end method main
        );
    }
} // end class DisplayQueryResults


Books.sql
0
for_yanCommented:
change this
    catch (SQLException sqlex)

to
 catch(Exception sqlex)
0
for_yanCommented:
You can change this whiolle code:

catch (ClassNotFoundException cnfex) {
            System.err.println("Failed to load JDBC/ODBC driver.");
            cnfex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

        catch (SQLException sqlex) {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
            System.exit(1); // terminate program
        } // end catch

Open in new window


to

catch(Exception ex) {
ex.printStackTrace();

}

Open in new window

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
for_yanCommented:
You really don't need that granularity - printStackTrace()
will print you which execpteption happened and at which line
these things only cause errors
0
for_yanCommented:
>This is the error message after trying to connect to the database.

 >----jGRASP exec: javac -g DisplayQueryResults.java

>DisplayQueryResults.java:42: unreported exception java.lang.InstantiationException; must be caught or declared to be thrown
  >                 Class.forName(driver).newInstance();


and this has not yet any connection to your databse - it is not yet running - it is just compilation error
0
CEHJCommented:
assaultkitty, having fixed your compile error, can you tell me why you ignored my comment?
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.