1

I am simply trying to test my connection with SQL Server 2008 as follows:

public class ForJDBC {


    public static void main(String[] args) {




        try{

        Connection conn = DriverManager.getConnection("jdbc:sqlserver://XX.XX.X.XXX:1433;databaseName=MyDB;user=sa;password=mypassword");

        if(conn != null){

            System.out.println("Connection is Successfull");
        }

        }
        catch(SQLException ex){

           ex.printStackTrace();
        }




    }
}

But I keep on getting the following exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:96f7d048-9734-4fd8-8216-21231d89b2e6
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    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(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at forjdbc.ForJDBC.main(ForJDBC.java:35)

When I connect to MySQL 5.5, it works fine.

Troubleshooting Steps I have followed:

1) Tried Logging with the "sa" user name and password within the SQL Server Management Console using SQL Server Management studio

2) I have sqljdbc4.jar in my Source Package folder of Netbeans 7.4

3) Updated to Java 8

Please let me know what am I doing wrong and let me know if I can answer any questions.

rocky
  • 435
  • 3
  • 8
  • 18
  • have you checked the below link - http://stackoverflow.com/questions/22458841/sqlserverexception-login-failed-for-user-sa – Sireesh Yarlagadda Apr 25 '14 at 21:57
  • Yes, I have checked many posts on the SO. Based on the above link, I do have a user on my name and permission granted to connect to SQL in SSMS. – rocky Apr 25 '14 at 22:00
  • Could this be a problem http://support.microsoft.com/kb/2653857 considering that I am not allowed to make any changes to SQL Server 2008 ? – rocky Apr 30 '14 at 07:10

3 Answers3

-1

Connection String Problem...

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Connection connection = DriverManager .getConnection("jdbc:sqlserver://localhost:1433;\SQLEXPRESS;databaseName=Tema6","sa","123456")

MIkka Marmik
  • 1,101
  • 11
  • 29
-1

I think you missed loading the Driver class and registering it.

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

If still it is not working then please ensure your SQLServer services is running

SparkOn
  • 8,806
  • 4
  • 29
  • 34
-3

Click SQL --> click right mouse --> propretise --> Securiti -->tick SQL server and windows Authentication mode

90% success!

iuiuadu_93
  • 45
  • 5