0

when i am trying to connect sql server 2008 with jdbc an exception has occured as fellows com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'.

the code is shown as follows

import java.sql.*;

public class myjdbc {
    public static void main(String[] args) {
        String sDriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
        String Name = "sa";
        String Pswd = "1111";
        String aDBUrl = "jdbc:sqlserver://localhost:1433;databaseName=pasdb";
        //String bDBUrl = "jdbc:sqlserver://scu-PC\\Barney;databaseName=pasdb;integratedSecurity=true";
        try {
            Class.forName(sDriverName);
            DriverManager.getConnection(aDBUrl,Name,Pswd);
            //DriverManager.getConnection(bDBUrl);
        } catch (ClassNotFoundException e) {            
            e.printStackTrace();
        } catch (SQLException ex) {         
            ex.printStackTrace();
        }       

    }
}

When i use the bDBUrl method to connect SQL, all goes well; when i use the aDBUrl mehtod, the error goes out.

PS: i can login sql server by using account sa through sql server management studio

1 Answer 1

1

With bDBUrl the username and password are ignored, and integrated security (your windows account) is used. So your own user account is used to login (and is allowed to login).

With aDBUrl you use the sa user and now login fails. There might be several causes: SQL authentication is completely disabled, there is no user sa, the user sa is disabled, the password of sa is not the password you specify, or the user sa has no access to the specified database.

Also note that aDBUrl and bDBUrl are different and might not be pointing to the same computer, or if scu-PC is the same as your localhost: they might be different instances.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.