Results 1 to 5 of 5
  1. #1
    alexander.s is offline Member
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default Cannot connect to sqlserver 2008 with JDBC

    All I want to do here is to connect to the database.

    So I've disabled my firewall, with the help of the sql server configuration manager I've enabled "VIA" and under tcp/ip->ip-addresses->IPAll. I've set tcp dynamic ports to 0 and the port to 1433. Those were the recommendations that I found from googling.

    my MySQL connection worked straight away, this microsoft stuff is really a pain :(

    Overlook:
    The SQLserver2008 is located on my local machine
    No firewall activated
    Enabled VIA and the sqlserver should be listening on 1433
    EDIT:
    I'm using Netbeans 6.8 and I've added jtds-1.2.5 and sqljdbc4 to the project.
    I've tried using both drivers with no luck.
    EDIT nr2:
    I forgot to mention that my sql server login account "sa" is activated and works fine, I can log with the sql server management studio with both the windows authentication and the sql server option.

    EDIT nr3:
    Apparently the server isn't listening on port 1433, that was the port number when I googled for sql server 2008. But mine listens on a high port number, I changed my java code and it works..for anyone else trying to make this work, open Microsoft SQL Server Mangement Studio click on Managment -> Sql Server Logs -> Current. Look around for a while and you'll find the line "Server is listening on <servername><port number>"


    Java Code:
    package javaapplication3;
    import java.sql.*;
    
    public class Main
    {
        public static void main(String[] args)
        {
            DB db = new DB();
            db.dbConnect("jdbc:sqlserver://localhost\\servername/Databases/databasename:1433","sa","password");
        }
    }
    
    class DB
    {
        public DB() {}
    
        public void dbConnect(String db_connect_string,
      String db_userid, String db_password)
        {
            try
            {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection conn = DriverManager.getConnection(
        db_connect_string, db_userid, db_password);
                System.out.println("connected");
    
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
    Java Code:
    SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException:  The TCP/IP connection to the host localhost, port 1433 has failed. 
    Error: "connect timed out. 
    Verify the connection properties, check that an instance of SQL Server is running on the host  and accepting TCP/IP connections at the  port, and that no firewall is blocking TCP connections to the port
    The code block above is the translation of the first part of the code block below.

    Java Code:
    [SIZE="2"]com.microsoft.sqlserver.jdbc.SQLServerException: TCP/IP-anslutningen till värddatorn localhost, port 1433 misslyckades. Fel: "Connection refused: connect. Kontrollera anslutningsegenskaperna, att en instans av SQL Server körs på värddatorn som accepterar TCP/IP-anslutningar på porten och att ingen brandvägg blockerar TCP-anslutningar till porten.".
            at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
            at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
            at java.sql.DriverManager.getConnection(DriverManager.java:582)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at javaapplication3.DB.dbConnect(Main.java:23)
            at javaapplication3.Main.main(Main.java:9)[/SIZE]
    Last edited by alexander.s; 04-26-2010 at 09:51 PM.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Hold on )

    To connect to MS SQL with Java you need to download some special connectors. MS SQL cannot be connected with MySQL connectors.
    Please visit microsoft.com to download the latest MS SQL Server 2008 Java connectors (drivers).
    I recommend this links
    MS SQL 2005 JDBC Driver

    And please read its tutorials because it is much differ thing as for MySQL
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    ideyatech is offline Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    7
    Rep Power
    0

    Default

    Import the driver file.

  4. #4
    aska is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default use jdbc

    hello programmers !
    i'm new in this website,so it's my first topic

    i have problem with ueing jdbc in my project
    i know this steps but after that do know!

    Download the newest driver from Microsoft: http://www.microsoft.com/downloads/d...7000d-68d0-453...
    Unpack/unzip the downloaded file. For me, expanding the archive resulted in a folder called sqljdbc_3.0
    Inside the expanded folder is another, called enu (or whatever language was selected with the download from step 1), which contains, among other items, a file called sqljdbc4.jar
    Copy or move sqljdbc4.jar to your Railo 'install' folder (I use Railo on Tomcat and, for me, this is at /Developer/tomcat/railo) ... it'll be the folder that contains a railo.jar file :)
    please some one tell me what must i do to use the driver?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JDBC commands for connecting to sqlserver
    By sudukrish in forum Advanced Java
    Replies: 7
    Last Post: 04-22-2009, 10:48 AM
  2. Can't connect using jdbc!!!
    By mikep_bluefish in forum JDBC
    Replies: 1
    Last Post: 11-26-2008, 11:49 PM
  3. Replies: 1
    Last Post: 10-16-2008, 03:09 PM
  4. connect JDBC to offline database
    By nancyhung in forum JDBC
    Replies: 1
    Last Post: 04-12-2008, 12:04 AM
  5. Using JDBC to connect to ORACLE database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 12:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •