Home Database Mysql Tutorial 关于mysql数据库“Incorrectstringvalue:'/xFF/xFE/_MySQL

关于mysql数据库“Incorrectstringvalue:'/xFF/xFE/_MySQL

Jun 01, 2016 pm 01:18 PM
mysql database

bitsCN.com

或许很多人能和我一样在使用mysql数据库时会遇到这样的问题,无法显示中文字符,而且无法无法被加载进数据库。

这就是程序员都会遇到的字符编码问题,接下来以JAVA链接数据库为列说明。

package fang.Demo;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import javax.swing.JPopupMenu.Separator;public class ConnectDemo04 {	// 定义MySQL的数据库驱动程序	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";	// 定义MySQL数据库的连接地址	public static final String DBURL = "jdbc:mysql://localhost:3306/demo2";	// MySQL数据库的连接用户名	public static final String DBUSER = "root";	// MySQL数据库的连接密码	public static final String DBPASS = "";	public static void main(String args[]) throws Exception { // 所有的异常抛出		Connection conn = null; // 数据库连接		PreparedStatement stmt = null; // 数据库操作		ResultSet res = null;		Class.forName(DBDRIVER); // 加载驱动程序		String name = "方萨安" ;	// 表示姓名		String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;		Class.forName(DBDRIVER) ;	// 加载驱动程序		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;		stmt = conn.prepareStatement(sql) ;	// 创建PreapredStatement对象		File f = new File("d:" + File.separator + "test.txt") ;		InputStream input = null ;		input = new FileInputStream(f) ;	// 通过输入流读取文件		stmt.setString(1,name) ;		stmt.setAsciiStream(2,input,(int)f.length()) ;		stmt.executeUpdate() ;		conn.close() ;			// 数据库关闭	}};
Copy after login

这个java程序读取的是D盘目录下的test.txt文件

/

所报错误:

/

遇到这种问题可以看到错误提示是不正确的数据类型(大概是这个样子),然后我试了试数字和英文字母均可成运行,并写入数据库库

/

数据库数据图

/

由此可见,此问题必定是数据字符编码不正确引起的,那么如何解决呢?

从我建的数据表易知,他们都是utf8类型的国际化通用数据类型(推荐)

可是记事本的编码呢?它是什么类型?

但是……

/

但是我们却是可以设置的打开txt文件点击另存为—>设置编码。

/

设置为数据编码utf8

再次运行java程序

你会发现错误消失了,打开数据库文件你会发现数据已经成功写入数据表。

/

当然,网站报错的原理和这是一样的都是,数据表字符编码设置的问题,大家认真设置下就OK了

设置详见另一篇博文。

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

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's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles