Android Question jdbc problem connection not good.

Discussion in 'Android Questions' started by 3394509365, Apr 26, 2019.

  1. 3394509365

    3394509365 Active Member Licensed User

    i wrote this code in the Main
    Code:
    #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

    Code:
    #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 StringAs 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.
     
  2. DonManfred

    DonManfred Expert Licensed User

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

    do you get this output in your log?
    What does
    Code:
    Log(LastException)
    output in case success is FALSE when calling Connect?
     
    Last edited: Apr 26, 2019
  3. 3394509365

    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)
     
  4. DonManfred

    DonManfred Expert Licensed User

    Sounds like a firewall problem? Have you opened the port in your router to forward the request on port 3306 to 192.168.0.2
     
  5. 3394509365

    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
     
  6. 3394509365

    3394509365 Active Member Licensed User

    no, I didn't open it, how do you do it?
     
  7. DonManfred

    DonManfred Expert Licensed User

    Check your router documentation. i CANT help you here
     
  8. 3394509365

    3394509365 Active Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    No. Better to use the ip address.
    Windows firewall blocks incoming connections be default. You need to open the port.
     
  10. 3394509365

    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:

    Code:
    #AdditionalJar: mysql-connector-java-5.1.40-bin.jar
    Code:
    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"
    Code:
    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: Apr 30, 2019
  11. OliverA

    OliverA Expert Licensed User

    José J. Aguilar likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice