package examples.jdbc.mssqlserver4;

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


/**
 * This simple example shows the alternatives for making a
 * JDBC connection to a MSSQLServer instance.
 * <p>
 * This example is run on the command line and is output to
 * System.out.
 * <p>
 * Change the hostname and port number used in the code below to match your
 * SQL Server installation.  (Since this example demonstrates several connection methods,
 *  you will need to change this in three places.)
 *
 * @author       Copyright (c) 1997-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class login {
  
  public static void main(String argv[])
       throws Exception
  {
    
    // You can connect to a database with JDBC in various ways.
    // Command line arguments and explicit Properties are both optional.
    
    //
    // Method 1 uses a connection URL that includes the host name
    // and port number of the database server. The user name and
    // password are specified in separate arguments. 
    // Change "myhost:1433" to the host name and port number of your SQL Server.
    //

    Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
    Connection conn1 =
      DriverManager.getConnection("jdbc:weblogic:mssqlserver4:myHost:1433",
                                   "sa",
                                   "secret");
    conn1.close();
    System.out.println("Logged in successfully using Method 1.");

    // 
    // Method 2 uses a Properties object to pass the connection information 
    // and the connection is made with the Driver.connect() method.
    // The optional "appname" and "hostname" properties are passed to MS SQL server
    // and can be read in the sysprocesses table under the column names 
    // "program_name" and "hostname". The hostname value will be prepended with "WebLogic" 
    // Change "myhost:1433" to the host name and port number of your SQL Server.
    //
    Properties props = new Properties();
    props.put("user",     "sa");
    props.put("password", "secret");
    props.put("db", "pubs");
    props.put("server",   "myHost:1433");
    props.put("appname",    "MyApplication");
    props.put("hostname",   "myhostName");
    
    Driver myDriver = (Driver) Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
    Connection conn2 = myDriver.connect("jdbc:weblogic:mssqlserver4", props);
    conn2.close();
    System.out.println("Logged in successfully using Method 2.");
    
    // 
    // Method 3 uses a connection URL with all of the connection information.
    //
    // Change "myhost:1433" to the host name and port number of your SQL Server.
    //
    Connection conn3 = DriverManager.getConnection(
      "jdbc:weblogic:mssqlserver4:myHost:1433?db=pubs;user=sa;password=secret");
    conn3.close();
    
    System.out.println("Logged in successfully using Method 3.");
  }
}



