B4J Question MSSQL DRIVER Issue

Peter Lewis

Active Member
Licensed User
Longtime User
Hi, I have the following additional jar
B4X:
#AdditionalJar:mssql-jdbc-12.8.2.jre11
#AdditionalJar: HikariCP-5.0.1
then I am using Java jdk-19.0.2
To init the pool we are using

B4X:
Public pool As HikariCP
pool.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver",$"jdbc:sqlserver://${host}:${port};databaseName=${database}"$,user,password)

with the following code starting sql

B4X:
Dim sql As SQL = pool.GetConnection

The error code I am getting is as follows

java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=com.microsoft.sqlserver.jdbc.SQLServerDriver

The .jar file is in the additional files folder and I cannot see what I have done wrong.

Thank you
 

besoft

Active Member
Licensed User
Longtime User
Try this:

string:
pool.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver", _
    $"jdbc:sqlserver://$host$:$port$;databaseName=$database$;encrypt=true;trustServerCertificate=true"$, _
    user, password)

Newer mssql-jdbc drivers have encryption enabled by default, and if the certificate is not trusted, the connection fails (sometimes it shows as "Failed to get driver instance..." or as an SSL error, depending on where the exception is caught).
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Try this:

string:
pool.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver", _
    $"jdbc:sqlserver://$host$:$port$;databaseName=$database$;encrypt=true;trustServerCertificate=true"$, _
    user, password)

Newer mssql-jdbc drivers have encryption enabled by default, and if the certificate is not trusted, the connection fails (sometimes it shows as "Failed to get driver instance..." or as an SSL error, depending on where the exception is caught).

I tried it with the same error code. Thank you for trying
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=com.microsoft.sqlserver.jdbc.SQLServerDriver
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
Try without port. My connection string:
string2:
sqlMS.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver://XXX\MSSQLSERVER;databaseName=dbname;user=user;password=password;encrypt=true;trustServerCertificate=true")

Pay attention to how the server name is written. If the server is on a local network and whether the server is accessible via the web.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
This is what I do using java 19 (v: is a network drive where is drive is located)

B4X:
#AdditionalJar: V:\jdbc\sqljdbc_13.2\enu\jars\mssql-jdbc-13.2.1.jre11.jar

B4X:
       u = username
       p = password
       sqlHost = '//yourdomain.com:port'
       driveClass = $"com.microsoft.sqlserver.jdbc.SQLServerDriver"$
        jdbcUrl = $"jdbc:sqlserver:${sqlHost};encrypt=false;trustServerCertificate=false;loginTimeout=10"$
        dbSQL.Initialize(driveClass,jdbcUrl, u, p)

Hope this helps

John
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I have tried all the options suggested.

I then made my mqtt server address blank and i do not get an error on the MSSQL


Waiting for debugger to connect...
Program started.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Adding credentials
Adding Connection Test Query
mssql 192.168.3.254
Emulated network latency: 100ms
mqtt (No IP address)
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.eclipse.paho.client.mqttv3.internal.NetworkModuleService (file:/C:/Program%20Files/Anywhere%20Software/B4J/Libraries/jMQTT.jar) to field java.net.URI.userInfo
WARNING: Please consider reporting this to the maintainers of org.eclipse.paho.client.mqttv3.internal.NetworkModuleService
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Error connecting MQTT.
Error connecting MQTT.

The minute I put MQTT back
Waiting for debugger to connect...
Program started.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Adding credentials
Adding Connection Test Query
mssql192.168.3.254
Emulated network latency: 100ms
mqtt 192.168.3.254
connected
$SYS/broker/clients/active
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=com.microsoft.sqlserver.jdbc.SQLServerDriver
Clients Active 0
$SYS/broker/clients/active
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=com.microsoft.sqlserver.jdbc.SQLServerDriver
Clients Active 1

The Port for MSSQL is 1433 and MQTT is 1883
1765991144179.png
 
Upvote 0
Top