1. 程式人生 > >Unknown initial character set index '255' received from server. Initial client character 解決方法

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

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驅動和資料庫字符集設定不搭配
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;
	}
}