Solution of com.microsoft.sqlserver.jdbc.SQLServerException – Fixing error connecting Microsoft SQL Server database using Java code.

Published: Last Updated on 3.1K views 5 minutes read
A+A-
Reset

While I returned to eclipse to test a simple JDBC related program after couple of months, I encountered some kind of error. The error was jdbc.SQLServerException.

Source Code

Let’s have look at source code where Microsoft SQL Server is connected using JDBC.SqlServerDriver.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class TestDB {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		String dburl="jdbc:sqlserver://localhost\\SQLEXPRESS;user=sa;password=password";
		Connection con=DriverManager.getConnection(dburl);
		String sql="Select * from DevelopmentDB.dbo.UserInfo";
		Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
		ResultSet rs=st.executeQuery(sql);
                System.out.println(rs);
		}

Output with jdbc.SQLServerException error

Now while running this program, I got following exception in output console.

Tip: An unwanted and unexpected event during program execution which disrupt the normal flow of execution of program is called Exception.
To learn more about what is exception in Java, please refer to above linked article.

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:5247)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1871)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1608)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
 at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at TestDB.main(TestDB.java:14)
jdbc.SQLServerException error, Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed.
Error showing jdbc.SQLServerException in console.

Error Troubleshooting – Validating the Instance name

Now I do as per the instruction in the error.

  1. Server name is fine. localhost or Name of pc is fine for server name. or run this code in SQL Server Management Studio.
    select @@SERVERName

    This code will display name of server

  2. Instance name: It is very difficult to find instance name for me. After checking a manual, I find a code to find instance name.
    select @@SERVICENAME

    This code will print name of instance.

Solution of com.microsoft.sqlserver.jdbc.SQLServerException - Fixing error connecting Microsoft SQL Server database using Java code. 1
Select commands in SQL for identifying Server name and Instance Name.

After executing these codes, I got – MSSQLSERVER as instance name.

Solution – Change the instance Name

As you can see in the connection string part, String dburl="jdbc:sqlserver://localhost\SQLEXPRESS;, I have mentioned SQLEXPRESS as instance name of localhost server. While as just returned above by codes in SSMS, the name of server was MSSQLSERVER.

I replaced SQLEXPRESS by MSSQLSERVER and my code run fine.

To learn Java in video tutorials, do not forget to subscribe us on YouTube.

Do not forget to revert with your feedback and suggestions.

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.