Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:14:58+00:00 2026-06-12T00:14:58+00:00

I am learning Java and need to just run something simple to retrieve some

  • 0

I am learning Java and need to just run something simple to retrieve some data from MSSQL via JDBC. The example in my book doesn’t work (but it is several years old) and this example below from MS doesn’t work for me either:

http://msdn.microsoft.com/en-us/library/ms378956(v=sql.90).aspx

Here’s my code:

package javasql;
import java.sql.*;
import java.util.*;

public class Program {

    private static String url = "jdbc:sqlserver://localhost\\SQLExpress;database=Northwind;integratedSecurity=true;";
    //private static String userName = "sa";
    //private static String password = "myPassword";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RunDemo();
    }

    public static void RunDemo() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection connection = DriverManager.getConnection(url);

            Statement statement = connection.createStatement();
            ResultSet results = statement.executeQuery("SELECT ProductName, Price FROM Products ORDER BY ProductName");

            while(results.next()) {
                System.out.println("Product Name: " + results.getNString("ProductName") + " Price: $" + results.getFloat("UnitPrice"));
            }

        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

When I run the code, I don’t get any exceptions thrown.. I just get this in the output window:

run:
com.microsoft.sqlserver.jdbc.SQLServerDriver
BUILD SUCCESSFUL (total time: 0 seconds)

I am using NetBeans 7.2. Please someone give me a working example.

EDIT:

By the way, for the connection string, where you see the \\SQLExpress, I did try removing that and using instanceName=SQLExpress instead.. but that didn’t have any effect either.

EDIT 2:

OK, I downloaded the latest JDBC driver for MSSQL from MS and referenced the 2 JAR files in there. Now I’m getting this output:

run:
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.
BUILD SUCCESSFUL (total time: 15 seconds)

Progress.. at least we can see it is trying to connect now, can someone enlighten me as to the above error though?

EDIT 3:

2 more problems fixed.. one is enable SQL Server Browser and the second was enabling TCP/IP for SQL Server. Thanks @Vikdor Now I’m getting this error:

run:
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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.".
BUILD SUCCESSFUL (total time: 15 seconds)

I checked windows firewall and added an inbound rule to allow that port, but I’m still getting the above error. Any ideas?

EDIT 4:

Tried the solution in this link:
http://www.coderanch.com/t/306316/JDBC/databases/SQLServerException-TCP-IP-connection-host

No longer getting error in EDIT 3. Now getting another…

run:
Sep 21, 2012 11:33:16 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
This driver is not configured for integrated authentication. ClientConnectionId:577f359e-4774-45f3-96fb-588785911817
BUILD SUCCESSFUL (total time: 14 seconds)

Getting very tired of this now.. why Java, why?? Seriously…I’m glad I work mostly with .NET. Well, when i find the solution, I will post it here to make sure it can help others before they go mad as I am about to…

EDIT 5:

This helped:
java connecting to MicrosoftSQLServer 2005

I put the directory path into my PATH environment variable. Didn’t work, so I also placed the sqljdbc_auth.dll into my JDK folder C:\Program Files\Java\jdk1.7.0_04\bin. Solved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:14:59+00:00Added an answer on June 12, 2026 at 12:14 am

    OK, so here’s what solved my problems:

    1. Download latest MSSQL JDBC driver from here:
      http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

    2. Referenced the 2 JAR files in my project:
      sqljdbc.jar and
      sqljdbc4.jar (I’m not yet sure if both of the above are required or just one..)

    3. Make sure the SQL Server Browser windows service is running

    4. Open SQL Server Configuration Manager and go to Protocols for SQLEXPRESS under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties. Set Enabled = YES.

    5. While you’re there, click on IP Addresses tab and find the section IP All. Set TCP Port to 1433.

    6. Add sqljdbc_auth.dll to your PATH Environment Variable. In my case: D:\Java\sqljdbc_4.0\enu\auth\x64

    7. Copy the sqljdbc_auth.dll to your JDK directory. In my case: C:\Program Files\Java\jdk1.7.0_04\bin

    I hope this helps someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple parser for some Java-like language (just for learning purposes). I'm
I just started learning C++(coming from Java & Python) and am trying to learn
I'm still learning Java/Android development and I need some guidance to implement an Async
I'm just now learning how to use the Android/Java Socket class and I need
Whilest learning Python 3 and converting some of my code from Java to Python
I am climbing the java learning curve, and for the first time I need
I am learning Java with Swing and I have some problems with using JTextField
We are just learning Java/Selenium as our QA is moving towards automation. I am
I just started learning Java and I'm confused about the topic of platform independence.
i just started learning swings. And thought of trying out a simple program, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.