title: JDBC connection to linked server
body: <p>I'm trying to connect from a Java application to a Linked Server I created with MSSQL Server. </p>
  
  <p>The URL string is </p>
  
  <blockquote>
    <p>jdbc:sqlserver://172.15.230.11</p>
  </blockquote>
  
  <p>and the query is </p>
  
  <blockquote>
    <p>SELECT * FROM OPENQUERY(172.15.230.11,'SELECT * FROM myTable WHERE
    myCode = 345')</p>
  </blockquote>
  
  <p>But when I run the program, this exception occurs:</p>
  
  <blockquote>
    <p>com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'myUser'.</p>
  </blockquote>
  
  <p>The actual code is here:</p>
  
  <pre><code>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;
      }
  </code></pre>