Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.71 KB | None | 0 0
  1. package jdbc;
  2. import java.sql.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7.  
  8. public class DisplayQuery1 extends JFrame {
  9.    private Connection connection;
  10.    private JTable table;
  11.  
  12.    public DisplayQuery1() //Constructor
  13.    {
  14.       // The URL specifying the local SQL Database server to which
  15.       // this program connects using JDBC to connect to a
  16.       // Microsoft ODBC database.
  17.       // name of our server is SQLEXPRESS2008EXAMPLES
  18.       String url = "jdbc:odbc:SQLSERVER2008EXAMPLES"; //local SQL Server
  19.       String username = "sa";
  20.       String password = "testing123";
  21.  
  22.       // Load the driver to allow connection to the database
  23.       try {
  24.          Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  25.  
  26.          connection = DriverManager.getConnection(
  27.             url, username, password );
  28.       }
  29.       // catch exception if driver is not loaded correctly
  30.       catch ( ClassNotFoundException cnfex ) {
  31.          System.err.println(
  32.             "Failed to load JDBC/ODBC driver." );
  33.          cnfex.printStackTrace();
  34.          System.exit( 1 );  // terminate program
  35.       }
  36.       // catch exception if problem with connection to the database
  37.       catch ( SQLException sqlex ) {
  38.          System.err.println( "Unable to connect" );
  39.          sqlex.printStackTrace();
  40.       }
  41.  
  42. //      getQuery1(); //get the query result
  43. //      getQuery2(); //get the query result
  44. //      getQuery3(); //get the query result
  45. //      getQuery4(); //get the query result
  46.       getQuery5(); //get the query result
  47.  
  48.       setSize( 450, 150 );  // set the size of the window
  49.       show();   //show the window
  50.  
  51.    }
  52.  
  53.    private void getQuery1()
  54.    {
  55.       Statement statement;
  56.       ResultSet resultSet;
  57.  
  58.       try {
  59.         String query = "USE CSC675; SELECT file_name FROM Audio WHERE file_id='3' UNION"
  60.         + " SELECT file_name FROM Video WHERE file_id='3' UNION"
  61.         + " SELECT file_name FROM Pdf WHERE file_id='3' UNION"
  62.         + " SELECT file_name FROM Images WHERE file_id='3';";
  63.          statement = connection.createStatement();
  64.          resultSet = statement.executeQuery( query );
  65.          displayResultSet( resultSet );
  66.          statement.close();
  67.       }
  68.       catch ( SQLException sqlex ) {
  69.          sqlex.printStackTrace();
  70.       }
  71.     }
  72.  
  73.    private void getQuery2()
  74.    {
  75.       Statement statement;
  76.       ResultSet resultSet;
  77.  
  78.       try {
  79.         String query = "USE CSC675; SELECT file_name FROM Audio WHERE displayed_url='url2' UNION "
  80.                 + "SELECT file_name FROM Video WHERE displayed_url='url2' UNION "
  81.                 + "SELECT file_name FROM Pdf WHERE displayed_url='url2' UNION "
  82.                 + "SELECT file_name FROM Images WHERE displayed_url='url2';";
  83.          statement = connection.createStatement();
  84.          resultSet = statement.executeQuery( query );
  85.          displayResultSet( resultSet );
  86.          statement.close();
  87.       }
  88.       catch ( SQLException sqlex ) {
  89.          sqlex.printStackTrace();
  90.       }
  91.    }
  92.  
  93.    private void getQuery3()
  94.    {
  95.       Statement statement;
  96.       ResultSet resultSet;
  97.  
  98.       try {
  99.         String query = "USE CSC675; SELECT file_name FROM Audio WHERE download_date='2010-08-08' AND file_type='audio' UNION "
  100.                 + "SELECT file_name FROM Video WHERE download_date='2010-08-08' AND file_type='audio' UNION "
  101.                 + "SELECT file_name FROM Pdf WHERE download_date='2010-08-08' AND file_type='audio' UNION "
  102.                 + "SELECT file_name FROM Images WHERE download_date='2010-08-08'AND file_type='audio';";
  103.          statement = connection.createStatement();
  104.          resultSet = statement.executeQuery( query );
  105.          displayResultSet( resultSet );
  106.          statement.close();
  107.       }
  108.       catch ( SQLException sqlex ) {
  109.          sqlex.printStackTrace();
  110.       }
  111.    }
  112.  
  113.    private void getQuery4()
  114.    {
  115.       Statement statement;
  116.       ResultSet resultSet;
  117.  
  118.       try {
  119.         String query = "USE CSC675; SELECT distinct i.file_name FROM Images i, Images_keywords ik "
  120.                 + "WHERE i.file_id=ik.file_id and ik.keyword='pdf_keyword1' UNION "
  121.                 + "SELECT distinct v.file_name FROM Video v, Video_keywords vk "
  122.                 + "WHERE v.file_id=vk.file_id and vk.keyword='pdf_keyword1' UNION "
  123.                 + "SELECT distinct p.file_name FROM Pdf p, Pdf_keywords pk "
  124.                 + "WHERE p.file_id=pk.file_id and pk.keyword='pdf_keyword1' UNION "
  125.                 + "SELECT distinct a.file_name FROM Audio a, Audio_keywords ak "
  126.                 + "WHERE a.file_id=ak.file_id and ak.keyword='pdf_keyword1';";
  127.          statement = connection.createStatement();
  128.          resultSet = statement.executeQuery( query );
  129.          displayResultSet( resultSet );
  130.          statement.close();
  131.       }
  132.       catch ( SQLException sqlex ) {
  133.          sqlex.printStackTrace();
  134.       }
  135.    }
  136.  
  137.    private void getQuery5()
  138.    {
  139.       Statement statement;
  140.       ResultSet resultSet;
  141.  
  142.       try {
  143.         String query = "USE CSC675; select (select sum (num_of_entries) from ("
  144.                 + "select cast(count(download_date) as float) from Audio union all "
  145.                 + "select cast(count(download_date) as float) from Pdf union all "
  146.                 + "select cast(count(download_date) as float) from Video union all "
  147.                 + "select cast(count(download_date) as float) from Images) as x (num_of_entries)"
  148.                 + ") / ("
  149.                 + "select datediff (day, min (num_of_days), max(num_of_days))+1 "
  150.                 + "from (select min(mindate) from ("
  151.                 + "select min (download_date) from Audio union all "
  152.                 + "select min(download_date) from Pdf union all "
  153.                 + "select min (download_date) from Video union all "
  154.                 + "select min(download_date) from Images) as x (mindate) union all "
  155.                 + "select max(maxdate) from (select max (download_date) from Audio union all "
  156.                 + "select min(download_date) from Pdf union all "
  157.                 + "select min(download_date) from Video union all "
  158.                 + "select min(download_date) from Images) as x (maxdate)) as x (num_of_days))";
  159.          statement = connection.createStatement();
  160.          resultSet = statement.executeQuery( query );
  161.          displayResultSet( resultSet );
  162.          statement.close();
  163.       }
  164.       catch ( SQLException sqlex ) {
  165.          sqlex.printStackTrace();
  166.       }
  167.    }
  168.  
  169.  
  170.    private void displayResultSet( ResultSet rs )
  171.       throws SQLException
  172.    {
  173.       // position to first record
  174.       boolean moreRecords = rs.next();
  175.  
  176.       // If there are no records, display a message
  177.       if ( ! moreRecords ) {
  178.          JOptionPane.showMessageDialog( this,
  179.             "ResultSet contained no records" );
  180.          setTitle( "No records to display" );
  181.          return;
  182.       }
  183.  
  184.       setTitle( "Car Table from SQL HomeWork" );  //CHANGE THIS!!
  185.  
  186.       Vector columnHeads = new Vector();
  187.       Vector rows = new Vector();
  188.  
  189.       try {
  190.          // get column heads
  191.          ResultSetMetaData rsmd = rs.getMetaData();
  192.  
  193.          for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
  194.             columnHeads.addElement( rsmd.getColumnName( i ) );
  195.  
  196.          // get row data
  197.          do {
  198.             rows.addElement( getNextRow( rs, rsmd ) );
  199.          } while ( rs.next() );
  200.  
  201.          // display table with ResultSet contents
  202.          table = new JTable( rows, columnHeads );
  203.          JScrollPane scroller = new JScrollPane( table );
  204.          getContentPane().add(
  205.             scroller, BorderLayout.CENTER );
  206.          validate();
  207.       }
  208.       catch ( SQLException sqlex ) {
  209.          sqlex.printStackTrace();
  210.       }
  211.    }
  212.  
  213.    private Vector getNextRow( ResultSet rs,
  214.                               ResultSetMetaData rsmd )
  215.        throws SQLException
  216.    {
  217.       Vector currentRow = new Vector();
  218.  
  219.       for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
  220.          switch( rsmd.getColumnType( i ) ) {
  221.             case Types.VARCHAR:
  222.                   currentRow.addElement( rs.getString( i ) );
  223.                break;
  224.             case Types.FLOAT:
  225.                   currentRow.addElement(
  226.                      new Float( rs.getFloat( i ) ) );
  227.                break;
  228.             default:
  229.                System.out.println( "Type was: " +
  230.                   rsmd.getColumnTypeName( i ) );
  231.          }
  232.  
  233.       return currentRow;
  234.    }
  235.  
  236.    public void shutDown()
  237.    {
  238.       try {
  239.          connection.close();
  240.       }
  241.       catch ( SQLException sqlex ) {
  242.          System.err.println( "Unable to disconnect" );
  243.          sqlex.printStackTrace();
  244.       }
  245.    }
  246.  
  247.    public static void main( String args[] )
  248.    {
  249.       final DisplayQuery1 app = new DisplayQuery1();  //CHANGE THIS!
  250.  
  251.       app.addWindowListener(new WindowAdapter() {
  252.             public void windowClosing( WindowEvent e )
  253.             {
  254.                app.shutDown();
  255.                System.exit( 0 );
  256.             }
  257.          }
  258.       );
  259.    }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement