package examples.jdbc.mssqlserver4;

import java.sql.*;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

/** This applet simply connects to a database. It is useful for
 * diagnosing problems with database connections from applets, such as
 * Java security manager restrictions and incorrect connection URLs or
 * CLASSPATHs.  
 * <p> 

 * The ConnectionApplet.java source file contains very little
 * executable code, but is is heavily commented; if you have problems
 * running this applet, be sure to read the comments in this source
 * file. Also, try running the <a
 * href="examples.jdbc.mssqlserver4.SocketApplet.html#_top_">
 * SocketApplet</a> example to test that you open a socket on the
 * database server host machine. 
 *
 *<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> In the <font face="Courier New" size=-1>ConnectionApplet.java</font>
 *file, 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 ConnectionApplet.java</b></font>
 *<p>	
 *
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ appletviewer ConnectionApplet.html</b></font>
 *
 *</ol>
 *
 */
public class ConnectionApplet extends Applet
{
  String url = "jdbc:weblogic:mssqlserver4:myhost:1433";
  String login = "sa";    // Change to your database login
  String password = "";   // Change to your password

  public void init()
  {
    try
      {
        // The following line of code will try to load the driver for
        // Microsoft SQL Server by name. For this line to work you
        // need to make sure you included the proper version of the
        // libraries in your CLASSPATH.

        // When the class is loaded, it calls DriverManager to
        // register the drivers with JDBC.

        // Some implementations of the Java VM, like the version built
        // into Internet Explorer 3, fail to call the static code. In
        // this case, just loading the class won't register it. You
        // can get around this by creating an instance of the class,
        // example: Class.forName().newInstance().

        // If this line fails: 

        // - check your classpath if running locally
        
        // - make sure your web server is serving the
        // weblogic/jdbc/mssqlserver directory

        // - if running remotely, read your development environment's
        // instructions to find out how to add a library to your
        // classpath

        // - use your debugger to step through the code and see where
        // it fails.

        // Some versions of Netscape Navigator and Internet Explorer
        // will also raise a security exception here. This is because
        // DriverManager.registerDriver() is trying to read the
        // properties file (which is not allowed for applets) and the
        // browser is complaining.

        Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();

        // The following line tries opening a JDBC connection with
        // your database using the given URL (which specifies the host
        // and port number), login and password.

        // When you call getConnection the DriverManager scans all
        // previously loaded drivers and asks each of them if they can
        // handle the given URL. The right driver then attempts to
        // connect with the database and, if successful returns a
        // Connection. There are several reasons why this could fail:

        // Your driver was not registered with DriverManager. This
        // returns an error like: "No suitable driver was found."
        // Make sure you're using java.sql.DriverManager with JDK
        // 1.1.x.  If this still doesn't work, try registering the
        // driver explicitly using
        // DriverManager.registerDriver(driver).

        // You get a security exception. If you're running within a
        // browser, the Java security manager will only allow your
        // applet to connect to the host it was served from. If your
        // web server and your database server are two different hosts
        // and you run in a browser you have a problem. You either
        // must install the web server and database server on the same
        // machine, or use a three-tier solution such as WebLogic JDBC.

        // You get a security exception even though your web and
        // database servers are on the same machine. If you call your
        // web server by name (e.g., server1), and your database
        // server by IP number (e.g., 220.128.2.3) the Java VM may not
        // recognize that these refer to the same machine.  Make sure
        // you use a consistent name in your .html file and in the
        // JDBC URL.

        // Your driver cannot connect with the database. Make sure your database
        // is available on the given host/port (use SocketApplet to test). Check
        // that the database is one of the supported versions. If there is
        // a firewall between you and the database, make sure that your firewall allows
        // connections to the database using its native protocol, or use the security
        // proxy with HTTP tunneling.

        // Your driver connects but the database rejects the
        // connection.  Make sure you specified a valid login and
        // password. If you need to connect to a particular database
        // on your server, make sure you specify that in the URL
        // too. For example:

        // "jdbc:weblogic:mssqlserver4:customers@localhost:1433?user=sa;password=".


        Connection connection = DriverManager.getConnection(url,login,password);

        message = "Connection was opened with " + url;  

        connection.close();  
      }

    catch(ClassNotFoundException ex)
      {
        message = "Exception: Driver wasn't found, check classpath: " + ex;
      }

    catch(Exception ex)
      {
        message = "Exception while opening the connection: " + ex;

        // Print the stack trace which shows where the problem is
        ex.printStackTrace(System.out);

      }

    catch(Error err)
      {
        message = "Error while opening the connection: " + err;

        // Print the stack trace which shows where the problem is
        err.printStackTrace(System.out);                

      }

    System.out.println(message);

    repaint();

    // If you made it to here and everything worked you're in
    // business, congratulations! If you have a problem, you read all
    // the instructions and the applet still doesn't work, write to
    // support@weblogic.com.

    // Please send us a short email describing your development
    // environment, platform, version of WebLogic jDriver for Microsoft SQL Server (make
    // sure you're using the latest) and the output that was generated
    // by this applet (in particular the stack trace). We usually
    // answer your questions within one or two days.
  }

  public void paint(Graphics graphics)
  {
    graphics.drawString(message,10,20);
  }

  String message = "";
}
