Android Question Access database name from client parameter with JRDC ?

PumaCyan

Member
Licensed User
I have problems getting data from several databases in one host, for example :
dbsample_01
dbsample_02

etc...

how do i pass parameters to jrdc's config.properties?
or there are special changes to be able to access between databases?

how to get the data with the following query?
B4X:
select * from dbsample_01.table_A;
select * from dbsample_02.table_A;

this is the query in my jrdc config.properties :
SQL:
sql.list_datatable = select * from dbsample_?.table_A;


sample code from client :
B4X:
Dim strNumberDB as String = "01"
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("list_datatable", Array(strNumberDB))
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
    ....
End If

And the result :

B4X:
(MySQLSyntaxErrorException) com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near ''01'.table_A LIMIT 0,25' at line 1
 
Last edited:
Solution
I modify the JRDC2 Multi so we can pass the DB number instead of the DB Name.
JRDC Server (Main Sub):
Sub GetDBName (DB As String) As String
    Select Case DB
        Case "01"
            Return "dbsample_01"
        Case "02"
            Return "dbsample_02"
        Case Else
            Return ""                 ' set to empty string to throw error
            'Return "dbsample_01"     ' default to "dbsample_01
    End Select
End Sub

We can use parameter "db" to pass the DB Number value (e.g. http://127.0.0.1:17178/test?db=01) and then by using GetDBName return the actual DB Name to pass to RDC Connector.

JRDC Server TestHandler.bas:
Dim DB As String = req.GetParameter("db")
Dim connector As RDCConnector = Main.Connectors.Get(Main.GetDBName(DB))

In Client app, we...

aeric

Expert
Licensed User
Longtime User
Here is one of the ways:
 
Upvote 0

PumaCyan

Member
Licensed User

The above code, Does it use files from this source?

JRDC Multi
 
Upvote 0

PumaCyan

Member
Licensed User
I have tried the following code :

Main B4J:
Sub AppStart (Args() As String)
    srvr.Initialize("")
    Connectors = srvr.CreateThreadSafeMap
    Dim dbs As List = Array("database_01", "database_02") 'read from a file
    For Each db As String In dbs
        Dim con As RDCConnector
        con.Initialize(db)
        Connectors.Put(db, con)
    Next
    srvr.Port = 17178
    srvr.AddHandler("/test", "TestHandler", False)
    srvr.AddHandler("/rdc", "RDCHandler", False)
    srvr.Start
    Log($"jRDC is running (version = $1.2{VERSION})"$)
    StartMessageLoop

RDC Connector:
Sub Class_Globals
    Private pool As ConnectionPool
    Private DebugQueries As Boolean
    Private commands As Map
    Public serverPort As Int
    Public mDBName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (DBName As String)
    mDBName = DBName
    Dim config As Map = LoadConfigMap
    Dim jdbc As String = config.Get("JdbcUrl")
    jdbc = jdbc.Replace("$DB$", mDBName)
    pool.Initialize(config.Get("DriverClass"), jdbc, config.Get("User"), _
        config.Get("Password"))
#if DEBUG
    DebugQueries = True
#else
    DebugQueries = False
#end if
    serverPort = config.Get("ServerPort")
    LoadSQLCommands(config)
End Sub

however, I found the database connection failed

 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
however, I found the database connection failed
Is the output from the /test URL? if so, did you include ?DBName=database_01 as part of the URL? (for example: 127.0.0.1:17178/test?DBName=database_01)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I modify the JRDC2 Multi so we can pass the DB number instead of the DB Name.
JRDC Server (Main Sub):
Sub GetDBName (DB As String) As String
    Select Case DB
        Case "01"
            Return "dbsample_01"
        Case "02"
            Return "dbsample_02"
        Case Else
            Return ""                 ' set to empty string to throw error
            'Return "dbsample_01"     ' default to "dbsample_01
    End Select
End Sub

We can use parameter "db" to pass the DB Number value (e.g. http://127.0.0.1:17178/test?db=01) and then by using GetDBName return the actual DB Name to pass to RDC Connector.

JRDC Server TestHandler.bas:
Dim DB As String = req.GetParameter("db")
Dim connector As RDCConnector = Main.Connectors.Get(Main.GetDBName(DB))

In Client app, we append the db parameter to rdc link.
B4X:
Sub CreateRequest (DB As String) As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, rdcLink & "?db=" & DB)
    Return req
End Sub
 

Attachments

  • jRDCMulti.zip
    4.9 KB · Views: 185
  • JRDC2MultiClient.zip
    16.5 KB · Views: 185
Upvote 0
Solution

PumaCyan

Member
Licensed User

thank you very much

this is very easy to understand, I missed the code in the test handler of the original JRDC modification of course, including forgetting to put the database fetch code... sorry I'm not careful

once again thank you very much for your help @aeric

Is the output from the /test URL? if so, did you include ?DBName=database_01 as part of the URL? (for example: 127.0.0.1:17178/test?DBName=database_01)

thank you too @OliverA, the error is due to my mistake in calling the URL
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…