Mysql JDBC connection pool Chinese problem solved

zhaozj2021-02-16  116

Using MySQL as a database in the nearest project, the application server is RESIN. However, when using the connection pool using RESIN, there is no garbled after saving to the database and cannot be displayed correctly. I want to be the reason why the character set is not supported. Later, I found that if I use org.gjt.mm.mysql.driver as driver, simply add two parameters, the following is the WEB.XML Writing:

PRM

Org.gjt.mm.mysql.driver

JDBC: MySQL: // localhost: 3306 / PRM

ABCM

ABCM

However, in the documentation in RESIN, JDBC2.0 is recommended, directly using MySQL connection pool class com.mysql.jdbc.jdbc2.Optional.MysqlConnectionPoolDataSource If you use this drive above, you can't. I saw the mysql driver source code, found that this class did not set the setting of the character set, so I changed it. com.mysql.jdbc.jdbc2.optional.MysqlDataSource / ** * Creates a connection using the specified properties. * * @param props the properties to connect with * * @return a connection to the database * * @throws SQLException if an error occurs * / protected java.sql.Connection getConnection (Properties props) throws SQLException {String jdbcUrlToUse = null; if (! explicitUrl) {StringBuffer jdbcUrl = new StringBuffer ( "jdbc: mysql: //");! if (hostName = null ) {Jdbcurl.Append (Hostname); JDBCURL.APpend (":"); jdbcurl.append (port); jdbcurl.append ("/"); if (DatabaseName! = Null) {jdbcurl.append (databasename); JDBCURL.Append ("& UseUnicode = true; characterencoding = GB2312"); this line is later me. } Jdbcurltouse = jdbcurl.tostring ();} else {jdbcurltouse = this.URL;} return mysqldriver.connect (jdbcurltouse, props);} Now you can use the application connection pool Web.xml. Chinese can display correctly . But this method also has a certain problem that is not flexible enough if it is possible to put the character set settings in the parameter.

PRM

com.mysql.jdbc.jdbc2.optional.mysqlconnectionPoolDataSource

JDBC: mysql: // localhost: 3306 / prmabcm

ABCM

Who said Mysql JDBC connection pool does not support Chinese

URL

JDBC: mysql: // localhost: 3306 / Struts? Useunicode = true; characterencoding = GBK

3. Finally, close-up one article to commemorate :-) My environment: Chinese Simplified WIN2000 Pro SP3 mysql server 4.0.12 J2SDK 1.4.01 ConnetCor-J 3.0.7 Question: Insert the database with JDBC, read the database String text string. First, things in the mysql database are binary storage, support any data, of course, including Chinese. You go to the command line Insert Into TestTable Values ​​('Chinese'); select * from testtable; all display normal. However, although there is no problem in accessing Chinese, there is a problem when sorting and matching. So if you have Chinese in your database, remember to add a line in the configuration file, such as [mysqld] in WinNTmy.ini: default-character-set = GBK then restart MySQL Server. Note that GBK should be lowercase, otherwise MySQLD can't start. Second, the database is fine, look at the Java program. In the program, it is very bored to add a debugging statement: Out.println ("Chinese"); also shows normal, indicating that the entire Java environment is no problem. So, of course, in contact with Java and MySQL, MySQL JDBC Driver has a problem. Analysis, Java uses Unicode, and MySQL defaults use ISO-8XXX (forgot), and JDBC Driver transmits the query string to MySQL Server, will do Unicode-> ISO-8XXX conversions, from MySQL Server Accept results When you do ISO-8xxx-> Unicode conversion. (Unicode-> GBK is displayed on the screen, but it doesn't matter here.) This has a problem, I am inserting a database in the command line into the database is GBK (this is the default of Simplified Chinese Windows), So JDBC Driver accepts the conversion of GBK-> Unicode when accepting query results. Verify that the Chinese string S, New String (S.GetByte ("ISO-8XXX"), "GBK"), "GBK"), "GBK"), "GBK"), "GBK"), is converted into a database in the database Original appearance. We know that it is GBK, so manually comes to GBK-> Unicode, so the Java program is explicitly normally. Similarly, when writing to the database, we look forward to JDBC Driver will put unicode-> GBK, the result is unicode-> ISO-8xxx, of course, is garbled. There are a lot of articles, let's go, and tell us: To solve Chinese problems, you can handle it. This is really irresponsible. If each string must be handlered, the program is designed.

I want to think about it, I don't know if the guy who writes mySQL JDBC Driver will even do it. So I look at the connector-j-3.0.7 inside the readme, to find a solution: connection = DriverManager.getConnection ( "? Jdbc: mysql: // localhost / test user = root & password = & useUnicode = true & characterEncoding = GBK"); this It is to tell JDBC Driver to enforce the transfer code to specify parameters. If Mysql Server must use ISO-8xxx, it only uses the previous method. But I remember my mysql is GBK, I didn't change my.ini? How does JDBC Driver automatically detect the character set of mysql server? At this time, I saw the benefits of open source :-) Connector-J-3.0.7 Source code does have code read the information of MySQL Server, including character sets. From the comment, the author is converted to Unicode to the single-byte character set to write its own conversion function, and is not much less than JVM. So there is a paragraph in the code, and the database is used to call your own conversion function if you are using a single byte. But after this code, I forgot to hand over the multi-byte character set to the JVM, so it became the default ISO-8xxx conversion. My modified way: Commysqljdbcconnecter.java's 1969 line of this file, this.dounicode = true; // forward The Issue will move all four lines to 1964, put the front of the line below: Try {Re-use this code The JDBC Driver, you can read and write Chinese correctly if you have any modifications. It is normal, and it is not dead, and it is abnormal. Oh, I feel very good.

转载请注明原文地址:https://www.9cbs.com/read-10450.html

New Post(0)