1

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.SQLException;

/**
 * This program demonstrates how to establish database connection to Microsoft
 * SQL Server.
 * @author www.codejava.net
 *
 */
public class JdbcSQLServerConnection {

public static void main(String[] args) {

    Connection conn = null;

    try {

        String dbURL = "jdbc:sqlserver://ASUS\\YES:1433";
        String user = "TestingUser";
        String pass = "12345";
        conn = DriverManager.getConnection(dbURL, user, pass);
        if (conn != null) {
            DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
            System.out.println("Driver name: " + dm.getDriverName());
            System.out.println("Driver version: " + dm.getDriverVersion());
            System.out.println("Product name: " + dm.getDatabaseProductName());
            System.out.println("Product version: " + dm.getDatabaseProductVersion());
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } 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 'TestingUser'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at guii.JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:25)
giro
  • 421
  • 1
  • 7
  • 19
jiahao
  • 13
  • 6
  • your server name is really `String server="ASUS\\YES";` ? – Scary Wombat Mar 10 '16 at 02:03
  • I'm not sure but i check through the properties of the server at Microsoft SQL Server Management Studio it listed like that. Or any other way to find out the server name?? – jiahao Mar 10 '16 at 02:18
  • have you tried using windows authentication? – giro Mar 10 '16 at 02:32
  • u mean try it in java ?? – jiahao Mar 10 '16 at 02:32
  • Yes. Look at this question [http://www.stackoverflow.com/questions/6938717/using-windows-authentication-with-sqljdbc-jar] for reference – giro Mar 10 '16 at 02:39
  • okok very thank you. – jiahao Mar 10 '16 at 02:40
  • @GilR. but im not sure whether my server name is really ASUS\\YES ? how to i double confirm it ?? – jiahao Mar 10 '16 at 02:41
  • Why not just log out of the server management studio and open in up again and check what's written in the Login window? – giro Mar 10 '16 at 02:43
  • Yes it written ASUS\\YES but it also having error. The code that i change is this : jdbc:sqlserver://ASUS\\YES;Database=testing;integratedSecurity=true – jiahao Mar 10 '16 at 02:46
  • try to add the port again like in the answer to this question http://www.stackoverflow.com/questions/12769171/jdbc-sqlserverexception-login-failed-for-user-for-any-user – giro Mar 10 '16 at 02:54
  • it says that the sqljdbc_auth.dll failed to load. – jiahao Mar 10 '16 at 03:09
  • Habe you installed the SQL driver? Check point 1 in this tutorial http://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via-jdbc – giro Mar 10 '16 at 03:12
  • but my one is SQL Server 2014 – jiahao Mar 10 '16 at 03:16
  • Just use sqljdbc4.Jar instead. I don't know about the differences of using different versions exactly but establishing a connection should not differ significantly, except using the latest driver versions and such. – giro Mar 10 '16 at 03:20
  • I really recommend you go through this tutorial step by step http://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via-jdbc I've used it myself. – giro Mar 10 '16 at 03:24
  • but still the same problem the error stated Mar 10, 2016 11:28:11 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path – jiahao Mar 10 '16 at 03:29
  • Then add it to your path. In eclipse copy it into your project then right click and add to build path – giro Mar 10 '16 at 03:32
  • Plesse use the code provided at the end of the tutorial I recommended. Don't try to load the driver yourself. – giro Mar 10 '16 at 03:39
  • So i have to change the sqlexpress from String dbURL = "jdbc:sqlserver://localhost\\sqlexpress"; to my server name or what ? – jiahao Mar 10 '16 at 03:45
  • @GilR. I have try many time about the tutorial that you gave me yesterday. I change the dburl become String dbURL = "jdbc:sqlserver://ASUS\\YES"; Then the error that occur is : com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host ASUS, named instance YES has failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names, check that no firewall is blocking UDP traffic to port 1434, and for SQL Server 2005 or later verify that the SQL Server Browser Service is running on the host. – jiahao Mar 11 '16 at 07:22
  • please update your question by inserting your current code and error message – giro Mar 11 '16 at 14:40
  • Question has been updated on top @GilR. – jiahao Mar 14 '16 at 00:52
  • Was the exception message updated well? – giro Mar 14 '16 at 01:05
  • The exception message still the same. I updated well. – jiahao Mar 14 '16 at 01:06

1 Answers1

0

Trouble Shooting Notes

  1. Use integratedSecurity=true again

  2. Check if you have the sqljdbc_auch.dll in your system32 folder Check out this link Make sure you use the correct one (32 vs 64 bit) Make sure the sqljdbc_auch.dll is in the Windows system path

  3. Check if the properties of your Server instance are set correctly: access rights, make sure it actually listens in the port 1433, ... check out this link and don't just look at the accepted answer but the other two as well.

  4. make sure your firewall does not prevent any connections

  5. check out if your Java Version matches the jdbc Version you use sqljdbc4.2 needs Java 1.8, jdbc4.1 needs java 1.7. look here

Community
  • 1
  • 1
giro
  • 421
  • 1
  • 7
  • 19
  • @jiahao what helped you in the end? – giro Mar 14 '16 at 11:34
  • The answer that you gave me is helping me but now the problem is that i cannot connect it through my user and password. And i cannot access to my table using query. I have already change my query(Table) to [Database Name].[Schema].[Table Name] still the same the error is Invalid object name. Please help me. – jiahao Mar 16 '16 at 01:47
  • I think if you're already connected to the database [Schema].[Table Name] should be enough. Also check your access rights so you can access your tables with the windows user. This should solve your problem. – giro Mar 16 '16 at 07:33
  • how to check whether it can be access by windows user ?? – jiahao Mar 16 '16 at 09:24
  • this isn't really within the scope of this question anymore but you should check [role management](https://msdn.microsoft.com/en-us/library/ms156293(v=sql.120).aspx) – giro Mar 16 '16 at 09:45
  • I cannot access to the database table. – jiahao Mar 17 '16 at 00:49
  • Post a new question and provide more details. I don't know what you're missing at this moment. – giro Mar 17 '16 at 13:31