Cannot read special characters with JDBC - java

I'm having problem reading UTF-8 data from MySQL database by using MySQL Connector v. 8.0.19. Scandic letters, such as "äö" are replaced with unknown characters. I already made sure the database and its tables and columns are using utf8mb4. Then I added useUnicode=true&characterEncoding=UTF-8 to JDBC connection string, but the outcome is still unexpected. I'm running MySQL CE v. 8 in a Docker container. I can see the scandic letters fine when I run the SELECT queries in a command-line.

I solved this problem by passing --default-character-set=utf8mb4 to MySQL command-line before creating the schema from a separate file. I could add this option to MySQL server configuration as a default.

Related

H2 Database Database to Lower Not Valid Connection Setting (2019)

I'm trying to connect to an embedded H2 database with the compatibility mode for PostgreSQL. I'm using the settings as described in the online documentation:
jdbc:h2:~/test.h2.db;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE
I'm getting the error:
Unsupported connection setting "DATABASE_TO_LOWER"
Is this still an experimental setting? Is there a solution and/or workaround for this error?
H2 Database v.1.4.199 + Java 11.0.2 (Oracle)
It looks like you have multiple versions of H2 in your classpath and wrong version is actually used. You can check the complete error message to be sure.
For example, version 1.4.197 throws
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Unsupported connection setting "DATABASE_TO_LOWER" [90113-197]
A 197 indicates a database version.
1.4.199 accepts your URL properly.
Also note that database path in the URL shouldn't include a file name extension. With ~/test.h2.db the file test.h2.db.mv.db will be actually used.
Please visit http://www.h2database.com/html/changelog.html
PR #1776: Improve DATABASE_TO_LOWER handling
Try to update your h2 version from 1.4.199
Warning : By default H2 transforms all names into uppercase (ex: fromIP => FROMIP). This setting will pass everything in lowercase but will not keep the case (ex: fromIP => fromip)

How MySQL connector jar version can affect query performance?

Currently, in my application with mysql-connector-java:5.1.36, everything works fine. But when I upgrade connector to mysql-connector-java:5.1.47, a query starts to take minutes-2-hours time to execute. If I run the same query directly from the terminal or from the application with v5.1.36, it takes less than a few seconds to execute.
How MySQL connector jar version can affect query performance?
I found the reason. There is a change in mysql-connector-java:5.1.47 and above:
When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4. While for mysql-connector-java:5.1.46 and below it corresponds to utf8 (or utf8mb3 more appropriately).
Link: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
For my database character encoding is set to utf8 (or utf8mb3 more appropriately). Due to this index was not being used in query. After setting server property
character_set_server : utf8
it is working fine.

How do I enforce MySQL to use utf8mb4 using JDBC connector without restarting the server?

My current configuration is as follows:
MySQL server version: 5.7.18
Operating System: Ubuntu 14.04
JDBC Connector: 5.1.42
The problem: I can't use the configuration file in my.cnf since it will require a server restart. Since I'm using MySQL version 5.7 it supports utf8mb4 and I've updated to the latest connector for the version 5.xx series.
I tried using init_connect as given in the documentation here using the following command and using mysql user without super user priviledges:
SET GLOBAL init_connect="SET NAMES 'utf8mb4';SET character_set_server='utf8mb4';";
and setting the connection url as follows:
jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&connectionCollation=utf8mb4_unicode_ci
I also tried adding session variable to the URL as follows but it doesn't work.
sessionVariables=character_set_server=utf8mb4,character_set_client=utf8mb4,character_set_connection=utf8mb4,character_set_results=utf8mb4
Can anyone help how to figure the connection settings to allow storing using utf8mb4?
P.S: Utf-8 works fine but it does not work with utf8mb4, so can't insert emoji's.

How to access a hsqldb DB with JDBC tools?

I have a hsqldb file database.
Therefore a folder contains:
hsqldb.script
hsqldb.properties
hsqldb.log
hsqldb.lck
hsqldb.tmp
When I open the script, I can see that SQL tables and INSERTs are present.
How can I use JDBC tools like squirrel to inspect the tables like a real database?
I tried connecting using jdbc:hsqldb:file:C:\path\to\my\data\hsqldb while the application is running that uses this DB.
I could connect, but all I could see are the initial INFORMATION_SCHEMA tables.
What am I missing here? Where is the data that I can see clearly in the .script, but not with an JDBC tool?
The .lck and .tmp files indicate that the database was not shutdown prior to last connection close. Use the SHUTDOWN command to close the database.

Jaybird / JDBC + National characters in path

I'm working with Firebird database in Java. Everything works fine, but I have problem with connecting to my database if database file path contains national characters, eg. "á" or "č".
Sample exception:
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544344. I/O error during "CreateFile (open)" operation for file "Z:/testing/á/sample.fdb"
Path is correct, database exists. Jaybird / JDBC has problem with "á" character in path.
Any ideas how to fix it or where is problem? Thanks for all responses.
OS: Windows 7 Pro 64 bit
JDK: 1.7.0.25
Jaybird: 2.2.3
Starting with Jaybird 3, database filename will always be sent as UTF-8, if the server is Firebird 2.5 or higher.
Jaybird 2.2 and earlier has limited support for special characters in the databasename. There are several options you can use to workaround this problem, but if they actually work depends highly on the version of Firebird and the default characterset of the OS (where Firebird is running).
Option 1: use connection property filename_charset=<name of charset> where <name of charset> is the default character set of the operating system where the Firebird server is running.
For example:
jdbc:firebirdsql://myserver/mydatabase?filename_charset=Cp1252
Option 2 (Firebird 2.5 or higher, with Jaybird 2.2): use the workaround described in JDBC-251:
Start your Java application with -Dfile.encoding=UTF8 and include utf8_filename=1 in the connection URL:
jdbc:firebirdsql://myserver/mydatabase?utf8_filename=1
When using this option, make sure that you already specify a connection characterset using connection property charSet, localEncoding or local_encoding (for Java character set names), and/or encoding or lc_ctype (for Firebird character set names). If not, you are using Firebird character set NONE which uses the JVM default character set, and you will need to set the charSet to the 'normal' default encoding of your JVM to prevent character set conversion issues because of the changed value of file.encoding (in some cases - in addition to specifying charSet - you may also explicitly need to set encoding to NONE).
Option 3: Define an alias with only ASCII characters in aliases.conf of the firebird server for the database and connect using this alias instead:
jdbc:firebirdsql://myserver/thealias
Disclosure: I am one of the Jaybird developers

Categories

Resources