Android Question jdbc problem connection not good.

3394509365

Active Member
Licensed User
i wrote this code in the Main
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'#AdditionalJar: mysql-connector-java-5.1.34-bin
#AdditionalJar: mysql-connector-java-5.1.40-bin
#BridgeLogger: true
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ProgressBar1 As ProgressBar
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")

End Sub

Sub Activity_Click
    ProgressBar1.Visible = True
    Wait For (CallSub(Starter, "ListAnimals")) Complete (Result As Boolean)
    Log("Completed. Success: " & Result)
    ProgressBar1.Visible = False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
and i have this code in the Starter

B4X:
#Region  Service Attributes 


    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public mysql As JdbcSQL
    Private driver As String = "com.mysql.jdbc.Driver"
    Private jdbcUrl As String = "jdbc:mysql://192.168.0.2/SQLTutorial"
    Private Username As String = "pippo"
    Private Password As String = "xxxxx"
End Sub
Sub Service_Create
    DisableStrictMode
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub DisableStrictMode
    Dim jo As JavaObject
    jo.InitializeStatic("android.os.Build.VERSION")
    If jo.GetField("SDK_INT") > 9 Then
        Dim policy As JavaObject
        policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
        policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
        Dim sm As JavaObject
        sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
    End If
End Sub

Sub Connect As ResumableSub
    mysql.InitializeAsync("mysql", driver, jdbcUrl, Username, Password)
    Wait For MySQL_Ready (Success As Boolean)
    If Success = False Then
        Log("Check unfiltered logs for JDBC errors.")
    End If
    Return Success
End Sub

Sub CloseConnection
     mysql.Close
End Sub

Sub ListAnimals As ResumableSub
    Wait For (Connect) Complete (Success As Boolean)
    If Success Then
        Try
            Dim sf As Object = mysql.ExecQueryAsync("mysql", "SELECT ID, PartNo FROM Products WHERE ID > ?", Array(300))
            Wait For (sf) mysql_QueryComplete (Success As Boolean, Crsr As JdbcResultSet)
            If Success Then
                Do While Crsr.NextRow
                    Log($"ID: ${Crsr.GetInt("ID")}, PartNo: ${Crsr.GetString("PartNo")}"$)
                Loop
                Crsr.Close
            End If
        Catch
            Success = False
            Log(LastException)
        End Try
        CloseConnection
    End If
    Return Success
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

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

Sub Service_Destroy

End Sub
I'm trying to at least connect to my database on Mysql server of my pc from which I work.
but it never connects. And I have no mistakes,

can you help me? pleace.
 

DonManfred

Expert
Licensed User
You are connected to the same network with your device? 192.168.0.*

Check unfiltered logs for JDBC errors.
do you get this output in your log?
What does
B4X:
        Log(LastException)
output in case success is FALSE when calling Connect?
 
Last edited:

3394509365

Active Member
Licensed User
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: failed to connect to /192.168.0.2 (port 3306) from /:: (port 51858): connect failed: ECONNREFUSED (Connection refused)
 

3394509365

Active Member
Licensed User
I added the line of code you suggested and the result is this

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Check unfiltered logs for JDBC errors.
(ErrnoException) android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
Completed. Success: false
 

3394509365

Active Member
Licensed User
if instead of the ip address I put the server name Change something?
B4X:
    sql.Initialize2("com.mysql.jdbc.Driver", "jdbc:mysql://LAPTOP-31M6JO38/SQLTutorial","pippo","xxxxx")
 

3394509365

Active Member
Licensed User
finally I managed to talk to the server, but with the driver "" net.sourceforge.jtds.jdbc.Driver "
but is it a safe driver or would it be better "com.mysql.jdbc.Driver"? since I use sql server from microsoft?

But while with the first driver I can connect, with the second no.
I followed these instructions in a tutorial by Erel and wrote like this:

B4X:
#AdditionalJar: mysql-connector-java-5.1.40-bin.jar
B4X:
Public mysql As JdbcSQL
    Private driver As String = "com.mysql.jdbc.Driver"
    Private jdbcUrl As String = "jdbc:mysql://192.168.56.1/SQLTutorial"
    Private Username As String = "pippo"
    Private Password As String = "xxxxx"
B4X:
mysql.InitializeAsync("mysql", driver, jdbcUrl, Username, Password)
this is the error in the LOG:

Check unfiltered logs for JDBC errors.

Are you missing an #AdditionalJar attribute setting?
 
Last edited:

OliverA

Expert
Licensed User
Top