0

I want to connect to sql server 2017

I try without error to connect to sql server using sql server management studio

enter image description here

and I execute some query like :

 SELECT fullname
 FROM [AssetInst.Integ].[dbo].[Employee]

my problem now is to connect using java hibernate.

I use sqljdbc4.jar and jdk 1.6

first I try with this java class to test connexion:

public static void main(String[] args)
    {

       try
       {

            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());   

            Connection con = DriverManager.getConnection("jdbc:sqlserver://192.168.0.12\\MSSQLSERVER2017:1433;databaseName=AssetInst.Integ","Test_user","P@ssw0rd123@");


            String query =" select * FROM [AssetInst.Integ].[dbo].[Employee]";
            Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while(resultSet.next())
            {
                System.out.print("fullname: " + resultSet.getString("fullname"));                
            }
       }catch(Exception ex)
       {
            ex.printStackTrace();
       }
    }

but I have this error :

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'Test_user'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
    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:2529)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at com.dq.hibernate.generator.Snippet.main(Snippet.java:27)
Dale K
  • 25,246
  • 15
  • 42
  • 71
franco
  • 1,829
  • 6
  • 42
  • 75
  • Check out this: https://stackoverflow.com/questions/16497998/jdbc-connection-to-mssql-server-in-windows-authentication-mode – fiveelements Oct 24 '19 at 06:17
  • 1
    **Remove `:1433`** from the connection URL. When you specify both instance name and port number, the port number takes precedence, and port 1433 is used for the unnamed instance. – Andreas Oct 24 '19 at 06:26
  • thank you a lot, the problem is solved when I remove the port number – franco Oct 24 '19 at 06:34

1 Answers1

0

this is work for me

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
connectionMsSQl = DriverManager.getConnection(jdbc:sqlserver:/192.168.0.12;instanceName=AssetInst.Integ, "Test_user", "P@ssw0rd123@");

check name and password. try this one hope its works for you

Basharat Ali
  • 1,099
  • 9
  • 11