Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a jTDS connection string

my sql server instance name is MYPC\SQLEXPRESS and I'm trying to create a jTDS connection string to connect to the database 'Blog'. Can anyone please help me accomplish that?

I'm trying to do like this:

DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/Blog", "user", "password"); 

and I get this:

 java.sql.SQLException: Network error IOException: Connection refused: connect     at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:395)     at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     at SqlConnection.Connect(SqlConnection.java:19)     at main.main(main.java:11) Caused by: java.net.ConnectException: Connection refused: connect     at java.net.PlainSocketImpl.socketConnect(Native Method)     at java.net.PlainSocketImpl.doConnect(Unknown Source)     at java.net.PlainSocketImpl.connectToAddress(Unknown Source)     at java.net.PlainSocketImpl.connect(Unknown Source)     at java.net.SocksSocketImpl.connect(Unknown Source)     at java.net.Socket.connect(Unknown Source)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:305)     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:255)     at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:323)     ... 6 more 
like image 745
Omu Avatar asked Dec 07 '09 19:12

Omu


People also ask

What is JTDS connection?

JDBC (Java Database Connectivity) is a programming interface that lets Java applications access a relational database. SuperCHANNEL needs a JDBC driver so that it can access the relational database system (e.g. SQL Server, Oracle, etc) where your source data is stored.

What is the difference between JTDS and JDBC?

One relevant difference is, that the JDBC 4.0 Implementation of the JTDS-Driver (v1. 3.1) requires at least Java 7, whereas the JDBC 4.0 Implementation of the Microsoft driver is available in Java 6. This is important if you want to use Hibernate 4.3 (relying on JDBC 4.0 API) and support Java 6.

What is net Sourceforge JTDS JDBC driver?

jTDS is an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000, 2005, 2008 and 2012) and Sybase Adaptive Server Enterprise (10, 11, 12 and 15). jTDS is based on FreeTDS and is currently the fastest production-ready JDBC driver for SQL Server and Sybase ASE.


2 Answers

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.

like image 63
Pascal Thivent Avatar answered Oct 20 '22 19:10

Pascal Thivent


Really, really, really check if the TCP/IP protocol is enabled in your local SQLEXPRESS instance.

Follow these steps to make sure:

  • Open "Sql Server Configuration Manager" in "Start Menu\Programs\Microsoft SQL Server 2012\Configuration Tools\"
  • Expand "SQL Server Network Configuration"
  • Go in "Protocols for SQLEXPRESS"
  • Enable TCP/IP

If you have any problem, check this blog post for details, as it contains screenshots and much more info.

Also check if the "SQL Server Browser" windows service is activated and running:

  • Go to Control Panel -> Administrative Tools -> Services
  • Open "SQL Server Browser" service and enable it (make it manual or automatic, depends on your needs)
  • Start it.

That's it.

After I installed a fresh local SQLExpress, all I had to do was to enable TCP/IP and start the SQL Server Browser service.

Below a code I use to test the SQLEXPRESS local connection. Of course, you should change the IP, DatabaseName and user/password as needed.:

import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException;  public class JtdsSqlExpressInstanceConnect {     public static void main(String[] args) throws SQLException {         Connection conn = null;         ResultSet rs = null;         String url = "jdbc:jtds:sqlserver://127.0.0.1;instance=SQLEXPRESS;DatabaseName=master";         String driver = "net.sourceforge.jtds.jdbc.Driver";         String userName = "user";         String password = "password";         try {             Class.forName(driver);             conn = DriverManager.getConnection(url, userName, password);             System.out.println("Connected to the database!!! Getting table list...");             DatabaseMetaData dbm = conn.getMetaData();             rs = dbm.getTables(null, null, "%", new String[] { "TABLE" });             while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); }         } catch (Exception e) {             e.printStackTrace();         } finally {             conn.close();             rs.close();         }     } } 

And if you use Maven, add this to your pom.xml:

<dependency>     <groupId>net.sourceforge.jtds</groupId>     <artifactId>jtds</artifactId>     <version>1.2.4</version> </dependency> 
like image 29
acdcjunior Avatar answered Oct 20 '22 21:10

acdcjunior