Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to connect local db sqlserver 2012 but unable to connect. I am using the below code. Please help to solve this issue.

Java
String url = "jdbc:sqlserver://(localdb)\\v11.0;" +  "databaseName=sakthi;user=sakthi;password=****;";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

             Connection con = DriverManager.getConnection(url);
             Statement stmt = con.createStatement();
             ResultSet res =stmt.executeQuery("select * from sakthi");
              while(res.next()){
                  System.out.println(res.getString(1));
                  System.out.println(res.getString(2));
              }


I am getting the below error.

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host (localdb), named instance v11.0 failed. Error: "java.net.UnknownHostException: (localdb)". 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:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
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 javaprojects.jdbc.main(jdbc.java:17)
Posted
Updated 8-Jun-14 5:48am
v2
Comments
[no name] 8-Jun-14 12:10pm    
http://stackoverflow.com/questions/11816522/connect-to-sql-server-localdb-jdbc

1 solution

(1)first thing I would do is go into sql server config manager and enable the browser service

(2)then open windows firewall and create new rule to allow UDP traffic through port 1434

see this article for more info:

"http://www.mssqltips.com/sqlservertip/1946/overview-of-the-sql-server-browser-service/"

hope it helps

Update:
After this you may get an error "SQL Server not configured to listen with TCP/IP". For this solution, watch the video: SQL Server not configured to listen to TCP/IP solved - YouTube[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900