B4J Question Looking for Database connection from B4J to Oracle

AndroidMadhu

Active Member
Licensed User
Hi,
I am trying to connect oracle database from B4j.
But I am not able to find any suitable example to connect to Oracle from B4J.

Can anyone advice on this.
 

DonManfred

Expert
Licensed User
Longtime User

Find a odbc driver and use it

Check this thread:
 
Last edited:
Upvote 1

jkhazraji

Active Member
Licensed User
Longtime User
Here is a functional code (taken from the forum)
Just download 'ojdbc8.jar' and place it in the proper location (libraries)
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True

 
    #AdditionalJar: ojdbc8.jar
#End Region

Sub Process_Globals
    Private SQL As SQL

#Region Database Location
    Private DBLocation As String = "DBLocation" 'Can use IP address or domain name
    Private DBUsername As String = "DBUsername"
    Private DBPassword As String = "DBPassword"
#End Region
End Sub

Sub AppStart (Args() As String)

    LogError("---------- XE Database (Oracle) ----------")
    SQL.InitializeAsync("Oracle", "oracle.jdbc.driver.OracleDriver", $"jdbc:oracle:thin:@${DBLocation}:1521:xe"$, DBUsername, DBPassword)
    SQL.Close
    StartMessageLoop 'only required in a console app
    
    ExitApplication
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 Oracle SQL Server
Sub Oracle_Ready (Success As Boolean)
    If Success Then
        Dim RS As ResultSet = SQL.ExecQuery("SELECT table_name FROM user_tables ")
        Do While RS.NextRow
            Log(RS.GetString2(0))
        Loop
        RS.Close
    End If
    StopMessageLoop 'only required in a console app
End Sub
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Thanks for the advice. I am able to connect Oracle Database successfully.
At One point I am bit confused. While connection of Oracle Database at Remote Server, it is taking some time.
How do I ensure, that as long as the DB is not connected, no other work cannot be done.

Below is my working test code.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private dbsrv As String="xx.xx.xx.xx"
    Private dbusername As String="xx_xx"
    Private dbpwd As String= "xx"
    Private sql As SQL
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    sql.InitializeAsync("Oracle", "oracle.jdbc.driver.OracleDriver", $"jdbc:oracle:thin:@${dbsrv}:1521:mmjd"$, dbusername, dbpwd)
    If sql.IsInitialized Then
        'xui.MsgboxAsync("Oracle Connected Success!!","Success")
        Log("connection success") ==> The msg coming when I click the button second or Thrice
    End If
    sql.Close
End Sub

Any advice why I am getting the success msg after click the button 3 times?

Thanks
 
Upvote 0
Top