/*
 * [ConnectJDBCSQLServer.java]
 *
 * Summary: demonstrate the how to connect to a Microsoft SQL Server Database with JBDC.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2007-09-17
 */
package com.mindprod.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import static java.lang.System.*;

/**
 * demonstrate the how to connect to a Microsoft SQL Server Database with JBDC.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-09-17
 * @since 2007-09-17
 */
@SuppressWarnings( { "WeakerAccess", "UnusedDeclaration" } )
public class ConnectJDBCSQLServer
    {
    /**
     * which database
     */
    private static final String DATABASENAME = "squirrels";

    /**
     * class name of the JDBC driver
     */
    private static final String DRIVERCLASSNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

    /**
     * access PASSWORD
     */
    private static final String PASSWORD = "sesame";

    /**
     * TCP/IP port number to access database
     */
    private static final String PORTNUMBER = "1433";

    /**
     * Informs the driver to use server a side-cursor, which permits more than one active statement on a connection.
     */
    private static final String SELECTMETHOD = "cursor";

    /**
     * which server hosts the database
     */
    private static final String SERVERNAME = "localhost";

    /**
     * login name of user
     */
    private static final String USERNAME = "charlie";

    /**
     * The connection.  Handle to the database
     */
    private static Connection conn;

    /**
     * connect to the database
     */
    private static Connection connect() throws SQLException
        {
        try
            {
            Class.forName( DRIVERCLASSNAME );
            }
        catch ( Exception e )
            {
            err.println( "can't load SQL Server driver: " + e.getMessage() );
            }
        return DriverManager.getConnection( "jdbc:microsoft:sqlserver://"
                                            + SERVERNAME
                                            + ":"
                                            + PORTNUMBER
                                            + ";DATABASENAME="
                                            + DATABASENAME
                                            + ";SELECTMETHOD="
                                            + SELECTMETHOD
                                            + ";",
                USERNAME,
                PASSWORD
        );
        }

    /**
     * initialise the database
     *
     * @param args not used
     */
    public static void main( String[] args ) throws SQLException
        {
        conn = connect();
        // ...
        conn.close();
        }
    }