assaultkitty
asked on
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.DB2jDri ver" );
connection = DriverManager.getConnectio n( 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.addAction Listener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
inputQueryTF = new JTextField( 30 );
inputQueryTF.addActionList ener(
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.getSelectedInde x();
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.showMessageDia log( 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
// 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.DB2jDri
connection = DriverManager.getConnectio
} // 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.addAction
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
inputQueryTF = new JTextField( 30 );
inputQueryTF.addActionList
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
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.getSelectedInde
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.showMessageDia
"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
My IDE does not reciognize any EditPanel at all.
Does it come form some library that you are using?
Does it come form some library that you are using?
ASKER
JTextArea is not working. Can you help? I
ASKER
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.DB2jDri ver" );
connection = DriverManager.getConnectio n( 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.addAction Listener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
inputQueryTF = new JTextField( 30 );
inputQueryTF.addActionList ener(
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.getSelectedInde x();
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.showMessageDia log( 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
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.DB2jDri
connection = DriverManager.getConnectio
} // 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.addAction
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
inputQueryTF = new JTextField( 30 );
inputQueryTF.addActionList
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
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.getSelectedInde
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.showMessageDia
"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
>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
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
ASKER
Here is the error message
----jGRASP exec: javac -g DisplayQueryResults.java
DisplayQueryResults.java:1 01: cannot find symbol
symbol : constructor JTextArea(java.sql.Connect ion)
location: class javax.swing.JTextArea
queryArea = new JTextArea( connection );
^
DisplayQueryResults.java:1 05: 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
----jGRASP exec: javac -g DisplayQueryResults.java
DisplayQueryResults.java:1
symbol : constructor JTextArea(java.sql.Connect
location: class javax.swing.JTextArea
queryArea = new JTextArea( connection );
^
DisplayQueryResults.java:1
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
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
This should not have warnings, although as CEHJ
correctly mentioned, warnings are not very important
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
ASKER
This is the error message after trying to connect to the database.
----jGRASP exec: javac -g DisplayQueryResults.java
DisplayQueryResults.java:4 2: unreported exception java.lang.InstantiationExc eption; must be caught or declared to be thrown
Class.forName(driver).newI nstance();
^
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:33 06/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).newI nstance();
connection = DriverManager.getConnectio n(
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.addAction Listener(n ew ActionListener() {
public void actionPerformed(ActionEven t e) {
getTable();
}
});
inputQueryTF = new JTextField(30);
inputQueryTF.addActionList ener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
try {
String query = inputQueryTF.getText();
statement = connection.createStatement ();
resultSet = statement.executeQuery(que ry);
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(inputQueryT F);
inputPanel.add(submitInput Query);
//queryArea = new JTextArea(connection);
//CEHJ
queryArea = new JTextArea();
JPanel instructionPanel = new JPanel();
instructionPanel.setLayout (new GridLayout(2, 1));
instructionPanel.add(input Panel);
instructionPanel.add(query Area);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.add(instructionPa nel, 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.getSelectedInde x();
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(que ry);
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.showMessageDia log(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(rsm d.getColum nName(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.g etString(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
----jGRASP exec: javac -g DisplayQueryResults.java
DisplayQueryResults.java:4
Class.forName(driver).newI
^
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:33
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).newI
connection = DriverManager.getConnectio
DATABASE_URL, "deitel", "deitel" );
} // end try
catch (ClassNotFoundException cnfex) {
System.err.println("Failed
cnfex.printStackTrace();
System.exit(1); // terminate program
} // end catch
catch (SQLException sqlex) {
System.err.println("Unable
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.addAction
public void actionPerformed(ActionEven
getTable();
}
});
inputQueryTF = new JTextField(30);
inputQueryTF.addActionList
public void actionPerformed(ActionEven
try {
String query = inputQueryTF.getText();
statement = connection.createStatement
resultSet = statement.executeQuery(que
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(inputQueryT
inputPanel.add(submitInput
//queryArea = new JTextArea(connection);
//CEHJ
queryArea = new JTextArea();
JPanel instructionPanel = new JPanel();
instructionPanel.setLayout
instructionPanel.add(input
instructionPanel.add(query
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.add(instructionPa
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.getSelectedInde
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(que
displayResultSet(resultSet
} // end try
catch (SQLException sqlex) {
sqlex.printStackTrace();
} // end catch
} // end method getTable
public void displayResultSet(ResultSet
// position to first record
boolean moreRecords = rs.next();
// If there are no records, display a message
if (!moreRecords) {
JOptionPane.showMessageDia
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(rsm
// get row data
do {
rows.addElement(getNextRow
} 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.g
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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.InstantiationExc eption; must be caught or declared to be thrown
> Class.forName(driver).newI nstance();
and this has not yet any connection to your databse - it is not yet running - it is just compilation error
>----jGRASP exec: javac -g DisplayQueryResults.java
>DisplayQueryResults.java:
> Class.forName(driver).newI
and this has not yet any connection to your databse - it is not yet running - it is just compilation error
assaultkitty, having fixed your compile error, can you tell me why you ignored my comment?
Use JTextArea perhaps