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
Top