B4J Question another PostgreSQL + B4J Qn from a newbie

vnpython

Member
after tested a few framework for cross platform app development tool, shortlisted Flutter/Dart & B4X/B4J. I am able to get the flutter to connect to a localhost postgresql database and hoping to do the same for B4J. After going thru 7 pages of PostgreSQL related thread using the search tool, downloaded few jar for postgresql, using Bing AI, Bard AI yet i couldnt get it connect to my test postgresql database (even got the config.properties file up).

config.properties:
DriverClass=org.postgresql.Driver
JdbcUrl=jdbc:postgresql://localhost/hd_app?characterEncoding=utf8
User=postgres
Password=Demo1234
ServerPort=5432

I am coming from MS Access (self-taught) and B4J will be more familiar for me than flutter, and hope to embark with B4J to port my access program over to postgresql + B4J. I got the AdditionalLibs path (D:\B4X\AdditionalLibs) set up on config path, downloaded the required jar (postgresql-9.4.1212.jre7.jar & postgresql-42.6.0.jar) and now i need some senior members to help write a test connection to my localhost postgresql database. The closest i can get is still with the error saying: " Waiting for debugger to connect... Program started. Error: java.lang.RuntimeException: Class not found: jdbc: postgresql://localhost:5432/hp_app?user=postgres&password=Demo1234 & Are you missing an #AdditionalJar attribute setting?"


almost but not there:
#AdditionalJar: postgresql-42.6.0.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private pgSQL As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Try
        Dim connectionString As String = "jdbc:postgresql://localhost:5432/hp_app?user=postgres&password=Demo1234"
        pgSQL.Initialize(connectionString, "hp_app")
        Log("Connection successful!")
    Catch
        Log("Error: " & LastException.Message)
    End Try
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    pgSQL.Close
End Sub

I will be grateful and thankful for this guide to get it connected to further my assessment for the porting project. Many thanks for your time reading and helping a newbie to some hands-on. Shalom!
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
pgSQL.Initialize(connectionString, "hp_app")
This part should be:
pgSQL.initialize("org.postgresql.Driver",connectionString)

if you hover over the initialize it will give you the parameters and the order you should input.

config.properties:
this is not needed unless you are working with JRDC2, as your second snippet is a UI i guess that's not the case. so better to stick with your second snippet code
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
You should use sql.Initialize2

B4X:
DriverClass=org.postgresql.Driver
JdbcUrl=jdbc:postgresql://localhost/hp_app
User=postgres
Password=Demo1234

Main:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public config As Map
    Public sql As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    config = LoadConfigMap
    sql.Initialize2(config.Get("DriverClass"), _
    config.Get("JdbcUrl"), _
    config.Get("User"), _
    config.Get("Password"))
End Sub

Private Sub LoadConfigMap As Map
    Return File.ReadMap(File.DirAssets, "config.properties")
End Sub

If you are using jRDC2 Connection Pool then,
B4X:
pool.Initialize(Main.config.Get("DriverClass"), _
Main.config.Get("JdbcUrl"), _
Main.config.Get("User"), _
Main.config.Get("Password"))
 
Last edited:
Upvote 0

vnpython

Member
You should use sql.Initialize2

B4X:
DriverClass=org.postgresql.Driver
JdbcUrl=jdbc:postgresql://localhost/hd_app
User=postgres
Password=Demo1234

B4X:
Dim sql As SQL
sql.Initialize2(Main.config.Get("DriverClass"), _
Main.config.Get("JdbcUrl"), _
Main.config.Get("User"), _
Main.config.Get("Password"))

If you are using jRDC2 Connection Pool then,
B4X:
pool.Initialize(Main.config.Get("DriverClass"), _
Main.config.Get("JdbcUrl"), _
Main.config.Get("User"), _
Main.config.Get("Password"))
thanks for this guide also. let me try both and revert. 谢谢你啦马来西亚大神
 
Upvote 0

vnpython

Member
1691405893669.png
1691405936852.png
1691406211583.png

After a day at work (no spare time to try out), finally able to test the code out. somehow the b4x didnt 'see' my jar file and kept saying cannot locate the libraries at a location (D:\Program Files\Anywhere Software\B4J\Libraries) and after I added the jar file into this location, the code worked! I also include jSQL using the libraries manager. Hope this would help those coming in late into B4X.

working main:
#AdditionalJar: postgresql-42.6.0.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private pgSQL As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Try
        Dim connectionString As String = "jdbc:postgresql://localhost:5432/my_app?user=postgres&password=Demo1234"
        pgSQL.Initialize("org.postgresql.Driver", connectionString)
        Log("Connection successful!")
    Catch
        Log("Error: " & LastException.Message)
    End Try
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    Try
        pgSQL.Close
        Log("Connection closed successfully!")
    Catch
        Log("Error: " & LastException.Message)
    End Try
End Sub
 
Upvote 0
Top