Create a jTDS connection string
As detailed in the jTDS Frequenlty Asked Questions, the URL format for jTDS is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
So, to connect to a database called "Blog" hosted by a MS SQL Server running on MYPC
, you may end up with something like this:
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS;user=sa;password=s3cr3t
Or, if you prefer to use getConnection(url, "sa", "s3cr3t")
:
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS
EDIT: Regarding your Connection refused
error, double check that you're running SQL Server on port 1433, that the service is running and that you don't have a firewall blocking incoming connections.
jTDS connection string: connect to a MS SQL Server instance with a backslash
Did further research and tests. Found out the correct connection URL string in this case is:
jdbc:jtds:sqlserver://server-name/database_name;instance=instance_name
In my case, the connection string is:
jdbc:jtds:sqlserver://server-name/MSSQL-DB09v1;instance=v1
See jTDS FAQs for more details
Java JDBC JTDS test connection string
You appear to be facing two issues:
Issue 1. It seems that jTDS 1.2 is sufficiently old that you actually do need to call
Class.forName("net.sourceforge.jtds.jdbc.Driver");
before you try to establish the connection.
Issue 2. When you specify the classpath, you need to explicitly include the jTDS jar file. That is, this won't work ...
C:\JavaTest>"\Program Files\Java\jdk1.6.0_45\bin\java.exe" -cp C:/JavaTest Example
1.
Error - net.sourceforge.jtds.jdbc.Driver
... but this works for me:
C:\JavaTest>"\Program Files\Java\jdk1.6.0_45\bin\java.exe" -cp C:/JavaTest;C:/JavaTest/jtds-1.2.jar Example
1.
2.
3.
Connected.
...
How to make use of Trusted_connection property with jTDS driver?
As described in the file README.SSO, you need not specify any credentials.
In order to set up the jTDS driver to use Windows Single Sign On, users
will have to connect without providing a user name and password. This will
only work on Windows.Driver example:
// No user name or password in URL or as parameters
DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/LionKing");
You need not specify the useNTLMv2 parameter either, if you do it determines the version of NTLM that will be used, not IF NTLM will be used or not.
SQL Server connection using JDBC - JTDS
FYI, LoginTimeout
sets how long you wait to establish a connection. Once connected it has no effect. It looks like the jTDS
drive implements socketTimeout which I think should cover your condition.
They also implement a socketKeepAlive
. I am not familiar with how they implement it but I assume the poll the connection to either keep it open and\or return an exception if it is dropped. I don't think jTDS
offers this but some connection failover functionality might be useful as well.
A summary of one implementation
Related Topics
Jdbc Connection to Mssql Server in Windows Authentication Mode
Change Location of Log4J.Properties
Java Class.Cast() VS. Cast Operator
Byte Array and Int Conversion in Java
Printing My MAC's Serial Number in Java Using Unix Commands
Mockito: Invaliduseofmatchersexception
Programmatically Change Log Level in Log4J2
In Java, How to Efficiently and Elegantly Stream a Tree Node's Descendants
Wrap the String After a Number of Characters Word-Wise in Java
How to Correctly Use Custom Renderers to Paint Specific Cells in a Jtable
Java.Lang.Outofmemoryerror: Java Heap Space in Maven
Recursively List All Files Within a Directory Using Nio.File.Directorystream;
How to Add a Utf-8 Bom in Java
Double Calculation Producing Odd Result