// File: DBTable.java
// T Balls : March 2001
// JDBC and JTable demo
// To access Oracle at LMU
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
public class DBTable extends JFrame
{ public static void main( String[] args )
{ new DetailsDialog();
}
private String driver = "oracle.jdbc.driver.OracleDriver";
private String dbURL = "jdbc:oracle:thin:@data:1521:DATA1";
private String loginName = "";
private String password ="";
private JTextArea query = new JTextArea( 3, 50 );
private JButton submit = new JButton( "Submit Query" );
public DBTable(String loginNameIn, String passwordIn )
{ super( "JDBC + JTable" );
loginName = loginNameIn;
password = passwordIn;
connect();
getContentPane().add( query, BorderLayout.NORTH );
getContentPane().add( submit, BorderLayout.SOUTH );
submit.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ performQuery();
}
});
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e )
{ System.exit(0);
}
});
pack();
setVisible( true );
}
private Connection con = null;
private Statement stmt = null;
/** connect to database
*/
private void connect()
{ try
{ Class.forName( driver );
con = DriverManager.getConnection( dbURL, loginName, password );
stmt = con.createStatement();
}
catch( ClassNotFoundException cnf )
{ cnf.printStackTrace();
System.exit(0);
}
catch( SQLException e )
{ while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
}
JTable table = null;
JScrollPane scroller = null;
/** send contents of TextArea to database
*/
private void performQuery()
{ ResultSet res = null;
try
{ res = stmt.executeQuery( query.getText() );
}
catch( SQLException e )
{ while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
return;
}
Vector header = null;
Vector rows = null;
try
{ ResultSetMetaData rsmd = res.getMetaData();
int cols = rsmd.getColumnCount();
header = new Vector();
int col;
for( col = 1; col <= cols; col++ )
{ header.add( rsmd.getColumnLabel( col ) );
}
rows = new Vector();
while( res.next() == true )
{ Vector row = new Vector();
for( col=1; col <= cols; col++ )
{ row.add( res.getString( col ) );
}
rows.add( row );
}
}
catch( SQLException e )
{ while( e != null )
{ e.printStackTrace();
e = e.getNextException();
}
return;
}
if( scroller != null )
{ getContentPane().remove( scroller );
}
table = new JTable( rows, header );
scroller = new JScrollPane( table );
table.setPreferredScrollableViewportSize( new Dimension( 300, 200 ) );
getContentPane().add( scroller, BorderLayout.CENTER );
pack();
}
}
class DetailsDialog extends JDialog
{ DetailsDialog()
{ setTitle( "Login" );
loginName = new JTextField( "", 10 );
password = new JPasswordField( "", 10 );
JPanel promptPanel = new JPanel();
promptPanel.setLayout( new GridLayout( 2, 1 ) );
promptPanel.add( new JLabel( "Login ID", JLabel.RIGHT ) );
promptPanel.add( new JLabel( "Password", JLabel.RIGHT ) );
JPanel inputPanel = new JPanel();
inputPanel.setLayout( new GridLayout( 2, 1 ) );
inputPanel.add( loginName );
inputPanel.add( password );
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( promptPanel, BorderLayout.WEST );
getContentPane().add( inputPanel, BorderLayout.EAST );
JButton connect = new JButton( "Connect" );
getContentPane().add( connect, BorderLayout.SOUTH );
connect.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e )
{ new DBTable( getLogin(), getPassword() );
dispose();
}
});
pack();
setVisible( true );
}
private JTextField loginName = null;
private JPasswordField password = null;
public String getLogin()
{ return loginName.getText();
}
public String getPassword()
{ return new String( password.getPassword() );
}
}
|