0

I am studying Core Java Volume 2 Advanced Features. I have to connect to any database to execute the source code in Database Programming chapter. I cannot share the whole source code probably because of copyright issues. But basically I am trying to connect to SQL Server with a simple TestDb java program and the connection properties such as DriverName, DB Url , Username and Password are located in a db.properties file as follows:

#jdbc.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433    ;databaseName=COREJAVA;int‌​egratedSecurity=true"
jdbc.username="sa"
jdbc.password="123456"

The code for connection is present in the getConnection() function as follows:

public static Connection getConnection() throws SQLException, IOException
{
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(Paths.get("database.properties")))
    {
        props.load(in);
    }
    String drivers = props.getProperty("jdbc.drivers");
    if (drivers != null) System.setProperty("jdbc.drivers", drivers);
    String url = props.getProperty("jdbc.url");
    String username = props.getProperty("jdbc.username");
    String password = props.getProperty("jdbc.password");

    return DriverManager.getConnection(url, username, password);
}

I fixed the error of TCP first thanks to this stackpost JDBC connection failed, error: TCP/IP connection to host failed

But now when I run the program using the following command from cmd:

java -cp .;sqljdbc4.jar test.TestDB

I get the following stacktrace:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '"sa"'. ClientConnectionId:2986bb7f-4f57-4932-b4c1-614ab3684ee0
        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(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at test.TestDB.getConnection(TestDB.java:68)
        at test.TestDB.runTest(TestDB.java:35)
        at test.TestDB.main(TestDB.java:19)

I have referred to Java connection to a SQL Server Database: Login failed for user 'sa'

I have IPALL port set to 1433, TCP/IP enabled in Server Configuration Manager and I am able to login normally using SQL server management studio with the 'sa' account. I am using the latest sqljdbc4.jar from Microsoft's website.

Community
  • 1
  • 1
  • Perhaps try `integratedSecurity=false` since you're trying to log in as `sa` anyway. – Gord Thompson Dec 08 '16 at 18:20
  • @GordThompson I tried without the integrated security property and even tried by adding another property instance='SQLExpress' I still get the same error. :( – Mohammed Salman Shaikh Dec 08 '16 at 18:25
  • Okay, try removing the double quotes from around the username and password in your properties file, e.g., `dbc.username=sa` instead of `dbc.username="sa"`. – Gord Thompson Dec 08 '16 at 18:32

1 Answers1

1

@GordThompson thank you! I removed the double quotes from both username and password making

jdbc.username=sa
jdbc.password=123456

And it is connecting to the database now!