I was overjoyed! jSQL lib was working perfectly with B4A. At last I could write Android apps and connect directly to the DB (my only real option).
Unfortunately, after days of writing an app in design mode with absolutely NO issues, I decided to test in Release mode.
Fails with the error
android.os.NetworkOnMainThreadException
Here is the full error message:
Oh I hope something can be done to fix this.
Thanks in advance for any help.
BTW: The ONLY way I have ever gotten this driver to connect, even using RDC is to open the current SQL server Log, Search for 'Listening on' and use the port specified there. If it is LOCALHOST then the IP address can be anything. If it is a remote server then the IP should be the computers IP. It has never worked with the default port 1433 and never worked with a UNC.
The entire code is below, but I have included a test project as well.
Unfortunately, after days of writing an app in design mode with absolutely NO issues, I decided to test in Release mode.
B4X:
sql1.Initialize("net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://192.100.1.1:99999;databaseName=MyDB;user=myuser;password=mypw")
Fails with the error
android.os.NetworkOnMainThreadException
Here is the full error message:
B4X:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at anywheresoftware.b4j.objects.SQL.Initialize2(SQL.java:56)
at anywheresoftware.b4j.objects.SQL.Initialize(SQL.java:45)
at b4a.example.main._button1_click(main.java:350)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
android.os.NetworkOnMainThreadException
Oh I hope something can be done to fix this.
Thanks in advance for any help.
BTW: The ONLY way I have ever gotten this driver to connect, even using RDC is to open the current SQL server Log, Search for 'Listening on' and use the port specified there. If it is LOCALHOST then the IP address can be anything. If it is a remote server then the IP should be the computers IP. It has never worked with the default port 1433 and never worked with a UNC.
The entire code is below, but I have included a test project as well.
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#AdditionalJar: jtds-1.3.1.jar
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#BridgeLogger: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim sql1 As SQL
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
End Sub
Sub Button1_Click
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Try
sql1.Initialize("net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://192.100.1.1:99999;databaseName=MyDB;user=myuser;password=mypw") 'sql 2014
'sql1.Initialize("net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://11.15.15.11:99999;databaseName=MyDB;user=myuser;password=mypw") 'sql 2008
'sql1.Initialize("net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://144.12.12.12:99999;databaseName=MyDB;user=myuser;password=mypw") 'sql 2012
Log("Connection Successful ")
Catch
Log("connection error ")
Log(LastException.Message)
Return
End Try
Dim C As ResultSet
C = sql1.ExecQuery("Select TOP 10 MyField FROM MyTable;")
Do While C.NextRow
Log(C.GetString("MyField"))
Loop
sql1.ExecNonQuery("Insert INTO MyTable (MyField) SELECT 'QQQQQQ'")
C = sql1.ExecQuery("Select TOP 10 MyField FROM MyTable WHERE MyField = 'QQQQQ';")
Do While C.NextRow
Log(C.GetString("MyField"))
Loop
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Attachments
Last edited: