使用JDBC获取和设置数据库服务器连接变量 [英] Get and SET Database Server Connection Variable with JDBC
问题描述
当我打开到MySQL数据库的JDBC连接时,我想设置一些数据库服务器变量,例如,每次建立连接时,我想用这个值设置变量sqlmode:
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 ...
我必须在查询语句中发送此参数,还是可以直接在JDBC Connection上设置此参数? JDBC连接上是否有允许设置此变量的属性?
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 ?
使用JDBC连接设置或检索数据库服务器变量的最佳方法是什么?
What is the best way to set or retrieve database server variable with a JDBC Connection ?
推荐答案
您是否尝试将其添加到getConnection调用中?
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);
问候
这篇关于使用JDBC获取和设置数据库服务器连接变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!