B4J Question Webserver tutorial with SQL not working for me. Collation issue

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All

I have been trying to run the Server example with SQL which was posted as the Web Server tutorial

The system creates the database tables and everything looks good.

When I try and go to test the login example I get this error
java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'utf8mb4'

I have included screen shots of the Database, tables and a quote of the My.ini config file, I cannot see what i am missing and it has been 4 hours now trying all different options and still the same errors. I am refreshing the browser each time CTRL-F5 (Just in case) which should not be the problem. I am out of options.




 

Xandoca

Active Member
Licensed User
Longtime User
Do you need to create/config your MySQL as utf8_unicode_ci (collation_server=utf8_unicode_ci)?
Mine is configured as utf8mb4 and utf8mb4_0900_ai_ci and is working fine.

 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Do you need to create/config your MySQL as utf8_unicode_ci (collation_server=utf8_unicode_ci)?
Mine is configured as utf8mb4 and utf8mb4_0900_ai_ci and is working fine.

View attachment 94366
If you look at the screen shots and also the My SQL config file, that is exactly what I have done. Initially it was utf8mb4 and I changed it because of the error. Unless there is somewhere I do not know where to change it that is not in any of the config areas and also that MySQL does not display. I have spent hours searching for this lost config param. So i do not think it exists
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
I've looked at my file "my.ini" and there's nothing set about utf. My tables was created like:
SQL:
CREATE TABLE `user` (
  `iduser` int NOT NULL AUTO_INCREMENT,
  ...
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

How about your tables?
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
My tables were created by the example program automatically. So maybe they made a mistake in their code, I doubt it.

here is the part of the example code that makes the tables

B4X:
Public Sub CreateUserTableIfNeeded
    Dim sql1 As SQL = pool.GetConnection
    If sql1.ExecQuerySingleResult("SELECT count(*) FROM information_schema.tables WHERE table_name = 'b4j_users'") = 0 Then
        Dim ct As String = "CREATE TABLE `b4j_users` (`name` VARCHAR(50) PRIMARY KEY, " _
             & " `hash` BLOB, `salt` BLOB)"
        sql1.ExecNonQuery(ct)
    End If
    sql1.Close
End Sub

and the table looking at it looks correct

 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
So you must have changed the example to match your MySQL or you would have had the same problem

this is the original code from the example posted by Erel
B4X:
Public Sub CheckUserExist (name As String) As Boolean
    Dim sq As SQL = pool.GetConnection
    Dim count As Int = sq.ExecQuerySingleResult2("SELECT count(name) FROM b4j_users WHERE name = ? COLLATE utf8_unicode_ci", _
        Array As String(name))
    sq.Close
    Return count > 0
End Sub

Public Sub CheckCredentials(User As String, Password As String) As Boolean
    Dim sq As SQL = pool.GetConnection
    Dim rs As ResultSet = sq.ExecQuery2("SELECT hash, salt FROM b4j_users WHERE name = ? COLLATE utf8_unicode_ci", _
        Array As Object(User))
    Dim res As Boolean = False
    If rs.NextRow Then
        Dim hash() As Byte = CalcHash(Password, rs.GetBlob("salt"))
        Dim storedHash() As Byte = rs.GetBlob("hash")
        If hash.Length = storedHash.Length Then
            res = True
            For i = 0 To hash.Length - 1
                If hash(i) <> storedHash(i) Then
                    res = False
                    Exit
                End If
            Next
        End If
    End If
    rs.Close
    sq.Close
    Return res
End Sub
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
how about change the COLLATE utf8_unicode_ci to COLLATE utf8mb4_0900_ai_ci and also remove the changes you've made at My.ini?
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
That might work but again I am then changing code of the example that seems to have worked for everyone else. So I want to know why is it not working for me. We do not learn anything if we keep doing work-arounds and never get to the source of the problem.

It is possible that Erel made a mistake (I doubt it) but we are all human. Bearing in mind this example is not new, maybe something changed in either MySQL or in B4j that no one updated the source. But then it is important to keep examples updated or we just waste hours with no possibility of a result unless we do a workaround.
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
You're right. I think that the point/problem is how your MySQL was installed/configured (character set and collation used to install).
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…