Can't connect to sql server database with java

Posted on 2006-05-15
Last Modified: 2010-04-17

i downloaded the drivers from microsoft and the jars are in the classpath of my project but it is thowing an exception when i try to connect to the sql server database, heres my code:

               con = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;databaseName=database;selectMethod=cursor;",userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               System.out.println("Error Trace in getConnection() : " + e.getMessage());

The exception i am getting is:
[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
      at Source)
      at Source)
      at Source)
      at<init>(Unknown Source)
      at Source)
      at Source)
      at Source)
      at Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at net.mainsequence.DBConnector.getConnection(
      at net.mainsequence.pcr.imagemanager.LuceneTest.main(

The username, password and port is correct and the same code worked on someone else's computer.
Any suggestions?

Question by:csalem05
    LVL 1

    Accepted Solution

    Try creating a datasource, you have to import
    import javax.sql.DataSource;

    protected Connection            conn;

    try {
          SQLServerDataSource ds = new SQLServerDataSource();
                   conn = ds.getConnection();
                       System.out.println("Connection Successful!");
        catch (SQLException sqlex) {

    LVL 1

    Expert Comment

    Or you can use

    try {
          DriverManager.registerDriver(new SQLServerDriver());
          Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;"
                                                                                   + " databaseName=databaseName","user", "password");
          if (conn!=null)
               System.out.println("Connection Successful!");
    catch (SQLException sqle) {

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    This is an explanation of a simple data model to help parse a JSON feed
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now