您当前的位置: 首页 >  ar

txwtech

暂无认证

  • 2浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

txwtech 发布时间:2018-06-23 23:02:26 ,浏览量:2

 

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驱动和数据库字符集设置不搭配

 

再补充一下,如果是在mybatis配置文件中的话应该修改成jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8

 

 

 

 

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
	 */
	public List find(int page){
		// ����List
		List list = new ArrayList();
		// ��ȡ���ݿ�����
		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;
	}
}

 

关注
打赏
1665060526
查看更多评论
立即登录/注册

微信扫码登录

0.0555s