# Connecting to SQL Server using Java

## 1. Setting up an ODBC data source

- Open the Control Panel from Start => Settings => Control Panel. If host OS is W2k, click on Administrative Tools
- Click on ODBC Data Sources. In user DSN (the default) click Add to register a new source
- Choose the SQL Server driver from the list and click Finish. Note that if you were trying to connect to Oracle, you
- would choose the Oracle ODBC driver.

- Enter the name of the new data source and the server. Choose a name that you will remember.

- Choose SQL Server authentication and provide the user name and password for default settings (this needs to be modified if password is changed later)

- In the next two windows the defaults can be used. Click Finish in the last window. In the resulting server setup, test the settings for the data source, by clicking on the "Test Data Source ..." button..

## 2. Connecting to the SQL server using java

- The sample java source code for connecting to the ODBC data source is provided here.
- Modify the query in the code to be suitable to your database (create tables etc. in the your database and form a query based on it).
- Compile using JDK. Run the application. It prompts for the name of the data source, the user name and the password. It outputs the result of the query (or a stack trace if there is an exception).