-1

I have the following expression:

 <bean class="java.net.InetAddress" id="inetAddress" />

<bean id="dataSource"
      class="org.apache.tomcat.jdbc.pool.DataSource"
      p:driverClassName="org.postgresql.Driver"
      p:url="#{'jdbc:postgresql://' + inetAddress.getLocalHost().getHostName()=='alex-HP-290-G1-SFF-Business-PC'?'localhost':'172.18.0.2' + ':5432/infostock'}"/>

This expression

 inetAddress.getLocalHost().getHostName()=='alex-HP-290-G1-SFF-Business-PC'?'localhost':'172.18.0.2'

must compare hostname-value (retrieved from getHostName) with 'alex-HP-290-G1-SFF-Business-PC'. If true, it returns 'localhost', otherwise '172.18.0.2'.

Actual value of hostname is 'alex-HP-290-G1-SFF-Business-PC'. I get this from:

 System.out.println(InetAddress.getLocalHost().getHostName());

So, ternary operator must return 'localhost'

But it returns '172.18.0.2'.

Jelly
  • 972
  • 1
  • 17
  • 40
  • 4
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Julien Mar 11 '20 at 12:55
  • I tried that yet (inetAddress.getLocalHost().getHostName().equals('andrej-HP-290-G1-SFF-Business-PC') ? 'localhost' : '172.18.0.2') But I see in logs: Invalid boolean value 'jdbc:postgresql://true' – Jelly Mar 11 '20 at 12:58

1 Answers1

0

I have found this solution:

    p:url="#{inetAddress.getLocalHost().getHostName().equals('andrej-HP-290-G1-SFF-Business-PC')?'jdbc:postgresql://localhost:5432/infostock':'jdbc:postgresql://172.18.0.2:5432/infostock'}"

But its too long, and I suppose, its not optimal

Jelly
  • 972
  • 1
  • 17
  • 40