JDBC study notes - problems and solutions encountered when using JDBC MySQL (continuously updated)

2018.4.22 more:

Problem 1: The JDBC MySQL driver is not selected correctly, resulting in incorrect connection between JDBC and MySQL (I am using MySQL version 5.7)

Solution: The driver selects mysql-connector-java-8.0.11.jar to solve the problem. (The driver needs to be downloaded from the official website)

Question 2: MySQL link url format and parameter problem
solution: Use jdbc:mysql://localhost:3306/database name for the url? useSSL=false&useUnicode=true&characterEncoding=utf8 problem solved. This solution supports Chinese insertion, provided that the encoding of the database is utf8. (If you need to solve the problem of garbled characters when inserting Chinese into the database, please continue to read below)
The solution to the problem of garbled characters when inserting Chinese into MySQL: Change the default database encoding to utf8_unicode_ci in the MySQL database visualization tool (I use MySQL Workbench myself), and in the The tables created after this can be inserted into Chinese normally. Note that tables created before changing the database encoding need to manually modify the encoding. The encoding method of manually modifying the table is as follows:
alter table table name charset=utf8,collate=utf8_unicode_ci; //This command modifies the encoding method of the table. The previously defined string type column is still the original encoding method and needs to continue to be manually changed , please continue to see
the column name that needs to be changed in alter table change column name new column name type character set utf8 collate utf8_unicode_ci;


Question 3: The DriverManager running window prompts an error

 Class.forName(“com.sql. jdbc.Driver");
 Connection con=DriverManager.getConnection(url,username,password);
 Statement stmt=con.createStatement();

错误提示:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Sun Apr 22 18:41:17 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

Reason: The new version of MySQL driver class name is com.mysql.cj.jdbc.Driver, so when using com.mysql.jdbc.Driver to load the driver, it will fail to load. And the new version of MySQL driver will automatically load DriverManager without us needing to manually use Class.forName to load the driver.
 
Solution: Change the loaded driver class name to com.mysql.cj.jdbc.Driver or remove Class.forName() directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324679651&siteId=291194637