0
votes

I know this question has been asked many times before. However I cannot seem to get right.

I'm trying to connect to a local MS SQL server instance with Java (I have no problem to connect to this server with .NET).

I'm using sqljdbc.jar library with the following code:

String url = "jdbc:microsoft:sqlserver://";
String serverName= "MPRSNT2765/SQLEXPRESS";
String portNumber = "1433";
String databaseName= "MogGPS";
String un = "admin";
String pw = "admin";        
String selectMethod = "cursor"; 

String connectionUrl = url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(connectionUrl,un,pw);

I get the following stacktrace:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host MPRSNT2765/SQLEXPRESS, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190) at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241) at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243) at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at bcapi.SQL.Init(SQL.java:29) at bcapi.App3.main(App3.java:33)

What am I doing wrong?

2
Compare your URL with the one from the manual: msdn.microsoft.com/en-us/library/ms378428%28v=sql.110%29.aspxa_horse_with_no_name
how did you get sqlite mixed in?A ツ
Oh my goodness, I had a SQLLite library in the mix as well. Well I removed it now and it is still not working, however a different error. I have updated the error in my postceds

2 Answers

2
votes

remove "microsoft" from the URL:

String url = "jdbc:sqlserver://";
1
votes

Normal URL to access SQL Server in java is :

jdbc:sqlserver://MPRSNT2765:1433;databaseName=MogGPS

URL:

jdbc:sqlserver://<server_name>:<server_port>;databaseName=<db_name>

Do not use MPRSNT2765/SQLEXPRESS use only server name MPRSNT2765

So that modified url and servername as below:

String url = "jdbc:sqlserver://";
String serverName = "MPRSNT2765";