jdbc连接mysql的url怎么写

发布时间: 2023-11-21 10:44 阅读: 文章来源:1MUMB104PS

很多问题都是在发现的时候才觉得重要.

autoReconnect=true&failOverReadOnly=false

问题现象:

com.MySQL.jdbc.CommunicationsException:The last packet successfully received from the server was58129 seconds ago.Thelast packet sent successfully to the server was 58129 seconds ago, which islonger than the server configured value of ‘wait_timeout‘. You should considereither expiring and/or testing connection validity before use in yourapplication, increasing the server configured values for client timeouts, orusing the Connector/J connection property ‘autoReconnect=true‘ to avoid thisproblem.

解决办法:

对应中文环境,通常mysql连接URL可以设置为:

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false

在使用数据库连接池的情况下,最好设置如下两个参数:

autoReconnect=true&failOverReadOnly=false

需要注意的是,在xml配置文件中,url中的&符号需要转义成&。比如在tomcat的server.xml中配置数据库连接池时,mysql jdbc url样例如下:

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk

&autoReconnect=true&failOverReadOnly=false

•••展开全文