B4J Question MSSQL DRIVER Issue

Peter Lewis

Well-Known 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

Well-Known Member
Licensed User
Longtime User

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

Well-Known 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



The minute I put MQTT back

The Port for MSSQL is 1433 and MQTT is 1883
 
Upvote 0

aminoacid

Well-Known 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

Well-Known Member
Licensed User
Longtime User
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

Well-Known Member
Licensed User
Longtime User
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

Well-Known 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



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

Well-Known Member
Licensed User
Longtime User
Hi

Went through that and a webpage opened.






This is the log window

 
Upvote 0

aeric

Expert
Licensed User
Longtime User
So it is working.
I am not sure about HikariCP.
 
Upvote 0

Peter Lewis

Well-Known 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…