package com.mindprod.example;
import static java.lang.System.err;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* TestSQLServer: demonstrate the how to connect to a Microsoft SQL Server Database with JBDC.
* <p/>
* composed with IntelliJ IDEA
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-09-17
*/
@SuppressWarnings( { "WeakerAccess", "UnusedDeclaration" } )
public class TestSQLServer
{
/**
* 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";
/**
* basic URL to access the database
*/
@SuppressWarnings( { "ConstantNamingConvention" } )
private static final String URL = "jdbc:microsoft:sqlserver://";
/**
* login name of user
*/
private static final String USERNAME = "charlie";
/**
* The connection. Handle to the database
*/
@SuppressWarnings( { "FieldCanBeLocal", "UnusedDeclaration" } )
private Connection conn;
/**
* glue parts of connection URL together.
*
* @return complete URL to access database.
*/
private static String getConnectionUrl()
{
return URL
+ SERVERNAME
+ ":"
+ PORTNUMBER
+ ";DATABASENAME="
+ DATABASENAME
+ ";SELECTMETHOD="
+ SELECTMETHOD
+ ";";
}
/**
* connect to the database
*/
private void init()
{
try
{
Class.forName( DRIVERCLASSNAME );
}
catch ( Exception e )
{
err.println( "can't load SQL Server driver: " + e.getMessage() );
}
try
{
conn =
DriverManager.getConnection( getConnectionUrl(),
USERNAME,
PASSWORD );
}
catch ( SQLException e )
{
err.println( "can't connect to SQL Server: " + e.getMessage() );
}
}
}