0

I'm trying to connect from a Java application to a Linked Server I created with MSSQL Server.

The URL string is

jdbc:sqlserver://172.15.230.11

and the query is

SELECT * FROM OPENQUERY(172.15.230.11,'SELECT * FROM myTable WHERE myCode = 345')

But when I run the program, this exception occurs:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'myUser'.

The actual code is here:

private static final String DB_URL_LOCAL = "jdbc:sqlserver://172.15.230.11";
    private static final String DB_USERNAME_LOCAL = "myUser";
    private static final String DB_PASSWORD_LOCAL = "myPassword";
    private static final String DB_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    static String SQL_READ = "SELECT * FROM OPENQUERY(172.15.230.11,'SELECT * FROM myTable WHERE myCode = 345')";

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(SQL_READ);
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static Connection getConnection(){
        Connection connection = null;
        Properties properties = new Properties();
        try {
            Class.forName(DB_CLASS);
            properties.setProperty("characterEncoding", "utf-8");
            properties.setProperty("user", DB_USERNAME_LOCAL);
            properties.setProperty("password", DB_PASSWORD_LOCAL);
            connection = DriverManager.getConnection(DB_URL_LOCAL, properties);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

1 Answers1

0

First of all you have to make sure that you enable remote connections to MSSQLserver.

Then make sure you use a user in your connection wich has sufficient rights to query your schema.

Then make sure you provide the correct source to the JDBC driver:

dbsource= "jdbc:sqlserver://IP:1433;database=MY_SCHEMA";

Then make sure you load the correct JDBC driver and use the appropriate user and password

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbCon = DriverManager.getConnection(dbsource, user, password);

On top of all that, if your database is using windows authentication, then you can use 'integratedSecurity':

dbsource= "jdbc:sqlserver://IP:1433;database=MY_SCHEMA;integratedSecurity=true;";
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • Thanks. It isn't using windows authentication. With same username and password I can login using sql server management studio or Navicat but the same username and password doesn't work within my application. – Amin Izadpanah Nov 17 '15 at 18:01
  • maybe if you add the actual code can help finding the problem – MaVRoSCy Nov 17 '15 at 21:50