Preparation (1): MySQL installation, configuration and basic learning
Before using JDBC to operate a database, you first need to have a database. Three links are provided here for readers to learn on their own. If you have ever used SQL language (including classroom learning in school), the first two links are enough to get started.
1. Installation and configuration: mysql installation illustration mysql graphic installation tutorial (detailed description)
2. Basic operation: 21-minute MySQL introductory tutorial
3. Simple command query: one thousand lines of MySQL study notes
It is recommended to practice while watching the introductory tutorial. While practicing basic operations such as insert, update, select, and delete, you can also build the tables to be used later.
Special note on the jdbc.url configuration: If you have upgraded mysql-connector, the characterEncoding=utf8 can be automatically recognized as utf8mb4 (of course it is also compatible with the original utf8), and I strongly recommend the autoReconnect configuration. In addition, I had ignored this attribute before. As a result, the latest DB configuration was not read due to caching, so I could not use the utf8mb4 character set. What a painful realization! !
The mysql driver is not enabled to enable batch execution of sql.
How to turn it on? When assembling the URL of the mysql link, add the allowMultiQueries parameter to it and set it to true, as follows:
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true mysql JDBC URL格式如下: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]...
Several commonly used more important parameters:
Parameter name Parameter description Default value Minimum version requirement
user database user name ( Used to connect to the database) All versions
passWord User password (used to connect to the database) All versions
useUnicode Whether to use the Unicode character set, if the parameter characterEncoding is set to gb2312 or gbk, this parameter value must be set to true false 1.1g
characterEncoding When useUnicode When set to true, specifies the character encoding. For example, it can be set to gb2312 or gbk false 1.1g
autoReconnect When the database connection is abnormal interrupted, will automatically reconnect? false 1.1
autoReconnectForPools Whether to use the reconnection strategy for the database connection pool false 3.1.3
failOverReadOnly After the automatic reconnection is successful, is the connection set to read-only? true 3.0.12
maxReconnects When autoReconnect is set to true, the number of retry connections is 3 1.1
initialTimeout When autoReconnect is set to true, the time interval between two reconnections, unit: seconds 2 1.1
connectTimeout Establish a socket connection with the database server Timeout in seconds, unit: milliseconds. 0 means never timeout, applicable to JDK 1.4 and higher versions 0 3.0.1
socketTimeout socket operation (read and write) timeout, unit: milliseconds. 0 means never timeout 0 3.0.1
Corresponds to the Chinese environment. Usually the mysql connection URL can be set to:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
When using the database connection pool, it is best to set the following two parameters:
autoReconnect=true&failOverReadOnly=false
It should be noted that , in the xml configuration file, the & symbol in the url needs to be escaped into &. For example, when configuring the database connection pool in tomcat's server.xml, the mysql jdbc url sample is as follows:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly
The above is the detailed content of MySQL installation, configuration, jdbc and basic learning. For more information, please follow other related articles on the PHP Chinese website!