prhmma
prhmma

Reputation: 953

connecting to SQLserver in eclipse

I'm having a problem connecting to an SQLserver in Java using Eclipse. I have used SQLjdbc4 but yet there's problem.

I have no idea what is wrong with my code, but it just doesn't work. I've searched for similar problems but it wasn't useful.

Here 's my code:

import java.*;
import java.sql.*;
public class Connect{
     public static void main(String[] args) {

          // Create a variable for the connection string.
          String connectionUrl = "jdbc:sqlserver://qp-vaio\\qp:1433;" +
             "databaseName=db;user=sa;password=1";

          // Declare the JDBC objects.
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;

          try {
             // Establish the connection.
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             con = DriverManager.getConnection(connectionUrl);

             // Create and execute an SQL statement that returns some data.
             String SQL = "SELECT TOP 10 * FROM Person.Contact";
             stmt = con.createStatement();
             rs = stmt.executeQuery(SQL);

             // Iterate through the data in the result set and display it.
             while (rs.next()) {
                System.out.println(rs.getString(4) + " " + rs.getString(6));
             }
          }

          // Handle any errors that may have occurred.
          catch (Exception e) {
             e.printStackTrace();
          }
          finally {
             if (rs != null) try { rs.close(); } catch(Exception e) {}
             if (stmt != null) try { stmt.close(); } catch(Exception e) {}
             if (con != null) try { con.close(); } catch(Exception e) {}
          }
       }
}

This is the problem :

Login failed for user 'sa'. ClientConnectionId:f87b859a-c901-4ce4-8e51-02478ffac83d
    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 Connect.main(Connect.java:18)

Upvotes: 1

Views: 811

Answers (1)

Karibasappa G C
Karibasappa G C

Reputation: 2732

work around for the problem

1) If this is a one-time issue, enable “sa” login WITH changing password of “sa” login.

ALTER LOGIN sa WITH PASSWORD = 'yourpass' UNLOCK ;
GO

2) If this is a one-time issue, enable “sa” login WITHOUT changing password of “sa” login.

ALTER LOGIN sa WITH CHECK_POLICY = OFF;
ALTER LOGIN sa WITH CHECK_POLICY = ON;
GO

Upvotes: 2

Related Questions