0

Question has been updated (The DriverManager is no longer loaded manually and instead the getConnection() method is used):

package guii;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcSQLServerConnection {

public static void main(String[] args) {

    Connection conn = null;
    Statement st=null;
    ResultSet rs=null;

    try {

        String dbURL = "jdbc:sqlserver://ASUS\\YES:1433;integratedSecurity=false";
        String user = "sa";
        String pass = "12345";
        conn = DriverManager.getConnection(dbURL,user,pass);
        String query="SELECT * FROM test";
        st=conn.createStatement();
        rs=st.executeQuery(query);

        while(rs.next()){
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));


        }
     } catch (SQLException ex) {
        ex.printStackTrace();
     }
}
}

The problem is the resulting Exception of this code. I can't find out know the reason why that particular exception is thrown.

The username, password and servername were double checked and they are definitively correct.

Currently this exception is thrown:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:21504e75-b630-4f71-b76b-88b25836969c
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:251)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:81)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:3077)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2360)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1404)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at guii.JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:23)

I can run the program with integratedSecurity=true but i cannot select the table from my database. I don't even know what is the problem but i have tried in SQL Server Management Studio and it login successfully. I really gave up.First thing is to connect it with my user and password. Second is that how to select table from my database. Please help me. Thank You.

jiahao
  • 13
  • 6
  • 1
    This seems to be a well-known problem, q.v. [here](http://stackoverflow.com/questions/2806438/login-failed-for-user-domain-machinename) and [here](http://stackoverflow.com/questions/13512328/sql-connection-fails-when-using-integrated-security-on-iis-applications). – Tim Biegeleisen Mar 14 '16 at 09:23
  • @TimBiegeleisen Sorry i don't really understand can you further explain for me please. – jiahao Mar 14 '16 at 09:26
  • check your username and password at SqlServer side. its seems wrong. – Piyush Ghediya Mar 14 '16 at 09:45
  • @PiyushGhediya I have check it is correct i can connect it through SQL Management Studio – jiahao Mar 15 '16 at 00:32

0 Answers0