B4J Question Could not connect Postgresql Database on web server

Pravee7094

Active Member
Hi all,
I tried to connect postgresql database with my local server in b4j. It will be perfectly alright.
I wish to upload my small project on web server using File Zilla Client and PuTTY with correct configuration.
But I coudn't connect the web server database using the same coding which is running on local server. [ corrctly configured for web server database ]

The coding I used : [Main]
B4X:
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
    #AdditionalJar: postgresql-42.2.6.jar
#End Region

Sub Process_Globals
    Private srvr As Server
    Dim is_sql As SQL
End Sub

Sub AppStart (Args() As String)
    
    Dim Database_host As String = "host_name"
    Dim Database_port As Int = 5432
    Dim Database_Name As String = "database_name"
    Dim DB_Username As String = "postgres"
    Dim DB_Password As String = "password"

    srvr.Initialize("srvr")
    srvr.Port = 17178
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    
    Dim ConnectionString As String=$"jdbc:postgresql://${Database_host}:${Database_port}/${Database_Name}"$
    is_sql.InitializeAsync("PostgreSql","org.postgresql.Driver", ConnectionString,DB_Username,DB_Password)
    is_sql.Close

    srvr.AddWebSocket("/Login", "Login")
    srvr.Start
    StartMessageLoop
End Sub

Sub PostgreSql_Ready (Success As Boolean)
    If Success Then
        Dim ls_resultset As ResultSet = is_sql.ExecQuery("SELECT username FROM table_name")
        Do While ls_resultset.NextRow
            Dim ls_username As String = ls_resultset.GetString("username")
            Log("Username : " & ls_username)
        Loop
        ls_resultset.Close
    
    End If
'    StopMessageLoop 'only required in a console app
End Sub

websocket : Login

B4X:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket
    Private txtemail,txtpassword,btnlogin As JQueryElement
    
End Sub

Public Sub Initialize
    
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
        ws = WebSocket1
            
End Sub

Private Sub WebSocket_Disconnected

End Sub

Sub btnlogin_Click (Params As Map)
    Try
        Dim ls_resultset As ResultSet
        ls_resultset = Main.is_sql.ExecQuery("select username, password from tablename where username = '" & txtemail.GetVal.Value & "' ")
        Do While ls_resultset.NextRow
            
            Dim ls_username As String = ls_resultset.GetString("username")
            Dim ls_password As String = ls_resultset.GetString("password")
            
        Loop
        ls_resultset.Close
                
        If txtpassword.GetVal.Value == ls_password Then
            ws.Eval("document.location.replace('index.html')",Null)
        Else
            ws.Eval("document.location.replace('login.html')",Null)
            
        End If
        
    Catch
        Log(LastException)
    End Try
End Sub

When I go to ip : port/login.html and enter the credentials click nothing happened. Database could not connect.

Any suggestion?

Regards
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
Hey dont use sql directly it will clog your app.

You have to use a pool

 
Upvote 0
Top