package examples.jdbc.mssqlserver4;

import java.sql.*;
import java.util.Properties;

/**
 * This simple example shows how data and result set metadata
 * are selected with JDBC.
 * <p>
 * This example is run on the command line and is output to
 * System.out.
 * <p>
 * Please note that this example uses a simple method for obtaining
 * result sets, update counts, and/or the output parameter values it may
 * generate. This is always possible with simple, known queries.
 * When executing complex or unknown possibly multi-statement SQL
 * or stored procedures, you should use the example in <tt>complexproc.java</tt>
 * to ensure that all results become available.
 *
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in <a
 *href=Package-examples.jdbc.mssqlserver4.html>Package examples.jdbc.mssqlserver4</a>.
 *<p>
 *<li> Change the database URL, login, and password connection
 *parameters to correspond to your Microsoft SQL Server
 *configuration.  If you need more help, check
 *the section on connecting to a database in the Developers Guide, <a
 *href=../../../classdocs/API_jmsq4.html>Using
 *WebLogic jDriver for Microsoft SQL Server</a>.
 *
 *<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ javac -d c:/weblogic/mssqlserver4/classes simpleselect.java</b></font>
 *<p>	
 *
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ java examples.jdbc.mssqlserver4.simpleselect</b></font>
 *
 *</ol>
 *
 * @author    Copyright (c) 1997-2000 by BEA Systems, Inc. All Rights Reserved.
 */

public class simpleselect {
  
  public static void main(String argv[])
       throws Exception
  {
      Properties props = new Properties();
      props.put("user", "sa");
      props.put("password", "secret");
      props.put("server", "myHost:1433");

      Driver myDriver = 
          (Driver)Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
      Connection conn = myDriver.connect("jdbc:weblogic:mssqlserver4", props);
    
    Statement stmt = conn.createStatement();
    
    stmt.execute("select * from sysdatabases");
    
    ResultSet rs = stmt.getResultSet();
    
    while (rs.next()) {
      System.out.println(rs.getString("name") + " - " + 
                         rs.getString("dbid")  + " - " + 
                         rs.getString("crdate"));
    }
    
    ResultSetMetaData rsmd = rs.getMetaData();
    
    System.out.println("Number of Columns: " + rsmd.getColumnCount());
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
      System.out.println("Column Name: "          + rsmd.getColumnName(i));
      System.out.println("Nullable: "             + rsmd.isNullable(i));
      System.out.println("Precision: "            + rsmd.getPrecision(i));
      System.out.println("Scale: "                + rsmd.getScale(i));
      System.out.println("Size: "                 + rsmd.getColumnDisplaySize(i));
      System.out.println("Column Type: "          + rsmd.getColumnType(i));
      System.out.println("Column Type Name: "     + rsmd.getColumnTypeName(i));
      System.out.println("");
    }
    
    rs.close();
    stmt.close();
    conn.close();
  }
}
