Android Question Remote server connection how to port B4J code (working) to B4A (SQL.InitializeAsync)

Diego Marcelo Croatto

Member
Licensed User
Longtime User
Hi to all programmers.
I have a MariaDB server working in a Rasberry Pi (all Ok)
I tested the folow code to get data over Wifi from my PC in B4J and works fine.
I need port it to Android and I have problems with the connection and some libraries can ayone help me?.... Thank's a lot ;)

The log Result is:

Captura de pantalla 2024-06-29 194409.jpg



The working code in B4J its simple as....

Get remote data from MariaDB Server:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

    'MariaDB Connector/J Driver
    #AdditionalJar: mariadb-java-client-2.4.2.jar

Sub Process_Globals
    Private SQL As SQL
    Dim RS As ResultSet
    
#Region Database Location
    Private DBLocation As String = "192.168.xxx.xxx:1818" '<-- IP address
    Private DBUsername As String = "user" '<-- DB user name
    Private DBPassword As String = "123456" '<-- DB password
    Private SQLCommand As String            '<-- Auxiliar string to see the text
    
#End Region
End Sub

Sub AppStart (Args() As String)

    LogError("---------- NorthWind Database (MariaDB) ----------")
    SQLCommand=$"jdbc:mariadb://${DBLocation}:3306/"$                         
    SQL.InitializeAsync("MariaDB", "org.mariadb.jdbc.Driver",SQLCommand , DBUsername, DBPassword)
    SQL.Close
    StartMessageLoop 'only required in a console app

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

'Connected to MariaDB SQL Server
Sub MariaDB_Ready (Success As Boolean)
    
    If Success Then
        RS = SQL.ExecQuery("SELECT x.* FROM TabletsDB.Calibracion x")
        
        Do While RS.NextRow
            Log(RS.GetString2(0)) 'First register
            Log(RS.GetString2(1))
            Log(RS.GetString2(2))
            Log(RS.GetString2(3))
            Log(RS.GetString2(4))
            Log(RS.GetString2(5))
            Log(RS.GetString2(6))
            Log(RS.GetString2(7)) 'Last register
        Loop
        
        RS.Close
        
    End If
    StopMessageLoop 'only required in a console app
End Sub
 

Diego Marcelo Croatto

Member
Licensed User
Longtime User
Well... I try a lot of time and nothing.... in left monitor I test the B4A code with JdbcSQL (not working) and in the right monitor the B4J code with JSQL library (the reference working OK)

Both (B4A and B4J) are the same additional line
#AdditionalJar:
#AdditionalJar: mariadb-java-client-2.4.2.jar
The B4J Connection code (working)
B4J MariaDB server connection code:
    SQLCommand="jdbc:mariadb://192.168.1.59:3306/"                         
    SQL.InitializeAsync("MariaDB", "org.mariadb.jdbc.Driver",SQLCommand , DBUsername, DBPassword)
    SQL.Close
And the B4A Connection code (not working)
B4A MariaDB connection code:
    SQLCommand="jdbc:mariadb://192.168.1.59:3306/"                        
    sql1.InitializeAsync("MariaDB", "org.mariadb.jdbc.Driver",SQLCommand , DBUsername, DBPassword)
    sql1.Close

And the same Sub for read one row with 8 columns
Result Database:
'Connected to MariaDB SQL Server
Sub MariaDB_Ready (Success As Boolean)
    If Success Then
        RS = sql1.ExecQuery("SELECT x.* FROM TabletsDB.Calibracion x")
        Do While RS.NextRow
            Log(RS.GetString2(0))
            Log(RS.GetString2(1))
            Log(RS.GetString2(2))
            Log(RS.GetString2(3))
            Log(RS.GetString2(4))
            Log(RS.GetString2(5))
            Log(RS.GetString2(6))
            Log(RS.GetString2(7))
        Loop
        RS.Close
    End If
End Sub

This is the connection results (Debugging) of B4A in sql1 --> Connection is null (all is null)
1719796455962.png

This is the connection results (Debugging) of B4J in SQL --> Connection is with data
1719796691233.png


What I do wrong?.... what is the error in the Android platform? The WiFi connection its Ok between the android device and the DB server.
Thank's a lot for the help.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What is the purpose of this strange code:
B4X:
SQLCommand="jdbc:mariadb://192.168.1.59:3306/"                        
    sql1.InitializeAsync("MariaDB", "org.mariadb.jdbc.Driver",SQLCommand , DBUsername, DBPassword)
    sql1.Close
?

Post the output of this code:
B4X:
sql1.InitializeAsync("MariaDB", "org.mariadb.jdbc.Driver",SQLCommand , DBUsername, DBPassword)
 Wait For MariaDB_Ready (Success As Boolean)
 Log(Success & ": " & LastException)
sql1.Close
 
Upvote 0

Diego Marcelo Croatto

Member
Licensed User
Longtime User
? Erel I'm sorry ... the strange code is for explain why I can't connect form android devices to the remote database. But it seems that my explanation was not clear enough.
The idea is to connect from a tablets (android) to remote intranet database server (raspberry pi) for register the data sended from the tablets.
Raspberry Pi is a economical way to mount a server (mariadb/mysql) with minimal recurses lot off storage and access it from many ways to the data stored.

SQLCommand is for the debugger and the purpose is to see if the string to be sent is correct.

I try to explain:
I write the code in B4J and works ok with this libraries:
B4J:
    Private SQL As SQL
    Dim RS As ResultSet
I write the same code in B4A and not connect to database with this libraries:
B4A:
    Dim sql1 As JdbcSQL
    Dim RS As JdbcResultSet

I cant find the code portability between B4J and B4A.
 
Upvote 0
Top