通过jdbc 连接mysql 数据库的实例,以及中文乱码的解决方法: import java.sql.*;/** * 使用JDBC连接数据库MySQL的过程: * DataBase: db_test * tables: tab_test; * username: user_test; * passwd: passwd_test; * * @author zhongbo.wzb@alibaba-inc.com
通过jdbc 连接mysql 数据库的实例,以及中文乱码的解决方法:
import java.sql.*; /** * 使用JDBC连接数据库MySQL的过程: * DataBase: db_test * tables: tab_test; * username: user_test; * passwd: passwd_test; * * @author zhongbo.wzb@alibaba-inc.com */ public class DBTest { public static Connection getConnection() throws SQLException, ClassNotFoundException { // 第一步:加载MySQL的JDBC的驱动 Class.forName("com.mysql.jdbc.Driver"); //取得连接的url,能访问MySQL数据库的用户名:user_test;密码:passwd_test, 数据库名: db_test String url = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf-8"; String username = "user_test"; String password = "passwd_test"; // 第二步:创建与MySQL数据库的连接类的实例 Connection conn = DriverManager.getConnection(url, username, password); return conn; } public static void main(String args[]) { System.out.println("args num: " + args.length); for (int i = 0; i <p><span>不</span><span><span>过,官方文档还说,"要想覆盖客户端上的自动检测编码功能,可在用于连接到服务器的URL中使用“characterEncoding”属性。" </span></span><br> </p> <p><span><span><br> </span></span></p> <p><span><span><span>解决方法二: </span><br> <br> <span>连接mysql时(无论在从mysql读还是取数据的情况),指定使用的编码方式为utf-8,具体代码如下 </span><br> <br> <span>//装载mysql-jdbc驱动 </span><br> <br> <span>Class.forName("com.mysql.jdbc.Driver").newInstance(); </span><br> <br> <span>//连接数据库 </span><br> <br> <span>Connection sqlCon = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test? user=root&password=1&useUnicode=true&characterEncoding=utf-8" ); </span><br> </span></span></p> <br> <p><br> </p> <p><br> </p> <p><br> </p> <p><br> </p> <p><br> </p> <p><span>charset 和 collation 有多个级别的设置:服务器级、数据库级、表级、列级和连接级</span><br> <br> <span>1.服务器级 </span><br> <span> 查看设置:show global variables like 'character_set_server'; 和 show global variables like 'collation_server';</span><br> <span> 修改设置:在OPTION FILE (/etc/mysql/my.cnf)里设置: </span><br> <span> [mysqld] </span><br> <span> character_set_server=utf8 </span><br> <span> collation_server=utf8_general_ci </span><br> <br> <span>2. 数据库级 </span><br> <span> 查看设置:select * from information_schema.schemata where schema_name = 'cookbook';</span><br> <span> 设置: </span><br> <span> 1.若没有显式设置,则自动使用服务器级的配置 </span><br> <span> 2.显式设置:在创建库时指定 </span><br> <span> create database playUtf8 DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;</span><br> <br> <span>3.表级 </span><br> <span> 查看设置:show create table course; </span><br> <span> 设置: </span><br> <span> 1.若没有显式设置,则自动使用数据库级的配置 </span><br> <span> 2.显式设置:在创建表时指定 </span><br> <span> create table utf ( id int ) default charset=utf8 default collate=utf8_bin;</span><br> <br> <span>4.列级 </span><br> <span> 查看设置:show create table course; </span><br> <span> 设置: </span><br> <span> 1.若没有显式设置,则自动使用表级的配置 </span><br> <span> 2.显式设置: </span><br> <br> <span> CREATE TABLE Table1(column1 VARCHAR(5) CHARACTER SET latin1 COLLATE latin1_german1_ci);</span><br> <br> <span>5.连接级别 </span><br> <span> 查看设置: </span><br> <span> show variables like 'character_set_client'; # 服务端使用这个编码来理解客户端发来的statements </span><br> <span> show variables like 'character_set_connection' ; # 我还不知道什么意思,等看了mysql源码再说 </span><br> <span> show variables like 'character_set_results'; # 服务端使用这个编码回送结果集和错误信息 </span><br> <span> 设置: </span><br> <span> 客户端在连接时可以指定这些参数;同时,服务端也提供了一个Global范围的值,客户端未指定这些参数时,服务端就使用这个Global值。这个global值怎么设置的? 我查遍了很多文档,似乎还没看到设置的办法 (有人说通过my.cnf,或者在启动mysqld时指定命令行参数,其实都是错的)</span><br> <br> <span> </span><br> <br> <span>附:connector/j传输SQL时用什么编码? </span><br> <span> 答案: "The character encoding between client and server is automatically detected upon connection. The encoding used by the driver is specified on the server using the character_set_server system variable for server versions 4.1.0 and newer."</span><br> <span> 也就是说,是在连接时查询服务器端的character_set_server值,再确定连接将使用的编码。 </span><br> <span> 不</span><span><span>过,官方文档还说,"要想覆盖客户端上的自动检测编码功能,可在用于连接到服务器的URL中使用“characterEncoding”属性。" </span></span><br> </p> <p><span><span><br> </span></span></p> <p><span><span><span>解决方法二: </span><br> <br> <span>连接mysql时(无论在从mysql读还是取数据的情况),指定使用的编码方式为utf-8,具体代码如下 </span><br> <br> <span>//装载mysql-jdbc驱动 </span><br> <br> <span>Class.forName("com.mysql.jdbc.Driver").newInstance(); </span><br> <br> <span>//连接数据库 </span><br> <br> <span>Connection sqlCon = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test? user=root&password=1&useUnicode=true&characterEncoding=utf-8" ); </span><br> <br> <br> </span></span></p>

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use