Android Question (SQLException) java.sql.SQLException: Invalid object name 'usersbc'.

Toni1982

Member
Hello Mr Erel

I have an error problem.

I had using Webhosting for MSSQL server more than 3 year and my app can run well to access the database table. But in march the webhosting migration My server to new server and change ip address

I had change my ip Address ini my app, and I can connect ms database using navicat

but when I run my app

there's an error

(SQLException) java.sql.SQLException: Invalid object name 'usersbc'.

this in the error

Can You help me Mr Erel?

Thank You
 

Attachments

  • Screenshot 2025-03-04 153953.png
    Screenshot 2025-03-04 153953.png
    84.6 KB · Views: 43
  • Screenshot 2025-03-04 154017.png
    Screenshot 2025-03-04 154017.png
    150.4 KB · Views: 48

josejad

Expert
Licensed User
Longtime User
Please, read about posting questions in the forum


In short:
- Don't ask Erel directly, this is an open forum
- Don't post code or errors as images, but as text using [code] ... .your code here... [/code]
- Try to upload a small example showing the problem, or, at least, post the relevant code

Take a look to "Code Smells". Tip 5, parameterized queries.

(SQLException) java.sql.SQLException: Invalid object name 'usersbc'.
The only thing I can see, there's some error in your SQL. Log your sQuery
B4X:
Log(SQuery)
and try to execute it in Navicat
 
Upvote 0

Toni1982

Member
I'm Sorry Mr Josejad

I Don't know the rule.

I try to execute log(Squery) in Navicat and the result is

Msg 102, Level 15, State 1, Server WIN-A2R3C6HPSN2, Procedure , Line 0
Incorrect syntax near 'Squery'.
> [42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near 'Squery'. (102)

> Time: 0.045s
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Incorrect syntax near 'Squery'.
Hiding your code and query used is the best way not to get help.

I can´t see how anyone can help with this less info you are providing.

Away from that you already got the best suggestion in #2
- Try to upload a small example showing the problem, or, at least, post the relevant code
- Take a look to "Code Smells". Tip 5, parameterized queries.
 
Upvote 0

Toni1982

Member
Hello Mr DonManfred

I got this error when Webhosting migration the server to new one, before migration there is no error to connect the database an table
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I got this error when Webhosting migration the server to new one, before migration there is no error to connect the database an table
Again. We do not know anything about how you are connecting to the server. If the connection is successfully or not.

If there is an problem with your ISP the best would be to contact their support.
If they say there is no pissibility to connect to the Database from "outside" any longer then you´re unlucky.

We can not give any congrete advice not knowing the details.

But, at least, we are trying to help you! Don´t give up.

PD: Using MSSQL may be a problem. I don´t think it is really suiteable for connections made from a B4A app.

Probably it is better to setup a jRDC2-Server (with B4J) on Windows and connect to MSSQL only from jRDC2.

You need to find out from your ISP if it still is possible to connect from outside.
 
Upvote 0

Toni1982

Member
I Am sorry Mr DonManfred

this is my code to connect the database

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'Subs in this code module will be accessible from all modules.
Dim Sql1 As JdbcSQL
Dim sIP As String ="//202.67.10.162"
Dim sPort As String =":25000"
Dim SDatabase As String = "DB9766_datamaster"
Dim sUser As String = "DB9766_sunshineedu"
Dim sPassword As String = "********"
Dim Driver As String = "net.sourceforge.jtds.jdbc.Driver"
Dim SqlServer As String = "jdbc:jtds:sqlserver:"
End Sub

Public Sub Koneksi()
Try
Sql1.InitializeAsync("Sql1", Driver,SqlServer & sIP & sPort & ";Databasename="& SDatabase & ";User=" & sUser & ";password=" & sPassword & ";appname=SKMJL;wsid=TEST;loginTimeout=10;sockettimeout=10","","")
ProgressDialogShow("Trying to Connect")
Catch
Log("Connection error")
Log(LastException.message)
Return
End Try
End Sub

And this is Code to Table

#Region Project Attributes
#ApplicationLabel: Beads Card
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: landscape
#CanInstallToExternalStorage: False
#BridgeLogger : true
#Additionaljar: jtds-1.3.1.jar
#End Region

#Region Activity Attributes
#FullScreen: True
#IncludeTitle: false
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'Private xui As XUI
Public Registration As String
Public dataIP As String
Public nama, phoneid As String
Dim pID As Phone
Dim imei As String
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Button1 As Button
Private EditText1 As EditText
Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
'imei = pID.GetDeviceId
'Activity.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "Backmain.jpg", 1920, 1080))
If FirstTime = True Then
modkoneksi.Koneksi
DisableStrictMode

End If

End Sub


public Sub sql1_ready(success As Boolean)
Try
ProgressDialogHide
Log(success)
If success = False Then
Log("connection Failed")
MsgboxAsync("connection Failed" & CRLF & LastException,"")
Log(LastException)
Return
End If

Catch
Log(LastException)
End Try
End Sub


Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub InjectionSafe(input As String) As Boolean
If _
input.Contains(" ") _
Or input.Contains(";") _
Or input.Contains("'") _
Or input.Contains(Chr(0x0022)) _
Or input.Contains(Chr(0x0027)) _
Or input.Contains(Chr(0x00AB)) _
Or input.Contains(Chr(0x00BB)) _
Or input.Contains(Chr(0x2018)) _
Or input.Contains(Chr(0x2019)) _
Or input.Contains(Chr(0x201A)) _
Or input.Contains(Chr(0x201B)) _
Or input.Contains(Chr(0x201C)) _
Or input.Contains(Chr(0x201D)) _
Or input.Contains(Chr(0x201E)) _
Or input.Contains(Chr(0x201F)) _
Or input.Contains(Chr(0x2039)) _
Or input.Contains(Chr(0x203A)) _
Or input.Contains(Chr(0x300C)) _
Or input.Contains(Chr(0x300D)) _
Or input.Contains(Chr(0x300E)) _
Or input.Contains(Chr(0x300F)) _
Or input.Contains(Chr(0x301D)) _
Or input.Contains(Chr(0x301E)) _
Or input.Contains(Chr(0x301F)) _
Or input.Contains(Chr(0xFE41)) _
Or input.Contains(Chr(0xFE42)) _
Or input.Contains(Chr(0xFE43)) _
Or input.Contains(Chr(0xFE44)) _
Or input.Contains(Chr(0xFF02)) _
Or input.Contains(Chr(0xFF07)) _
Or input.Contains(Chr(0xFF62)) _
Or input.Contains(Chr(0xFF63)) _
Then
Return False
Else
Return True
End If
End Sub

Private Sub Button1_Click
Try

Dim i As Int
Registration = EditText1.text
InjectionSafe(EditText1.Text)
Dim sQuery As String = "SELECT COUNT(*) FROM usersbc WHERE username ='" & EditText1.Text & "' AND akses = 'True'"

i = modkoneksi.Sql1.ExecQuerySingleResult(sQuery)

If i <> 0 Then
tampildata

' StartActivity(Home)

Else
MsgboxAsync("Login Gagal, Hubungi Developer untuk ijin penggunaan","Pemberitahuan")
Return
End If

Catch
Log(LastException)
End Try
End Sub

Public Sub tampildata
Dim cursor As JdbcResultSet
imei = pID.getSettings("android_id")
cursor = modkoneksi.Sql1.ExecQuery2("SELECT * FROM usersbc WHERE username = ?",Array(EditText1.Text))
Do While cursor.NextRow
nama = cursor.GetString("name")
phoneid = cursor.GetString("phoneid")

Loop
If phoneid = Null Then
modkoneksi.Sql1.ExecNonQuery("UPDATE usersbc SET phoneid = '"& imei & "' WHERE username = '" & EditText1.Text & "'")
else if phoneid = imei Then
StartActivity(Home)
Else
MsgboxAsync("Login Gagal, Hubungi Developer untuk ijin penggunaan","Pemberitahuan")
Return
End If
End Sub


Private Sub Button2_Click
imei = pID.getSettings("android_id")
'Msgbox(imei,"info")
tampildata
If phoneid = Null Then
modkoneksi.Sql1.ExecNonQuery("UPDATE usersbc SET phoneid = '" & imei & "' WHERE username = '" & EditText1.Text & "'")
End If
End Sub
 
Upvote 0

Toni1982

Member
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Subs in this code module will be accessible from all modules.
    Dim Sql1 As JdbcSQL
    Dim sIP As String ="//202.67.10.162"
    Dim sPort As String =":25000"
    Dim SDatabase As String = "DB9766_datamaster"
    Dim sUser As String = "DB9766_sunshineedu"
    Dim sPassword As String = "*******"
    Dim Driver As String = "net.sourceforge.jtds.jdbc.Driver"
    Dim SqlServer As String = "jdbc:jtds:sqlserver:"
End Sub

Public Sub Koneksi()
    Try
        Sql1.InitializeAsync("Sql1", Driver,SqlServer & sIP & sPort & ";Databasename="& SDatabase & ";User=" & sUser & ";password=" & sPassword & ";appname=SKMJL;wsid=TEST;loginTimeout=10;sockettimeout=10","","")
        ProgressDialogShow("Trying to Connect")
    Catch
        Log("Connection error")
        Log(LastException.message)
        Return
    End Try
End Sub
 
Upvote 0

Toni1982

Member
B4X:
#Region  Project Attributes
    #ApplicationLabel: Beads Card
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
    #BridgeLogger : true
    #Additionaljar: jtds-1.3.1.jar
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Private xui As XUI
    Public Registration As String
    Public dataIP As String
    Public nama, phoneid As String
    Dim pID As Phone
    Dim imei As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1 As Button
    Private EditText1 As EditText
    Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    'imei = pID.GetDeviceId
    'Activity.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "Backmain.jpg", 1920, 1080))
    If FirstTime = True Then
        modkoneksi.Koneksi
        DisableStrictMode
        
    End If
    
End Sub


public Sub sql1_ready(success As Boolean)
    Try
        ProgressDialogHide
        Log(success)
        If success = False Then
            Log("connection Failed")
            MsgboxAsync("connection Failed" & CRLF & LastException,"")
            Log(LastException)
            Return
        End If

    Catch
        Log(LastException)
    End Try
End Sub


Sub DisableStrictMode
    Dim jo As JavaObject
    jo.InitializeStatic("android.os.Build.VERSION")
    If jo.GetField("SDK_INT") > 9 Then
        Dim policy As JavaObject
        policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
        policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
        Dim sm As JavaObject
        sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub InjectionSafe(input As String) As Boolean
    If _
           input.Contains(" ") _
        Or input.Contains(";") _
        Or input.Contains("'") _
        Or input.Contains(Chr(0x0022)) _
        Or input.Contains(Chr(0x0027)) _
        Or input.Contains(Chr(0x00AB)) _
        Or input.Contains(Chr(0x00BB)) _
        Or input.Contains(Chr(0x2018)) _
        Or input.Contains(Chr(0x2019)) _
        Or input.Contains(Chr(0x201A)) _
        Or input.Contains(Chr(0x201B)) _
        Or input.Contains(Chr(0x201C)) _
        Or input.Contains(Chr(0x201D)) _
        Or input.Contains(Chr(0x201E)) _
        Or input.Contains(Chr(0x201F)) _
        Or input.Contains(Chr(0x2039)) _
        Or input.Contains(Chr(0x203A)) _
        Or input.Contains(Chr(0x300C)) _
        Or input.Contains(Chr(0x300D)) _
        Or input.Contains(Chr(0x300E)) _
        Or input.Contains(Chr(0x300F)) _
        Or input.Contains(Chr(0x301D)) _
        Or input.Contains(Chr(0x301E)) _
        Or input.Contains(Chr(0x301F)) _
        Or input.Contains(Chr(0xFE41)) _
        Or input.Contains(Chr(0xFE42)) _
        Or input.Contains(Chr(0xFE43)) _
        Or input.Contains(Chr(0xFE44)) _
        Or input.Contains(Chr(0xFF02)) _
        Or input.Contains(Chr(0xFF07)) _
        Or input.Contains(Chr(0xFF62)) _
        Or input.Contains(Chr(0xFF63)) _
    Then
        Return False
    Else
        Return True
    End If
End Sub

Private Sub Button1_Click
    Try
        
        Dim i As Int
        Registration = EditText1.text
        InjectionSafe(EditText1.Text)
        Dim sQuery As String = "SELECT COUNT(*) FROM usersbc WHERE username ='" & EditText1.Text & "' AND akses = 'True'"
    
        i = modkoneksi.Sql1.ExecQuerySingleResult(sQuery)

        If i <> 0 Then
            tampildata
            
    '        StartActivity(Home)
                    
        Else
            MsgboxAsync("Login Gagal, Hubungi Developer untuk ijin penggunaan","Pemberitahuan")
            Return
        End If
    
    Catch
        Log(LastException)
    End Try
End Sub

Public Sub tampildata
    Dim cursor As JdbcResultSet
    imei = pID.getSettings("android_id")
    cursor = modkoneksi.Sql1.ExecQuery2("SELECT * FROM usersbc WHERE username = ?",Array(EditText1.Text))
    Do While cursor.NextRow
        nama = cursor.GetString("name")
        phoneid = cursor.GetString("phoneid")
        
    Loop
    If phoneid = Null Then
        modkoneksi.Sql1.ExecNonQuery("UPDATE usersbc SET phoneid = '"& imei & "' WHERE username = '" & EditText1.Text & "'")
    else if phoneid = imei Then
        StartActivity(Home)
    Else
        MsgboxAsync("Login Gagal, Hubungi Developer untuk ijin penggunaan","Pemberitahuan")
        Return
    End If
End Sub


Private Sub Button2_Click
    imei = pID.getSettings("android_id")
    'Msgbox(imei,"info")
    tampildata
    If phoneid = Null Then
        modkoneksi.Sql1.ExecNonQuery("UPDATE usersbc SET phoneid = '" & imei & "' WHERE username = '" & EditText1.Text & "'")
    End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sql1.InitializeAsync("Sql1", Driver,SqlServer & sIP & sPort & ";Databasename="& SDatabase & ";User=" & sUser & ";password=" & sPassword & ";appname=SKMJL;wsid=TEST;loginTimeout=10;sockettimeout=10","","")
You are not checking if the connection is successfully!?

B4X:
Sql1.InitializeAsync("Sql1", Driver,SqlServer & sIP & sPort & ";Databasename="& SDatabase & ";User=" & sUser & ";password=" & sPassword & ";appname=SKMJL;wsid=TEST;loginTimeout=10;sockettimeout=10","","")
Wait For Sql1_Ready (Success As Boolean)
If Success Then
    log("Databaseconnection successfully")
else
    log("Problem connecting to the Database")
end if
 
Upvote 0

Toni1982

Member
I check conection from this code:
public Sub sql1_ready(success As Boolean)
    Try
        ProgressDialogHide
        Log(success)
        If success = False Then
            Log("connection Failed")
            MsgboxAsync("connection Failed" & CRLF & LastException,"")
            Log(LastException)
            Return
        End If

    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
I'm Sorry Mr Josejad
No need to apologize

I try to execute log(Squery) in Navicat and the result is
You must execute it in B4A, in the line 134, and check the logs screen in the B4A IDE, then get the result and paste in navicat, but, as DonManfred says, probably the problem is that your not getting a successful connection to the DB

I got this error when Webhosting migration the server to new one
Maybe they don't have the 25000 port open, or they have not given permission to connect externally
 
Upvote 0

Toni1982

Member
Hello Mr josejad

B4X:
Private Sub Button1_Click
    Try
        
        Dim i As Int
        Registration = EditText1.text
        InjectionSafe(EditText1.Text)
        Dim sQuery As String = "SELECT COUNT(*) FROM usersbc WHERE username ='" & EditText1.Text & "' AND akses = 'True'"
        Log(sQuery)
        i = modkoneksi.Sql1.ExecQuerySingleResult(sQuery)

        If i <> 0 Then
            tampildata
            
    '        StartActivity(Home)
                    
        Else
            MsgboxAsync("Login Gagal, Hubungi Developer untuk ijin penggunaan","Pemberitahuan")
            Return
        End If
    
    Catch
        Log(LastException)
    End Try
End Sub

result at log is

SELECT COUNT(*) FROM usersbc WHERE username ='adm001' AND akses = 'True'
(SQLException) java.sql.SQLException: Invalid object name 'usersbc'.

then I copy to navicat query and the result is

SELECT COUNT(*) FROM usersbc WHERE username ='adm001' AND akses = 'True'
> Msg 208, Level 16, State 1, Server WIN-A2R3C6HPSN2, Procedure , Line 0
Invalid object name 'usersbc'.
> [42S02] [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'usersbc'. (208)

> Time: 0.032s
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
So there's some error in your sql, make it work on navicat, and then fix it in B4A.

But, please, don't ignore the other questions pointed by DonManfred. Have you a successful connection with the code posted in #11?

About the error, check in SQL server forums, I don't know what can be that error.
Or try with chatGPT

 
Upvote 0

Toni1982

Member
When I try this code in navicat query

B4X:
SELECT * FROM [DB9766_sunshineedu].[usersbc] WHERE username = 'adm001' AND akses = 'True'

it show the result

but when I Copied to B4A the error is

SELECT * FROM [DB9766_sunshineedu].[usersbc] WHERE username ='adm001' AND akses = 'True'
(NumberFormatException) java.lang.NumberFormatException: For input string: "adm001"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
modkoneksi - 18: Static code modules cannot handle events.
You can not use the code in a CODE-Module! Codemodules can not handle any events.

Use an Class, an Service or an Activity. A B4XPage is also ok (under the hood is is an class)

That said you should switch to B4XPages if you are still using Activities.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
java.lang.NumberFormatException: For input string:
As said above, try:

B4X:
SQL.ExecNonQuery2("SELECT * FROM [DB9766_sunshineedu].[usersbc] WHERE username = ? AND akses = ?", Array(EditText1.Text, True))
 
Upvote 0
Top