mysqlwso2ei

Pass dynamic values to WSO2 DBLook 'like' query


Scenario : I have used DBLookup mediator to retrieve the full name by passing part of the name. For that I used like option in sql.

Full Name : John Smith

Value passed : John

SQL : SELECT * FROM table WHERE FullName like '%John%'

Used config :

    <dblookup>
   <connection>
      <pool>
         <driver>com.mysql.jdbc.Driver</driver>
         <url>jdbc:mysql://localhost:3306/world</url>
         <user>root</user>
         <password>root</password>
      </pool>
   </connection>
   <statement>
      <sql>SELECT TP_ID, TP_FULL_NAME, TP_USER_NAME, TP_USER_PASSWORD, TP_ACTIVE, TP_CHANGED_TIME, TP_TENANT_ID FROM tp_user WHERE TP_FULL_NAME like ('%?%');</sql>
      <parameter expression="get-property('name')" type="VARCHAR" />
   </statement>
</dblookup>

Error :

    [2018-11-08 13:04:14,943] [] ERROR - DBLookupMediator SQL Exception occurred while executing statement : SELECT TP_ID, TP_FULL_NAME, TP_USER_NAME, TP_USER_PASSWORD, TP_ACTIVE, TP_CHANGED_TIME, TP_TENANT_ID FROM tp_user WHERE TP_FULL_NAME like ('%?%'); against DataSource : jdbc:mysql://localhost:3306/world
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)

Solution

  • Change your sql statement as below;

    SELECT TP_ID, TP_FULL_NAME, TP_USER_NAME, TP_USER_PASSWORD, TP_ACTIVE, TP_CHANGED_TIME, TP_TENANT_ID FROM tp_user WHERE TP_FULL_NAME like CONCAT('%',?,'%');

    For further reference you can follow this LINK