欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unknown initial character set index '255' received from server. Initial client character 解决方法

程序员文章站 2024-03-04 21:27:06
...

Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
从错误的提示信息中发现字符集设置出现问题

mysql连接数据库时报此错误:
//String url = "jdbc:mysql://localhost:3306/db_cjky" 如果使用这句就会报错。
//Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
String url = "jdbc:mysql://localhost:3306/db_cjky?useUnicode=true&characterEncoding=utf8";//改成这句,就可以了

最终解决方法:
删除 \WebContent\WEB-INF\lib目录下的。mysql-connector的jar文件。原因是:MySQL驱动和数据库字符集设置不搭配
Unknown initial character set index '255' received from server. Initial client character 解决方法

package com.lyq.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * ��Ʒ���ݿ����
 * @author Li YongQiang
 *
 */

public class BookDao {
	/**
	 * ��ȡ���ݿ�����
	 * @return Connection����
	 */
	public Connection getConnection(){
		// ���ݿ�����
		Connection conn = null;
		try {
			// �������ݿ�������ע�ᵽ����������
			Class.forName("com.mysql.jdbc.Driver");
			// ���ݿ������ַ���
			//String url = "jdbc:mysql://localhost:3306/db_cjky" 如果使用这句就会报错。
			//Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
			String url = "jdbc:mysql://localhost:3306/db_cjky?useUnicode=true&characterEncoding=utf8";//改成这句,就可以了。
			// ���ݿ��û���
			String username = "root";
			// ���ݿ�����
			String password = "123456";
			// ����Connection����
			conn = DriverManager.getConnection(url,username,password);
			if(conn!=null)
			{System.out.println("database is good, you are good");}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// �������ݿ�����
		return conn;
	}
	
	/**
	 * ��ҳ��ѯ������Ʒ��Ϣ
	 * @param page ҳ��
	 * @return List<Product>
	 */
	public List<Product> find(int page){
		// ����List
		List<Product> list = new ArrayList<Product>();
		// ��ȡ���ݿ�����
		Connection conn = getConnection();
		// ��ҳ��ѯ��SQL���
		String sql = "select * from tb_product order by id limit ?,?";
		try {
			// ��ȡPreparedStatement
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, (page - 1) * Product.PAGE_SIZE);
			// ��SQL����еĵ�2��������ֵ
			ps.setInt(2, Product.PAGE_SIZE);
			// ִ�в�ѯ����
			ResultSet rs = ps.executeQuery();
			// �������ƶ������ж��Ƿ���Ч
			while(rs.next()){
				// ʵ����Product
				Product p = new Product();
				// ��id���Ը�ֵ
				p.setId(rs.getInt("id"));
				// ��name���Ը�ֵ
				p.setName(rs.getString("name"));
				// ��num���Ը�ֵ
				p.setNum(rs.getInt("num"));
				// ��price���Ը�ֵ
				p.setPrice(rs.getDouble("price"));
				// ��unit���Ը�ֵ
				p.setUnit(rs.getString("unit"));
				// ��Product��ӵ�List������
				list.add(p);
			}
			// �ر�ResultSet
			rs.close();
			// �ر�PreparedStatement
			ps.close();
			// �ر�Connection
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * ��ѯ�ܼ�¼��
	 * @return �ܼ�¼��
	 */
	public int findCount(){
		// �ܼ�¼��
		int count = 0;
		// ��ȡ���ݿ�����
		Connection conn = getConnection();
		// ��ѯ�ܼ�¼��SQL���
		String sql = "select count(*) from tb_product";
		try {
			// ����Statement
			Statement stmt = conn.createStatement();
			// ��ѯ����ȡResultSet
			ResultSet rs = stmt.executeQuery(sql);
			// �������ƶ������ж��Ƿ���Ч
			if(rs.next()){
				// ���ܼ�¼����ֵ
				count = rs.getInt(1);
			}
			// �ر�ResultSet
			rs.close();
			// �ر�Connection
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// �����ܼ�¼��
		return count;
	}
}