B4J Question jRDC3 - pass a string instead of an array

Wien.Mart

Member
Licensed User
Longtime User
private sub button_click
userid = userid.text

Wait For (rdc.ExecuteQuery("select_name_address", Array(userid))) Complete (Result As RDCResult) 'queries are defined in the server config.properties file


config.properties entry/query
sql.select_name_address.Select firstname, lastname, address from userlist where userid = ?

Is it possible to send a string instead value of an array to the jRDC3 server in B4J?
 
It does not make sense at all.

What if you need to pass two or more parameters?
 
Upvote 0
Yes.
You need to add a new sub eg:
Code: Copy
'Sends the query and asynchronously returns a RDCResult.
'Example: <code> Wait For (Manager.ExecuteQuery1("select_item", "sample")) Complete (Result As RDCResult)</code>
Public Sub ExecuteQuery1 (QueryName As String, Parameter As String) As ResumableSub
    Dim ser As B4XSerializator
    Dim data() As Byte = ser.ConvertObjectToBytes(CreateMap("command": CreateMap("name": "SQL." & QueryName, "parameters": Array(Parameter)),  "version": VERSION))
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostBytes(mLink & "?method=query2", data)
    j.GetRequest.SetContentType("application/octet-stream")
    Wait For (j) JobDone(j As HttpJob)
    Dim res As RDCResult
    If j.Success Then
        Dim ser As B4XSerializator
        Dim data() As Byte = Bit.InputStreamToBytes(j.GetInputStream)
        ser.ConvertBytesToObjectAsync(data, "ser")
        Wait For (ser) ser_BytesToObject (Success As Boolean, NewObject As Object)
        If Success Then
            res = CreateRDCResult(True, "", LOAUtils.WrapWithHeader(NewObject))
        Else
            res = CreateRDCResult(False, "Error during serialization: " & LastException, Null)
        End If
    Else
        res = CreateRDCResult(False, j.ErrorMessage, Null)
    End If
    j.Release
    Return res
End Sub

Code: Copy
Private Sub TestExecuteQueryWithSingleParameter (Parameter As String)
    Wait For (rdc.ExecuteQuery1("SELECT_PRODUCT_BY_CATEGORY_NAME", Parameter)) Complete (Result As RDCResult) 'queries are defined in the server config.properties file
    If Result.Success Then
        Log(Result.Data.ToString(0))
        For Each row() As Object In Result.Data.IterateRows
            Dim M1 As Map = CreateMap("id": row(0), "category name": row(2), "product_name": row(4), "product_price": row(5))
            Log(M1)
        Next
    Else
        Log(Result.ErrorMessage)
    End If
End Sub

Calling:
B4X: Copy
TestExecuteQueryWithSingleParameter("Toys")

Take example from my template https://www.b4x.com/android/forum/threads/project-template-jrdc3-server.171350/
The test query in config.properties.
Code: Copy
SQL.SELECT_PRODUCT_BY_CATEGORY_NAME=SELECT p.id, c.id AS category_id, c.category_name, p.product_code, p.product_name, p.product_price FROM tbl_products AS p LEFT JOIN tbl_category AS c ON p.category_id = c.id WHERE c.category_name = ?
 
Last edited:
Upvote 0
Dear Aeric,

I get <body><h2>HTTP ERROR 500 java.lang.ClassCastException: class java.lang.String cannot be cast to class [Ljava.lang.Object; (java.lang.String and [Ljava.lang.Object; are in module java.base of loader &apos;bootstrap&apos;)</h2> Can it be a library update issue? Thanks.
 
Upvote 0
Dear Aeric,

I get <body><h2>HTTP ERROR 500 java.lang.ClassCastException: class java.lang.String cannot be cast to class [Ljava.lang.Object; (java.lang.String and [Ljava.lang.Object; are in module java.base of loader &apos;bootstrap&apos;)</h2> Can it be a library update issue? Thanks.
Are you using RDC_Client.b4xlib ?
How do you add ExecuteQuery1 sub?

Option 1: Leave RDC_Client.b4xlib checked but also add RDCManager.bas (which has ExecuteQuery1 sub added as from post #5 above). It means you don't need to check other libraries required by RDCManager.bas.

Option 2: Don't check RDC_Client.b4xlib in Libraries Manager tab. You need to add RDCManager.bas as in Option 1 and also need to check the libraries depends on platform you are working on as listed in manifest.txt.
B4X: Copy
B4A.DependsOn=ListOfArrays, OkHttpUtils2, RandomAccessFile
B4i.DependsOn=ListOfArrays, iHttpUtils2, iRandomAccessFile
B4J.DependsOn=ListOfArrays, jOkHttpUtils2, jRandomAccessFile

Option 1 is more straight forward but Option 2 is cleaner.

Attached is the modified RDCManager that you can use.
 

Attachments

Upvote 0
Back
Top