Search code examples
ibm-mobilefirstworklight-adapters

IBM Worklight SQL Adapter - Cannot connect to database. Cannot load JDBC driver class 'com.mysql.jdbc.Driver'


my adapter.xml

<?xml version="1.0" encoding="UTF-8"?>
<wl:adapter name="DbConnect"

    <displayName>DbConnect</displayName>
    <description>DbConnect</description>
    <connectivity>
        <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
            <!-- Example for using a JNDI data source, replace with actual data source name -->
            <!-- <dataSourceJNDIName>java:/data-source-jndi-name</dataSourceJNDIName> -->

            <!-- Example for using MySQL connector, do not forget to put the MySQL connector library in the project's lib folder -->
            <dataSourceDefinition>
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <url>jdbc:mysql://localhost:3036/test</url>
                <user>root</user>
                <password>root</password>
            </dataSourceDefinition>
        </connectionPolicy>
        <loadConstraints maxConcurrentConnectionsPerNode="5" />
    </connectivity>

    <!-- Replace this with appropriate procedures -->
    <procedure name="select"/>

</wl:adapter>

and adapter-impl.js

var statement = WL.Server.createSQLStatement("select * from categorie");
    function select(statement) {
        return WL.Server.invokeSQLStatement({
            preparedStatement : statement
        });
    }

I downloaded the connector .jar driver and locate it in the server/lib folder. I don't understand what could be wrong..

edit: below i post the message of console javascript when i run the app in worklight mobile browser simulator

wlclient init started wlgap.android.js:1481
before: app init onSuccess wlgap.android.js:1481
Request [/apps/services/api/Canti_Liturgici/android/query] wlgap.android.js:1481
after: app init onSuccess wlgap.android.js:1481
wlclient init success wlgap.android.js:1481
Failed to load resource: the server responded with a status of 401 (Unauthorized) http://localhost:8080/apps/services/api/Canti_Liturgici/android/query
Request [/apps/services/api/Canti_Liturgici/android/query] wlgap.android.js:1481
response [/apps/services/api/Canti_Liturgici/android/query] success: /*-secure-
{"responseID":"8","errors":["Runtime: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'"],"isSuccessful":false,"warnings":[],"info":[]}*/ wlgap.android.js:1481
Procedure invocation error. Runtime: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver' wlgap.android.js:1487
Failure {"status":200,"invocationContext":null,"errorCode":"PROCEDURE_ERROR","errorMsg":"Procedure invocation error. Runtime: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'","invocationResult":{"responseID":"8","errors":["Runtime: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'"],"isSuccessful":false,"warnings":[],"info":[]}}

Solution

  • I solved the problem. I edited the port of url in .xml file (is 3306 and not 3036), also i corrected the function select(statement) in select().

    below the two file corrected.

    adapter.xml

    <displayName>DbConnect</displayName>
        <description>DbConnect</description>
        <connectivity>
            <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
                <!-- Example for using a JNDI data source, replace with actual data source name -->
                <!-- <dataSourceJNDIName>java:/data-source-jndi-name</dataSourceJNDIName> -->
    
                <!-- Example for using MySQL connector, do not forget to put the MySQL connector library in the project's lib folder -->
                <dataSourceDefinition>
                    <driverClass>com.mysql.jdbc.Driver</driverClass>
                    <url>jdbc:mysql://localhost:3306/test</url>
                    <user>root</user>
                    <password>mysql</password>
                </dataSourceDefinition>
            </connectionPolicy>
            <loadConstraints maxConcurrentConnectionsPerNode="5" />
        </connectivity>
    
        <!-- Replace this with appropriate procedures -->
        <procedure name="remoteDbSize"/>
    

    adapter-impl.js

    var statement = WL.Server.createSQLStatement("SELECT SUM( data_length + index_length ) FROM information_schema.TABLES WHERE table_schema =  \"test\" GROUP BY table_schema");
    function remoteDbSize() {
        return WL.Server.invokeSQLStatement({
            preparedStatement : statement,
            parameters: []
        });
    }