Java.sql.SQLException: Login failed for user 'admin'

Disclaimer: I have never used SQL Server before.

I am trying to connect to SQL Server Express using java code.

public class Test1 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String url = "jdbc:jtds:sqlserver://localhost:1433/POC;instance=MOHITCH-LAPTOP/SQLEXPRESS";
        String user = "admin";
        String password = "admin";
        Connection conn = DriverManager.getConnection(url, user, password);
        Statement st = conn.createStatement ();
            ResultSet rs = st.executeQuery("select * from POC.dbo.poc_table");
            while (rs.next())
            {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
        }
    }

      

And I am getting an exception:

Exception in thread "main" java.sql.SQLException: Login failed for user 'admin'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:603)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:352)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at my.java.Test1.main(Test1.java:16)

      

I also tried to login using MS SQL Server Management Studio 2014. And I did it successfully .

enter image description here

Here is my database structure:

enter image description here

Any help is appreciated! Thanks to

+3


source to share


2 answers


I think you need to change some settings on your server.

Follow the step below and hope it helps you.



1. Open your SQL Server Management Studio.

2. Database server right click and go to properties.

3. Choose Security option and check SQL Server and Windows authentication mode.

4. Enable TCP/IP connection in SQL Configuration Manager.

5. Restart your SQL server service.

      

+6


source


First of all, make sure the SQL Browser Service is running - in Windows Control Panel Services. If you cannot use the JTDS driver, there is an official Microsoft driver - according to various criteria it is a little slower, but this is the most complete implementation - you will find a lot of problems with JTDS (something is not supported or just doesn't work, nobody worries about it , version 1.3 doesn't work in JDK6).

Connection string which is sufficient (no instance needed for express version):

jdbc:jtds:sqlserver://localhost:1433/MyDatabase

      



If you used the MS driver connection string, it would be:

jdbc:sqlserver://localhost:1433;databaseName=MyDatabase

      

+1


source







All Articles