B4J Question B4J MySQL connection Error

Declan

Well-Known Member
Licensed User
Longtime User
I am attempting to connect to a MySQL database.
The MySQL database name "ebuki".
I receive the following error:
B4X:
Waiting for debugger to connect...
Program started.
An error occurred:
(Line: 16) sql1.Initialize("com.mysql.jdbc.Driver", "jdbc:my
java.lang.NumberFormatException: For input string: "jdbc:mysql://localhost/ebuki?"

My Code:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
    #AdditionalJar: mysql-connector-java-5.1.40-bin.jar
#End Region

Sub Process_Globals
    Public srvr As Server
    Private sql1 As SQL
End Sub

Sub AppStart (Args() As String)
  
'    sql1.Initialize("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/ebuki?characterEncoding=utf8")
    sql1.Initialize("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/ebuki?" + "user=root&password=Ecowatch!@#$%")
  
    srvr.Initialize("")
    srvr.Port = 32009
    srvr.AddWebSocket("/ws", "B4A")
    srvr.Start
    StartMessageLoop
End Sub
 

keirS

Well-Known Member
Licensed User
Longtime User
Try

B4X:
sql1.Initialize("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/ebuki?" & "user=root&password=Ecowatch!@#$%")

The & operator is used to concatenate strings.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I have tried replacing the "+" with "&", but now have the following error:
B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 17
java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
    at java.net.URLDecoder.decode(URLDecoder.java:187)
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:649)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:319)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at anywheresoftware.b4j.objects.SQL.Initialize2(SQL.java:56)
    at anywheresoftware.b4j.objects.SQL.Initialize(SQL.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:656)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:29)
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Simplest thing to do is to change your password to one which is only alphanumeric. Alternatively star reading about regex escape sequences.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I cannot change the password - too many apps out there using the database.
However I sorted this out with:
Dim MyDBConn As String = "user=root&password=Ecowatch!@#$%"
MyDBConn = MyDBConn.Replace("%", "%25")
 
Upvote 0
Top