java数据库等——乱码问题解决办法
中文乱码解决方案:
数据库乱码:
UTF-8对中文用3个字节来表示
String s =”飞翔”;
byte[] utf8 = s.getBytes(“utf-8”);//获得utf-8编码的字节值
System….(new String(utf8,”gbk”);//用GBK编码格式对UTF-8编码格式的字节数组进行解码,将产生乱码。
查看数据库的字符集
show variables like 'character\_set\_%';
show variables like 'collation_%';
--设置数据库字符编码
set names 'GBK'
alter database payment character set GBK;
create database mydb character set GBK;
set character_set_client=gbk;
set character_set_connection=gbk;
set character_set_database=gbk;
set character_set_results=gbk;
set character_set_server=gbk;
set character_set_system=gbk;
set collation_connection=gbk;
set collation_database=gbk;
set collation_server=gbk;
Servlet解决服务器端传给客户端产生的乱码问题
添加GB2312编码
向服务器输出中文时出现乱码或“?”最常用的解决方案:
service(HttpServletRequest request,HttpServletResponse response){
String s =” 中文”;
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out = response.getWriter();
out.println(s);
}
以上方法有时候会失灵,所以使用下面试试:
service(){
String s = “中文”;
PrintWriter out = response.getWriter();
response.setHeader(“Content-Type”,”text/html;charset=utf-8”);//设置Conten-Type响应头
out.println(new String(s.getByte(“utf-8”),”iso-8859-1”));
}//获得utf-8编码的字节数组后,将其按原样保存在String对象中
}
1struts:
String name =request.getParameter("username");
String username;
try {
username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "gb2312" );
username = new String(username.getBytes(“ISO8859-1”),”GBK”);
2 struts2
HttpServletRequest request=ServletActionContext.getRequest();String
str=new Strin
相关文档:
JAVA的设计原则
1. 接口隔离原则(ISP:Interface Segregation Principle)
定义:使用多个专门的比使用单一的总接口要好。也可以说:建立单一接口,不要建立臃肿庞大的接口。
ISP的两种定义:
◇ “Clients should not be forced to depend upon interfaces that they don't u ......
Java Thread有一个 volatile关键字,主要用来防止多线程访问公共变量,不能及时被其它线程正确访问, 而造成的不同步问题。
比如ThreadA 和ThreadB 同时访问一个 int a = 0; 并且修改,
可能会造成 ThreadA 修改a变量后,ThreadB访问a变量还是原来的值。
&n ......
import java.util.Random;
/**
* 排序测试类
*
* 排序算法的分类如下:
* 1.插入排序(直接插入排序、折半插入排序、希尔排序);
* 2.交换排序(冒泡泡排序、快速排序);
* 3.选择排序(直接选择排序、堆排序);
* 4.归并排序;
* 5.基数排序。
&nbs ......
/*
* EncryptUtils.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* ......
/*
* CookieSupport.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version ......