package examples.jdbc.mssqlserver4;

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

/**
 * This simple example shows how JDBC can be used to execute DDL and
 * DML.
 * <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 records.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.records</b></font>
 *
 *</ol>
 *
 * @author         Copyright (c) 1997-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class records {
  
  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();
    
    // Create temporary table
    stmt.execute("create table #empdemo(empid int, name char(30), dept int)");
    
    // Insert 10 records
    System.out.println("Inserting 10 records...");
    String inssql = "insert into #empdemo(empid, name, dept) values (?, ?, ?)";
    PreparedStatement pstmt = conn.prepareStatement(inssql);
    
    for (int i = 0; i < 10; i++) {
      pstmt.setInt(1, i);
      pstmt.setString(2, "Person " + i);
      pstmt.setInt(3, i);
      pstmt.execute();
    }
    
    pstmt.close();
    
    // Update 10 records
    System.out.println("Updating 10 records...");
    String updsql = "update #empdemo set dept = dept + ? where empid = ?";
    PreparedStatement pstmt2 = conn.prepareStatement(updsql);
    
    for (int i = 0; i < 100; i++) {
      pstmt2.setInt(1, i);
      pstmt2.setInt(2, i);
      pstmt2.execute();
    }
    
    // Get the current contents and print them out
    ResultSet rs = stmt.executeQuery("select * from #empdemo");
    while (rs.next())
      {
	System.out.println( rs.getInt(1) + ", "
                       + rs.getString(2) + ", "
                       + rs.getInt(3));
      }
    pstmt2.close();
    stmt.close();
    conn.close();
  }
}

