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

aeric

Expert
Licensed User
Longtime User
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

aminoacid

Active Member
Licensed User
Longtime User
Looks like your MQTT broker and MSSQL Server are running on the same computer. Use this to connect to the MQTT server and set "working=true" only after the connection to the MSSQL server has been established:

 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Looks like your MQTT broker and MSSQL Server are running on the same computer. Use this to connect to the MQTT server and set "working=true" only after the connection to the MSSQL server has been established:

Tried that and also tried a public MQTT server with still ther same issue
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Create a small project to test. Don't add MQTT.
If you want a quick test, try my jRDC2 Server template. Edit the config.properties file and test run it.

I have tested with JDK 19.0.2 and mssql-jdbc-12.8.2.jre11

config.properties:
## MS SQL Server configuration:
MSSQL.DriverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
MSSQL.JdbcUrl=jdbc:sqlserver://localhost:1433;databaseName={DBName};encrypt=false;trustServerCertificate=true
MSSQL.DBName=test
MSSQL.User=sa
MSSQL.Password=password
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Create a small project to test. Don't add MQTT.
If you want a quick test, try my jRDC2 Server template. Edit the config.properties file and test run it.

I have tested with JDK 19.0.2 and mssql-jdbc-12.8.2.jre11

config.properties:
## MS SQL Server configuration:
MSSQL.DriverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
MSSQL.JdbcUrl=jdbc:sqlserver://localhost:1433;databaseName={DBName};encrypt=false;trustServerCertificate=true
MSSQL.DBName=test
MSSQL.User=sa
MSSQL.Password=password
Thank you for the information.

I have never used a project template before. It is not seen in File > Open , or add existing Modules . I even renamed the extension to n4j and no luck

Please point me in the right direction.

Thank you
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I have never used a project template before
Project template is similar to b4xlib. Don't rename the extension.
Just drop the file into B4J additional libraries folder. Refresh the libraries tab (Right Click, Refresh) or restart B4J.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
also tried a public MQTT server with still ther same issue
Are you creating a B4J UI or non-UI app?
Is this a MQTT broker or client?
Can you list the libraries you are using?
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Are you creating a B4J UI or non-UI app?
Is this a MQTT broker or client?
Can you list the libraries you are using?
I am trying to make a simple app to test , The end product will have to be a non-ui

1766051278915.png


I cannot find the template here after I put the file in the additional libs folder and restarted b4j.

Cannot find the config.properties area.

I have not used b4j for over 1 year so please bear with me.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
The template should be appeared when you click on the menu File -> New, if you put the file in the correct directory.

Cannot find the config.properties area.
Once you have created a new project, you can find it inside the project's Files folder.

After you have edited the config.properties, save the file.
Open B4J IDE, click on the compilation dropdown select from "Default" change to "MSSQL". The dropdown is next to "Debug" dropdown.
 
Last edited:
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
The template should be appeared when you click on the menu File -> New, if you put the file in the correct directory.


Once you have created a new project, you can find it inside the project's Files folder.

After you have edited the config.properties, save the file.
Open B4J IDE, click on the compilation dropdown select from "Default" change to "MSSQL". The dropdown is next to "Debug" dropdown.
Hi

Went through that and a webpage opened.

1766053100828.png





This is the log window

Waiting for debugger to connect...
Program started.
Checking database...
Database found!
2025-12-18 12:14:37.239:INFO :cmvl.MLog:MLog-Init-Reporter: MLog clients using slf4j logging.
2025-12-18 12:14:37.339:INFO :cmvc.C3P0Registry:main: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Starting server...
2025-12-18 12:14:37.417:INFO :eek:ejs.Server:main: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2025-12-18 12:14:37.462:INFO :eek:ejss.DefaultSessionIdManager:main: Session workerName=node0
2025-12-18 12:14:37.471:INFO :eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@669513d8{/,file:///C:/SOURCE~2/testmss/Objects/www,AVAILABLE}
2025-12-18 12:14:37.475:INFO :eek:ejs.RequestLogWriter:main: Opened C:\source code\testmss\Objects\logs\b4j-2025_12_18.request.log
2025-12-18 12:14:37.484:INFO :eek:ejs.AbstractConnector:main: Started ServerConnector@14dda234{HTTP/1.1, (http/1.1)}{0.0.0.0:17178}
2025-12-18 12:14:37.487:INFO :eek:ejs.Server:main: Started Server@58ebfd03{STARTING}[11.0.9,sto=0] @1582ms
Emulated network latency: 100ms
jRDC2 is running (template version = 3.30)
2025-12-18 12:14:37.693:INFO :cmvci.AbstractPoolBackedDataSource:main: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 20000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge1oebfdkzpi6bjsqqj|40317ba2, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.microsoft.sqlserver.jdbc.SQLServerDriver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge1oebfdkzpi6bjsqqj|40317ba2, idleConnectionTestPeriod -> 600, initialPoolSize -> 3, jdbcUrl -> jdbc:sqlserver://localhost:1433;databaseName=dna;encrypt=false;trustServerCertificate=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 150, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {password=******, user=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
So it is working.
I am not sure about HikariCP.
Just as a matter of Interest, I decided to run the Server App on a remote PC connecting to my Server which runs MQTT and MSSQL and it is running perfectly with no change of code from 2 years ago. They always used to run on the same server. Nothing on the code has changed. The only item that changed is the Version of B4J.
host 192.168.3.254
2025-12-18 15:19:37.220:INFO :eek:ejs.Server:main: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2025-12-18 15:19:37.328:INFO :eek:ejss.DefaultSessionIdManager:main: Session workerName=node0
2025-12-18 15:19:37.353:INFO :eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@22ef9844{/,file:///C:/Users/peter/OneDrive/Documents/GitHub/DNA/dnaserver/Objects/www/,AVAILABLE}
2025-12-18 15:19:37.360:INFO :eek:ejs.RequestLogWriter:main: Opened C:\Users\peter\OneDrive\Documents\GitHub\DNA\dnaserver\Objects\logs\b4j-2025_12_18.request.log
2025-12-18 15:19:37.494:INFO :eek:ejs.AbstractConnector:main: Started ServerConnector@4387b79e{HTTP/1.1, (http/1.1)}{0.0.0.0:51042}
2025-12-18 15:19:37.505:INFO :eek:ejs.Server:main: Started Server@6eda5c9{STARTING}[11.0.9,sto=0] @699ms
connected MQTT
$SYS/broker/clients/active
2025-12-18 15:19:37.946:INFO :czh.HikariDataSource:Thread-8: HikariPool-1 - Starting...
2025-12-18 15:19:38.324:INFO :czh.HikariDataSource:Thread-8: HikariPool-1 - Start completed.
Clients Active 0
$SYS/broker/clients/active
Clients Active 1
 
Upvote 0
Top