3

When i open a JDBC connection to a MySQL database i want to set some database server variable, so for example every time that a connection is established i want to set the variable sqlmode with this value :

SET sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT';

for enabling pipe string concatenation ...

I have to send this parameter in a query statement or can i set this parameter directly on the JDBC Connection ? Is there a property on the JDBC connection that allow set this variable ?

What is the best way to set or retrieve database server variable with a JDBC Connection ?

1 Answer 1

10

Have you tried adding it into the getConnection call?

String url = "jdbc:mysql://localhost/test?user=sqluser&password=sqluserpw&sessionVariables=sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT'";
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(url);

Regards

Sign up to request clarification or add additional context in comments.

2 Comments

To URL i want to add below SQL mode. Could you please help me how my url will look like. SET @@SQL_MODE = CONCAT(@@SQL_MODE, ',PAD_CHAR_TO_FULL_LENGTH');
I had to use the %3D in the value of the sessionVariables to make it work. But this really helped me, thanks @Gigab0rt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.