Search code examples
javajtds

Java jTDS Connection Problems Ubuntu Server


From what I have read the correct connection string for a jTDS is:

jdbc:jtds:<server_type>://<server>[:<port>][/<database>]

I believe the issue is the server name. The server name is formatted like this

servername\adhoc

An SQLException gets thrown anytime I try to connect saying "unknown server host name"

Is that my issue, or is there something else I need to look into as well...?

import java.sql.*;

public class Main {

  // The JDBC Connector Class.
  private static final String MSdbClassName = "net.sourceforge.jtds.jdbc.Driver";
  private static final String MSHOST = "servername\\adhoc";  //cascrmeufosqlp1\adhoc
  private static final String MSDATABASE = "tier2";
  private static final String MSUSER = "feed_****2";
  private static final String MSPASSWORD = "*******0";

  public static void main(String[] args) throws ClassNotFoundException,SQLException
  {
    Class.forName(MSdbClassName);
    String url2 = "jdbc:jtds:sqlserver://" + MSHOST + ":1433/" + MSDATABASE;
    Connection c2 = java.sql.DriverManager.getConnection( url2, MSUSER, MSPASSWORD );
    System.out.println("MS SQL works...");
    c2.close();
  }
}

Solution

  • It looks like you are trying to connect to a "named instance" of sql server. You will need to use the "instance" property in the url. Something like this might work:

    jdbc:jtds:sqlserver://servername:1433/dbName;appName=MyAPP;instance=instanceName

    See the jTDS faq for more information found here: http://jtds.sourceforge.net/faq.html