How to Connect a jTDS Driver to SQL Express

Written by leena kudalkar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Connect a jTDS Driver to SQL Express
Client-server connectivity is crucial to enterprise applications. (pc connecting e-mail handdrawn image by patrimonio designs from Fotolia.com)

A driver is a system utility that provides an interface to a hardware or software device (in this case, a JDBC engine) from another entity in the system (SQL Server Express). You must find and use an appropriate driver for a particular database server. jTDS is an open-source JDBC 3.0 driver for Microsoft SQL Server (6.5, 7.0, 2000 and 2005). Connecting a JDBC tool like the jTDS driver to SQL Server Express involves three basic steps: acquire the JDBC driver; set up TCP/IP for SQL Server Express; and set the authentication method for connecting to SQL Server.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Download and install jTDS JDBC driver for SQL Server if you find it missing. See Resources for the hyperlink to the SourceForge website's download page. You will know that the driver is not being used if you get this exception: "Unable to find driver: net.sourceforge.jtds.jdbc.Driver."

  2. 2

    Enable TCP/IP for SQL Server Express as shown below. By default, TCP/IP is disabled, in which case the JDBC engine cannot connect to it and gives the following error: "Network error IOException: Connection refused: connect." Expand SQL Server 2005 Network Configuration node. In the right pane, select "Protocols for SQL Express." It will show protocols and their statuses. Select "Enable" for TCP/IP.

  3. 3

    Select "Properties" from the TCP/IP context menu.

  4. 4

    Select the "IP Addresses" tab in the "Properties" dialogue box.

  5. 5

    Set the TCP Dynamic Ports field to blank (empty). As a result, SQL Server Express will not choose a port automatically at restart.

  6. 6

    Enter the correct port number in the TCP Port field under IPAll node.

  7. 7

    Press "OK" to save and apply these settings.

  8. 8

    Restart SQL Server Express, and start a command window.

  9. 9

    Type "netstat -an". If your port number is, say, 5000, it should show up in the output of this command.

    TCP 0.0.0.0:5000(your port no.) 0.0.0.0:0 LISTENING

    This is a test that the new port number is being used.

  1. 1

    Prepare to enable SQL Server Authentication Mode for logins if you get the following error with the default Windows Authentication mode: "Login failed for user '<User name>'. The user is not associated with a trusted SQL Server connection."

    You may have to create a new user or enable logging in with this mode for the current one, as shown below.

  2. 2

    Start Microsoft SQL Server Management Studio Express (SSMSE), and connect to SQL Server Express.

  3. 3

    Right-click "Properties" from your database's context menu in the Object Explorer window.

  4. 4

    Select the "Security" tab in the "Server Properties" dialogue box, and check the "SQL Server and Windows Authentication Mode" check box.

  5. 5

    Press "OK" in the "Properties" dialogue box to save these settings.

  6. 6

    Pull up "Security / Logins" in the Object Explorer window, and select the existing system login "sa."

  7. 7

    Select "Status" page in the "Login Properties" dialogue box that appears if you see a red down arrow against the login "sa." A red arrow means that either login with this mode is not enabled for "sa," or there is no password.

  8. 8

    Select the "Login: Enabled" radio button.

  9. 9

    Go to the General page on the same "Properties" dialogue, and enter a password for "sa." The password should be fairly cryptic as this is the system administrator.

  10. 10

    Press "OK" to save these changes to the dialogue box. Note: If you refresh the Object Explorer page now, there should be no red down arrow against "sa."

  11. 11

    Write Java source code in your client application to connect to SQL Server Express now, using jTDS driver for JDBC. Here is a sample piece of test code:

    import java.sql.*;

    public class test Connection

    {

        public static void main(String[] args) 

        {

            DB db = new DB();

            db.dbConnect(

         "jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");

        }

    }

    class DB

    {

        public DB() {}

        public voidn dbConnect(String db_connect_string, 

      String db_userid, String db_password)

        {

            try

            {

                Class.forName("net.sourceforge.jtds.jdbc.Driver");

                Connection conn = DriverManager.getConnection(

        db_connect_string, db_userid, db_password);

                System.out.println("connected");

                

            }

            catch (Exception e)

            {

                e.printStackTrace();

            }

        }

    };

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.