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.

1589900627154.png

1589900705188.png


# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# You can copy this file to
# C:/xampp/mysql/bin/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:/xampp/mysql/data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
# password = your_password
port=3306
socket="C:/xampp/mysql/mysql.sock"


# Here follows entries for some specific programs

# The MySQL server
default-character-set=utf8
[mysqld]
port=3306
socket="C:/xampp/mysql/mysql.sock"
basedir="C:/xampp/mysql"
tmpdir="C:/xampp/tmp"
datadir="C:/xampp/mysql/data"
pid_file="mysql.pid"
# enable-named-pipe
key_buffer=16M
max_allowed_packet=1M
sort_buffer_size=512K
net_buffer_length=8K
read_buffer_size=256K
read_rnd_buffer_size=512K
myisam_sort_buffer_size=8M
log_error="mysql_error.log"

# Change here for bind listening
# bind-address="127.0.0.1"
# bind-address = ::1 # for ipv6

# Where do all the plugins live
plugin_dir="C:/xampp/mysql/lib/plugin/"

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
# commented in by lampp security
#skip-networking
#skip-federated

# Replication Master Server (default)
# binary logging is required for replication
# log-bin deactivated by default since XAMPP 1.4.11
#log-bin=mysql-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id =1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin


# Point the following paths to different dedicated disks
#tmpdir = "C:/xampp/tmp"
#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000

# Comment the following if you are using InnoDB tables
#skip-innodb
innodb_data_home_dir="C:/xampp/mysql/data"
innodb_data_file_path=ibdata1:10M:autoextend
innodb_log_group_home_dir="C:/xampp/mysql/data"
#innodb_log_arch_dir = "C:/xampp/mysql/data"
## You can set .._buffer_pool_size up to 50 - 80 %
## of RAM but beware of setting memory usage too high
innodb_buffer_pool_size=16M
## Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size=5M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=50

## UTF 8 Settings
#init-connect=\'SET NAMES utf8\'
character_set_server=utf8
collation_server=utf8_unicode_ci

#skip-character-set-client-handshake
#character_sets-dir="C:/xampp/mysql/share/charsets"
sql_mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION
log_bin_trust_function_creators=1


[mysqldump]
max_allowed_packet=16M

[mysql]
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer=20M
sort_buffer_size=20M
read_buffer=2M
write_buffer=2M

[myisamchk]
key_buffer=20M
sort_buffer_size=20M
read_buffer=2M
write_buffer=2M

[mysqlhotcopy]
 

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.

1589906612604.png
 
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
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?
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

1589908332804.png
 
Upvote 0

Peter Lewis

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?
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
Top