Java Reference
In-Depth Information
The javax.naming.InitialContext class is used to communicate with the
naming service, and its lookup() method is used to look up a name in the nam-
ing service. The following DataSourceDemo program demonstrates the steps
involved in connecting to a database using the DataSource class.
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
public class DataSourceDemo
{
public static void main(String [] args)
{
String dsn = args[0];
System.out.println(“Attempting to connect to “ + dsn);
try
{
System.out.println(“Initializing the naming context...”);
InitialContext init = new InitialContext();
System.out.println(“Looking up “ + dsn);
DataSource source = (DataSource) init.lookup(dsn);
System.out.println(“Establishing a connection...”);
Connection connection = source.getConnection();
System.out.println(“Connect to “
+ connection.getCatalog() + “ a success!”);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
When using the DataSource class to connect to a database, you do not typi-
cally load a JDBC driver; however, if you need to, you can. The drivers are
typically specified either from a command-line property or a properties file
managed by the application server or naming service tool you are using.
An SQL Primer
Structured Query Language (SQL) is a standardized language that allows you
to perform operations on a database, such as creating entries, reading content,
updating content, and deleting entries. SQL is supported by most any data-
base you will likely use, and it allows you to write database code indepen-
dently of the underlying database.
Search WWH ::




Custom Search