/**
The Table2 program invokes a JDBC SQL query, assuming the
ORACLE database listener at the UB site.
Program usage is:
> java Table2 <user> <password> ["<query>"]
example:
> java Table2 joe x45-12 "select * from MYTABLE"
the query needs to be double quoted if it is sent as a parameter
*/
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Table2 extends JFrame {
private String user, password;
private Connection connection;
private JTable table;
public Table2(Properties props) {
// get user & password properties
user = props.getProperty("USER");
password = props.getProperty("PASSWORD");
// Load the driver to allow connection to the database
try {
Class.forName ("oracle.jdbc.driver.OracleDriver"); // find the class
connection= DriverManager.getConnection("jdbc:oracle:thin:"+ "@cpe.bridgeport.edu:1521:csdb",
user, password); }
catch ( ClassNotFoundException cnfex) {
System.err.println("Failed to load JDBC driver.");
cnfex.printStackTrace();
System.exit( 1 ); }
catch ( SQLException sqlex ) {
System.err.println ( "Unable to connect" );
sqlex.printStackTrace(); }
execQuery(props.getProperty("QUERY"));
setSize (450, 150 );
show();
} // TableDisplay
private void execQuery(String query)
{
Statement statement;
ResultSet resultSet;
try {
// feel free to set your own query on the next line
if (query == "")
query = "select * from ALL_SYNONYMS";
statement = connection.createStatement();
resultSet = statement.executeQuery ( query );
displayResultSet ( resultSet );
statement.close();
} catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private 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 ,"No records" );
setTitle( "No records to display" );
return; }
setTitle( "My Title" );
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 );
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroller = new JScrollPane ( table );
getContentPane().add (scroller, BorderLayout.CENTER );
validate();
} catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private Vector getNextRow ( ResultSet rs,ResultSetMetaData rsmd ) throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i) {
String string = rs.getString(i);
if(string == null)
string = "NULL";
currentRow.addElement(string); }
return currentRow; }
public void shutDown()
{
try {
connection.close(); }
catch ( SQLException e ) {
e.printStackTrace();
}
}
public static void main ( String args[] ) {
if(args.length < 2) {
System.err.println("Usage: % java Table2 <user> <password> [\"<query>\"]");
System.exit(1); }
Properties props = new Properties();
props.setProperty("USER", args[0]);
props.setProperty("PASSWORD", args[1]);
if (args.length == 3)
props.setProperty("QUERY", args[2]);
else
props.setProperty("QUERY", "");
final Table2 app = new Table2(props);
app.addWindowListener(new WindowAdapter() {
public void windowClosing ( WindowEvent e ) {
app.shutDown();
System.exit( 0 );}}); } }