German java.lang.reflect.InvocationTargetException

bfw

New Member
Guten Morgen,

ich versuche eine Verbindung zu einer lokalen MariaDB herzustellen. Die MariaDB an sich läuft soweit, mit z. B. HeidiSQL kann ich problemlos darauf zugreifen. In meinem Main Module habe ich die Zeile
B4X:
#AdditionalJar: mariadb-java-client-3.5.1.jar
hinzugeüfgt, das scheint auch zu klappen. In meinem Hauptprogramm habe ich folgende zwei Methoden ergänzt:

Zugriff zur Datenbank:
Sub ConnectToMariaDB(jdbcURL As String, username As String, password As String) As JavaObject
    Dim driver As JavaObject
    driver.InitializeNewInstance("org.mariadb.jdbc.Driver", Null)
    Dim connection As JavaObject = driver.RunMethod("connect", Array(jdbcURL, CreateCredentials(username, password)))
    Return connection
End Sub

Sub CreateCredentials(username As String, password As String) As JavaObject
    Dim properties As JavaObject
    properties.InitializeNewInstance("java.util.Properties", Null)
    properties.RunMethod("setProperty", Array("user", username))
    properties.RunMethod("setProperty", Array("password", password))
    Return properties
End Sub

In der Methode
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
schreibe ich dann folgendes

B4X:
Try
    Dim jdbcURL As String = "jdbc:mariadb://localhost/mydb"
    sqlConnection = ConnectToMariaDB(jdbcURL, "username", "password")
Catch
    Log(LastException)
End Try

und erhalte diesen Stacktrace:

B4X:
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.program.b4xmainpage._connecttomariadb(b4xmainpage.java:101)
    at b4a.program.b4xmainpage._b4xpage_created(b4xmainpage.java:86)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1087)
    at b4a.program.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1068)
    at b4a.program.b4xpagesmanager._showpage(b4xpagesmanager.java:425)
    at b4a.program.b4xpagesmanager._addpage(b4xpagesmanager.java:245)
    at b4a.program.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:259)
    at b4a.program.b4xpagesmanager._initialize(b4xpagesmanager.java:165)
    at b4a.program.main._activity_create(main.java:415)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at b4a.program.main.afterFirstLayout(main.java:105)
    at b4a.program.main.access$000(main.java:17)
    at b4a.program.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.sql.SQLNonTransientConnectionException: Socket fail to connect to localhost. failed to connect to localhost/127.0.0.1 (port 3306) from /127.0.0.1 (port 32864) after 30000ms: isConnected failed: ECONNREFUSED (Connection refused)
    at org.mariadb.jdbc.client.impl.ConnectionHelper.connectSocket(ConnectionHelper.java:131)
    at org.mariadb.jdbc.client.impl.StandardClient.<init>(StandardClient.java:127)
    at org.mariadb.jdbc.Driver$$ExternalSyntheticLambda1.apply(D8$$SyntheticClass:0)
    at org.mariadb.jdbc.Driver.connect(Driver.java:73)
    at org.mariadb.jdbc.Driver.connect(Driver.java:102)
    at org.mariadb.jdbc.Driver.connect(Driver.java:27)
    ... 38 more
Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3306) from /127.0.0.1 (port 32864) after 30000ms: isConnected failed: ECONNREFUSED (Connection refused)
    at libcore.io.IoBridge.isConnected(IoBridge.java:278)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:187)
    at libcore.io.IoBridge.connect(IoBridge.java:129)
    at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
    at java.net.Socket.connect(Socket.java:621)
    at org.mariadb.jdbc.client.impl.ConnectionHelper.connectSocket(ConnectionHelper.java:118)
    ... 43 more
Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
    at libcore.io

Hat jemand eine Idee, wieso keine Verbindung zur lokalen MariaDB hergestellt werden kann?
 

DonManfred

Expert
Licensed User
Longtime User
Du brauchst keinen Client, Du brauchst einen odbc-treiber.
Den kannst Du dann ganz einfach mit der jSQL Library einbinden. Also in B4J.

In b4a musst Du die entsprechende Library verwenden wobei der direkte Datenbankzugriff von Android nicht empfohlen ist.

Zurück zur B4J

B4X:
#AdditionalJar: mysql-connector-java-5.1.34-bin
in main

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public db As SQL
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    db.InitializeAsync("DB","com.mysql.jdbc.Driver", "jdbc:mysql://host:port/dbname?characterEncoding=utf8&autoReconnect=true","username","passwort")

End Sub
Sub DB_Ready (Success As Boolean)
    If Success = False Then
        Return
    End If
  ' Mach was immer du willst mit der Datenbank...
END sub
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…