title: Java JDBC JTDS test connection string
body: <p>I have an application using JTDS to connect to SQL Server.  I need to change the database and want to test the connection string first before reconfiguring the application.  I'm a SQL Server DBA, not a Java developer!
  Here's my test code:</p>
  
  <pre><code>// Import the SQL Server JDBC Driver classes 
  import java.sql.*; 
  class Example 
  {  
     public static void main(String args[]) 
     {  
     try  
     { 
          // Build the connection string, and get a connection 
          System.out.println("1."); 
          System.out.println("2."); 
          String connectionUrl = "jdbc:jtds:sqlserver://UK-SB-Server:53569;DatabaseName=helpdesk;user=helpdesk;password=MyPwd;Tds=8.0;PrepareSql=3;XaEmulation=false";
          System.out.println("3."); 
          Connection con = DriverManager.getConnection(connectionUrl); 
          System.out.println("Connected."); 
  
          // Create and execute an SQL statement that returns some data.  
          String SQL = "SELECT * from dbo.AllowedValues";  
          Statement stmt = con.createStatement();  
          ResultSet rs = stmt.executeQuery(SQL); 
  
          // Iterate through the data in the result set and display it.  
          while (rs.next())  
          {  
             System.out.println(rs.getString(1) + " " + rs.getString(2));  
          } 
     }  
     catch(Exception e)  
     { 
          System.out.println("Error - " + e.getMessage()); 
          System.exit(0);  
     } 
  } 
  }
  </code></pre>
  
  <p>I compile it with:</p>
  
  <pre><code>C:\Progra~1\Java\jdk1.6.0_45\bin\javac C:\JavaTest\example.java
  </code></pre>
  
  <p>I run it with:</p>
  
  <pre><code>C:\Progra~1\Java\jdk1.6.0_45\bin\java -classpath C:\JavaTest Example
  </code></pre>
  
  <p>jtds-1.2.jar and Example.class are both in C:\JavaTest</p>
  
  <p>I get the following error:</p>
  
  <pre><code>1.
  2.
  3.
  Error - No suitable driver found for jdbc:jtds:sqlserver://UK-SB-Server:53569;DatabaseName=helpdesk;user=helpdesk;password=MyPwd;Tds=8.0;PrepareSql=3;XaEmulation=false
  </code></pre>
  
  <p>I've read conflicting posts as to whether I need </p>
  
  <pre><code>Class.forName("net.sourceforge.jtds.jdbc.Driver");
  </code></pre>
  
  <p>or not.  If I put the line between <code>println("1.")</code> and <code>println("2.")</code>, it just fails earlier with </p>
  
  <pre><code>1.
  Error - net.sourceforge.jtds.jdbc.Driver
  </code></pre>
  
  <p>I may be missing something obvious, but please help me resolve this issue.</p>