Database-------Mysql (JDBC Implementation & solve storage garbled problem)

Source: Internet
Author: User
Tags connection pooling


Database-------Mysql (JDBC Implementation & solve storage garbled problem)



1, the problem of garbled solution is very simple!

First, when you set up the database to specify the character set as Utf-8, and then JDBC programming, the following code after the URL with the parameter characterencoding! More settings for URL parameters can be found on the MySQL official website documentation:

Http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html


2.MySQL JDBC URL format for connector/j The following example:

Jdbc:mysql://[host][,failoverhost ...] [:p Ort]/[database]
[? PROPERTYNAME1] [=propertyvalue1] [&propertyname2] [=propertyvalue2] ...

Jdbc:mysql://[host:port],[host:port].../[database]
[? PROPERTYNAME1] [=propertyvalue1] [&propertyname2] [=propertyvalue2] ...

Instance:
Jdbc:mysql://localhost:3306/sakila?profilesql=true

Package JAVA_DATA_JDBC;



Only a few important parameters are listed, as shown in the following table:

Parameter name Parameter description Default value Minimum version requirements
User Database user name (used to connect to database)
All versions
Password User password (used to connect to database)
All versions
Useunicode If the Unicode character set is used, the value of this parameter must be set to True if the parameter characterencoding is set to gb2312 or GBK False 1.1g
Characterencoding When Useunicode is set to True, the character encoding is specified. For example, can be set to gb2312 or GBK False 1.1g
AutoReConnect Is the connection automatically reconnected when the database connection is interrupted abnormally? False 1.1
Autoreconnectforpools Whether to use a reconnection policy for database connection pooling False 3.1.3
Failoverreadonly Is the connection set to read-only after the automatic reconnection is successful? True 3.0.12
Maxreconnects When AutoReConnect is set to true, the number of times to retry the connection 3 1.1
Initialtimeout When AutoReConnect is set to true, the time interval between two re-interconnects, in seconds 2 1.1
ConnectTimeout Time-out, in milliseconds, when establishing a socket connection with the database server. 0 means never timeout, for JDK 1.4 and later 0 3.0.1
Sockettimeout Socket operation (Read-write) timeout, in milliseconds. 0 means never time out 0 3.0.1

For the Chinese environment, the MySQL connection URL can usually be set to:
jdbc:mysql://localhost:3306/test?user=root&password=&useunicode=true&characterencoding=gbk& Autoreconnect=true&failoverreadonly=false

In the case of using a database connection pool, it is best to set the following two parameters:
Autoreconnect=true&failoverreadonly=false

Note that in the XML configuration file, the & symbol in the URL needs to be escaped to &. For example, when configuring a 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 =gbk
&autoreconnect=true&failoverreadonly=false



3, the code is as follows:

import java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import  Java.sql.statement;public class jdbc_01 {public static void main (String[]  args)  {String userName =  "root"; string password =  "root";/* *  the format of this URL can take a lot of parameters, please refer to the MySQL website for details http://dev.mysql.com/ doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html */string url =  "Jdbc:mysql://localhost:3306?characterencoding=utf-8&usessl=true"; string sql1 =  "Select * from class"; string sql =  "insert into class  (name,age)  VALUES  (' Hello ', ') '; try  {/* *  This driver is written in two ways: the Inheritance and the Org.gjt.mm.mysql.driver */class.forname (" Com.mysql.jdbc.Driver "); Connection conn = drivermanager.getconnection (Url, username, password); Statement stmt = conn.createstatement (); STMT.execute ("use student;"); Stmt.execute (SQL); Resultset res = stmt.executequery (SQL1); while (Res.next ()) {string id =  Res.getstring ("id"); String name = res.getstring ("name"); Int age = res.getint ("Age"); System.out.println ("Ordinal: " +id +  "  "  + "name: " + name +  "    "+ age+" years old! ");}}  catch  (exception e)  {e.printstacktrace ();}}

Part of Source: http://elf8848.iteye.com/blog/1684414

This article is from the "11941149" blog, please be sure to keep this source http://11951149.blog.51cto.com/11941149/1850911

Database-------Mysql (JDBC Implementation & solve storage garbled problem)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.