Website Construction Summary (Cont.)

  1. Fetching Data by Using SQL
  2. This step includes the following two programs:

    • Students.cgi, setting up the web environment for Oracle and

    • Students.java, fetching dynamic data from a web server running SQL and returning a JSON object.

    JSON stands for JavaScript Object Notation.

 [
  {
   "Name": "Alfreds Futterkiste",
   "City": "Berlin",
   "Country": "Germany"
  },
  {
   "Name": "Berglunds snabbköp",
   "City": "Luleå",
   "Country": "Sweden"
  },
  {
   "Name": "Island Trading",
   "City": "Cowes",
   "Country": "UK"
  },
  {
   "Name": "Ernst Handel",
   "City": "Graz",
   "Country": "Austria"
  },
  {
   "Name": "FISSA Fabrica Inter.",
   "City": "Madrid",
   "Country": "Spain"  
  }
 ]
    JSON, much like XML, is syntax for storing and exchanging text information. One example of JSON objects is shown as above. The script is used in the file Students.html, where the method JSON.parse parses a string as JSON.

    ~/public_html/cgi-bin/demo/OracleJSON/Students.cgi
    #!/usr/bin/bash
    
    CLASSPATH=.:/usr/lib/oracle/12.1/client64
    CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ojdbc7.jar
    CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ottclasses.zip
    export CLASSPATH
    
    echo "Content-type: text/plain"
    echo ""
    
    /usr/bin/java  Students

    ~/public_html/cgi-bin/demo/OracleJSON/Students.java
     /*********************************************************************
    
      This program shows how to retrieve the student data
        in the Students table.
    
      To use this program, you need to create the table
        Students by using the following commands:
    
      SQL> create table  Students (
        2    Name      varchar(32)  not null,
        3    City      varchar(16)  not null,
        4    Country   varchar(16)  not null );
      Table created.
    
    *******************************************************************/
    
    // Import the following packages to use JDBC.
    import  java.sql.*;
    import  java.io.*;
    import  oracle.jdbc.*;
    import  oracle.jdbc.pool.OracleDataSource;
    
    class  Students {
      public static void  main( String args[ ] ) throws SQLException {
        String user     = "C##user.id";
        String password = "password";
        String database = "65.52.222.73:1521/cdb1";
    
        // Open an OracleDataSource and get a connection.
        OracleDataSource ods = new OracleDataSource( );
        ods.setURL     ( "jdbc:oracle:thin:@" + database );
        ods.setUser    ( user );
        ods.setPassword( password );
        Connection conn = ods.getConnection( );
    
        try {
          // Create, compose, and execute a statement.
          Statement stmt = conn.createStatement( );
          String query = "SELECT name, city, country FROM students";
          ResultSet rset = stmt.executeQuery( query );
    
          // Iterate through the result and save the data.
          String  outp = "[";
          while ( rset.next( ) ) {
            if ( outp != "[" ) outp += ",";
            outp += "{\"Name\":\""   + rset.getString(1) + "\",";
            outp += "\"City\":\""    + rset.getString(2) + "\",";
            outp += "\"Country\":\"" + rset.getString(3) + "\"}";
          }
          outp += "]" ;
          // Print the JSON object outp.
          System.out.println( outp );
          // Close the ResultSet and Statement.
          rset.close( );
          stmt.close( );
        }
        catch ( SQLException ex ) {
          System.out.println( ex );
        }
        // Close the Connection.
        conn.close( );
      }
    }




      That’s your best friend and your worst enemy—your own brain.