Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testing_sql_server;
- import java.sql.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- public class ExtraCredit1 extends JFrame {
- private Connection connection;
- private JTable table;
- public ExtraCredit1() //Constructor
- {
- // The URL specifying the local SQL Database server to which
- // this program connects using JDBC to connect to a
- // Microsoft ODBC database.
- // name of our server is SQLEXPRESS2008EXAMPLES
- String url = "jdbc:odbc:SQLEXPRESS2008EXAMPLES"; //local SQL Server
- String username = "sa";
- String password = "testing123";
- // Load the driver to allow connection to the database
- try {
- Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
- connection = DriverManager.getConnection(
- url, username, password );
- }
- // catch exception if driver is not loaded correctly
- catch ( ClassNotFoundException cnfex ) {
- System.err.println(
- "Failed to load JDBC/ODBC driver." );
- cnfex.printStackTrace();
- System.exit( 1 ); // terminate program
- }
- // catch exception if problem with connection to the database
- catch ( SQLException sqlex ) {
- System.err.println( "Unable to connect" );
- sqlex.printStackTrace();
- }
- getTable(); //get the query result
- setSize( 450, 150 ); // set the size of the window
- show(); //show the window
- }
- private void getTable()
- {
- Statement statement;
- ResultSet resultSet;
- try {
- String query = "select sr.name, sr.phone from Sales_Rep sr, Car c where sr.name = c.seller and c.color = 'green';";
- 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,
- "ResultSet contained no records" );
- setTitle( "No records to display" );
- return;
- }
- setTitle( "Car Table from SQL HomeWork" ); //CHANGE THIS!!
- 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 );
- 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 )
- switch( rsmd.getColumnType( i ) ) {
- case Types.VARCHAR:
- currentRow.addElement( rs.getString( i ) );
- break;
- case Types.INTEGER:
- currentRow.addElement(
- new Long( rs.getLong( i ) ) );
- break;
- default:
- System.out.println( "Type was: " +
- rsmd.getColumnTypeName( i ) );
- }
- return currentRow;
- }
- public void shutDown()
- {
- try {
- connection.close();
- }
- catch ( SQLException sqlex ) {
- System.err.println( "Unable to disconnect" );
- sqlex.printStackTrace();
- }
- }
- public static void main( String args[] )
- {
- final ExtraCredit1 app = new ExtraCredit1(); //CHANGE THIS!
- app.addWindowListener(new WindowAdapter() {
- public void windowClosing( WindowEvent e )
- {
- app.shutDown();
- System.exit( 0 );
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement