B4J Question Microsoft JDBC Driver - Problem connecting to SQL Server 2022 - Not Suitable driver

hatzisn

Expert
Licensed User
Longtime User
I am having problems connecting with MSSQL Server 2022.

In this page it says that from 12.2.0 it is supported:

I have Microsoft JDBC driver 12.2.0 and also the latest 12.8.1. Yet with this code it fails:

B4X:
    sConnectionString = $"jdbc:sqlserver://127.0.0.1:1433;databaseName=TrialDB;encrypt=false;trustServerCertificate=true;user=${DBUsername};password=${DBPassword}"$
    Dim conn As SQL
    conn.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver", sConnectionString)
    l = DBUtils.ExecuteMemoryTable(conn, "SELECT * FROM [dbo].[Trial1] WHERE [TrialID]=? ", Array As String(sID), 1)
    conn.Close

With this error message:

java.sql.SQLException: No suitable driver found for
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at anywheresoftware.b4j.objects.SQL.Initialize2(SQL.java:58)
at anywheresoftware.b4j.objects.SQL.Initialize(SQL.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:673)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:240)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:146)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at dhqi.octpybt.main.main(main.java:29)

I also have these additional jars:

B4X:
    #AdditionalJar: sqlite-jdbc-3.7.2
    #AdditionalJar: mssql-jdbc-12.8.1.jre11

I cannot figure out what might be wrong... Can you figure out what is wrong?
 

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
I have some small experience trying to connect SQL SERVER 2022 via ODBC and I remember that I could not connect, unless I selected true for Encryption and also true for TrustServerCertificate. May be it's worth giving it a shot here, too (although, admittedly, this is not what the error message suggests).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just tried this code:
B4X:
#AdditionalJar: C:\Users\H\Downloads\mssql-jdbc-12.8.1.jre11.jar
Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim sConnectionString As String = $"jdbc:sqlserver://127.0.0.1:1433;databaseName=TrialDB;encrypt=false;trustServerCertificate=true;user=${""};password=${""}"$
    Dim conn As SQL
    conn.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver", sConnectionString)
End Sub

With Java 14. The driver loads properly. Are you using Java 11+?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thank you all.

I figured out what was wrong.
The sConnectionString is being returned from a sub. I have put it directly in the code above.
It seems like I accidentally pressed Ctrl+X in the IDE and deleted the whole line Return sConnectionString. If you do not send a connection string it returns this totally descripting the situation error. If you try f.e. to send "" as connection string you get this error.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…