Android Question (ask) jdbc url dynamic database and implementation

hugoyaw

Member
hi people, please help me a link / a good way so I don't have to write the database name in

B4X:
JdbcUrl = jdbc: mysql: // localhost / test? CharacterEncoding = utf8

so that I can set which database that I will select via the client side.

this is an example that more or less I mean

B4X:
sql.operator_count = SELECT COUNT (file_operator.RECNO) 'COUNT' FROM? .file_operator WHERE file_operator.ID =? AND file_operator.PASSWORD_FINAL =? .System_string_encrypt (?)

I have tried but the cmd parameter thinks that the database name I entered has quotes so the query is wrong. Thx before
 

sfsameer

Well-Known Member
Licensed User
Longtime User
hi people, please help me a link / a good way so I don't have to write the database name in

B4X:
JdbcUrl = jdbc: mysql: // localhost / test? CharacterEncoding = utf8

so that I can set which database that I will select via the client side.

this is an example that more or less I mean

B4X:
sql.operator_count = SELECT COUNT (file_operator.RECNO) 'COUNT' FROM? .file_operator WHERE file_operator.ID =? AND file_operator.PASSWORD_FINAL =? .System_string_encrypt (?)

I have tried but the cmd parameter thinks that the database name I entered has quotes so the query is wrong. Thx before
i have used a method before for a cloud central database
here is what i did :
1- Created a back-end admin page to create users
1-1 each user is created, a database with that user will be created ex : i create a user called Erel then a database will be created named Erel

2- in the B4J login page :
i used the code to connect to a database and that database name is the user name that i have entered earlier

this solution is working with 45+ customers using our cloud point of sales app.

hope that gives you an idea :)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I use the "replace" trick to replace the database name in JdbcUrl.

B4X:
Public Sub Init(DBUser As String, DBPassword As String, DBName As String) As Boolean
    Dim Success As Boolean
    Try
        DriverClass = Main.settings.Get("DriverClass")
        JdbcUrl = Main.settings.Get("JdbcUrl")
        If DBName <> "" Then
            JdbcUrl = JdbcUrl.ToLowerCase.Replace("test", DBName)
        End If       
        pool.Initialize(DriverClass, JdbcUrl, DBUser, DBPassword)
        con = pool.GetConnection
        If con.IsInitialized Then
            Success = True
        Else
            Success = False
        End If       
    Catch
        LogError(LastException)
        Exceptions = LastException.Message
        Success = False
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    If pool.IsInitialized Then pool.ClosePool
    Return Success
End Sub

 
Upvote 0

hugoyaw

Member
The table name cannot be parameterized. It has nothing to do with quotes.

You will need to make multiple commands, one for each table.

thx Mr. Erel i hope i found other way

i have used a method before for a cloud central database
here is what i did :
1- Created a back-end admin page to create users
1-1 each user is created, a database with that user will be created ex : i create a user called Erel then a database will be created named Erel

2- in the B4J login page :
i used the code to connect to a database and that database name is the user name that i have entered earlier

this solution is working with 45+ customers using our cloud point of sales app.

hope that gives you an idea :)

that exactly what i want to do mr. sfsameer
I feel more confident that my step was not wrong

I use the "replace" trick to replace the database name in JdbcUrl.

B4X:
Public Sub Init(DBUser As String, DBPassword As String, DBName As String) As Boolean
    Dim Success As Boolean
    Try
        DriverClass = Main.settings.Get("DriverClass")
        JdbcUrl = Main.settings.Get("JdbcUrl")
        If DBName <> "" Then
            JdbcUrl = JdbcUrl.ToLowerCase.Replace("test", DBName)
        End If    
        pool.Initialize(DriverClass, JdbcUrl, DBUser, DBPassword)
        con = pool.GetConnection
        If con.IsInitialized Then
            Success = True
        Else
            Success = False
        End If    
    Catch
        LogError(LastException)
        Exceptions = LastException.Message
        Success = False
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    If pool.IsInitialized Then pool.ClosePool
    Return Success
End Sub

if i use jRDC can i use this method too mr. aeric?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
if i use jRDC can i use this method too mr. aeric?
I believe you can but remember to close the previous connections when you no longer need them.
B4X:
If con <> Null And con.IsInitialized Then con.Close
If pool.IsInitialized Then pool.ClosePool
Otherwise, you will reach max connection if you didn't set the max connection explicitly or leave it as default.
 
Upvote 0

hugoyaw

Member
I believe you can but remember to close the previous connections when you no longer need them.
B4X:
If con <> Null And con.IsInitialized Then con.Close
If pool.IsInitialized Then pool.ClosePool
Otherwise, you will reach max connection if you didn't set the max connection explicitly or leave it as default.
thxx mr. Aeric
 
Upvote 0
Top