Example Application Using SQL/JDBC

/* @(#) ExampleApp.java ver 1.0 00/08/21 14:43:49
 * ???.java, initial version May 2000
 *
 * Copyright (c) 2000 NEC Software Services (UK) Limited.
 * All Rights Reserved.
// Import the required java classes and interfaces
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DataTruncation;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.io.*;
public class ExampleApp {
     public static void main(String args[]) {
           Connection con = null;
           Statement stmt = null;
           ResultSet rs = null;
           Properties info = new Properties();
           String driver = "com.northgateis.reality.realsql.RealSQLDriver";
           String url = "jdbc:realsql://magpie:1203/TESTDBASE";
           // Initialise the connection properties
           info.put("user", "ajt");
           info.put("password", "a23Fgh"); //
           info.put("account", "SQL-TEST");
           info.put("accountpwd", "");
           PrintWriter pw = new PrintWriter(System.out);
           DriverManager.setLogWriter(pw);
           ////// REGISTER THE DRIVER AND MAKE A CONNECTION //////
           try {
                // Register the driver and connect to the database
              Class.forName(driver);
                con = DriverManager.getConnection(url, info);
           }
           catch( SQLException e ) {
               System.out.println("Failed to connect to database: " + e.getMessage());
               return;
           }
           catch( ClassNotFoundException e ) {
               System.out.println("Unable to find driver class.");
               return;
           }
           ////// CREATE A STATEMENT AND EXECUTE AN SQL QUERY //////
           try {
               stmt = con.createStatement();
               rs = stmt.executeQuery("SELECT * FROM EMP");
           }
           catch (SQLException sqle) {
               System.out.println("ERROR: " + sqle.getMessage());
           }
         ////// FETCH AND DISPLAY THE RESULTSET //////
         try {
           // Print column labels, padded to correct display size.
           for (int c=1; c<=rs.getMetaData().getColumnCount(); ++c)
           {
              byte[] buf = new byte[rs.getMetaData().getColumnDisplaySize(c) + 1];
              System.arraycopy(rs.getMetaData().getColumnLabel(c).getBytes(),
                    0, buf, 0, rs.getMetaData().getColumnLabel(c).length());
              if (rs.getMetaData().getColumnLabel(c).length() < buf.length)
              {
                  int i;
                  for (i=rs.getMetaData().getColumnLabel(c).length(); i<buf.length; i++
                  {
                      buf[i] = (byte)0x2E; // pad with "."s
                  }
                  buf[i-1] = (byte)0x20; // overwrite last byte with space " "
              }
              System.out.write(buf,0,buf.length);
            }
            System.out.println();
            int numRows =0;
            // Print column data for all rows using column number.
            while (rs.next())
            {
                for (int c=1; c<=rs.getMetaData().getColumnCount(); ++c)
                {
                     String cname = rs.getMetaData().getColumnName(c);
                     String value = rs.getString(c);
                     
                     if (rs.wasNull())
                           value = "";
                     byte[] buf = new byte[rs.getMetaData().getColumnDisplaySize(c) + 1];
                     System.arraycopy(value.getBytes(), 0, buf, 0, value.length());
                     if (value.length() < buf.length)
                     {
                         for (int i=value.length();i<buf.length;i++)
                         {
                             buf[i] = (byte)0x20;
                         }
                     }
                     System.out.write(buf,0,buf.length);
             }
             numRows++;
             System.out.println();
        }
        System.out.println(numRows + " Rows listed.");
   }
   catch (SQLException sqle) {
         System.out.println("ERROR: " + sqle.getMessage());
   }
   try {
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (con != null) con.close();
   }
   catch (SQLException sqle) { 
        System.out.println("ERROR: " + sqle.getMessage());
 }
}
}