String fontname="GB2312"; int type=Font.PLAIN; int size=36; Font font,font1; Label text; public jdbc() { font=new Font(fontname,type,size); font1=new Font(fontname,type,size); students = new Choice(); grades = new Choice(); name=new Choice(); String tempStr = new String("下银生".getBytes("ISO8859_1"),"GB2312"); text=new Label(tempStr); font1=grades.getFont(); System.out.println(font.getFamily()); System.out.println(font.getName());
--------------------------------------------------------------------------------在基于JAVA的编程中,经常会碰到汉字的处里及显示的问题,比如一大堆乱码或问号。
这是因为JAVA中默认的编码方式是UNICODE,而中国人通常使用的文件和DB都是基于GB2312或者BIG5等编码,故会出现此问题。以前我也经常为这个问题而苦恼,后来经查了些资料,终于解决了,我知道一定有很多朋友也会碰到这个问题,所以特就总结了一下,来拿出来让大家一起分享了。
1、在网页中输出中文。
JAVA在网络传输中使用的编码是"ISO-8859-1",故在输出时需要进行转化,如:
String str="中文";
str=new String(str.getBytes("GB2312"),"8859_1");
但如果在编译程序时,使用的编码是“GB2312”,且在中文平台上运行此程序,不会出现此问题,一定要注意。
2、从参数中读取中文
这正好与在网页中输出相反如:
str=new String(str.getBytes("8859_1"),"GB2312");
3、操作DB中的中文问题
一个较简单的方法是:在“控制面扳”中,把“区域”设置为“英语(美国)”。如果还会出现乱码,还可进行如下设置:
取中文时:str=new String(str.getBytes("GB2312"));
向DB中输入中文:str=new String(str.getBytes("ISO-8859-1")); 4、在JSP中的中文解决: 在“控制面扳”中,把“区域”设置为“英语(美国)”.
在JSP页面中加入:
如果还不行正常显示,则还要进行下面的转换:
如:name=new String(name.getBytes("ISO-8859-1"),"GBK");
就不会出现中文问题了。
import java.sql.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;public class jdbc extends Frame
{
Choice students;
Choice grades;
Choice name;
Connection connection;
Statement statement;
String fontname="GB2312";
int type=Font.PLAIN;
int size=36;
Font font,font1;
Label text; public jdbc()
{
font=new Font(fontname,type,size);
font1=new Font(fontname,type,size);
students = new Choice();
grades = new Choice();
name=new Choice();
String tempStr = new String("下银生".getBytes("ISO8859_1"),"GB2312");
text=new Label(tempStr);
font1=grades.getFont();
System.out.println(font.getFamily());
System.out.println(font.getName());
grades.setFont(font);
//System.out.println(font1.getFamily());
//System.out.println(font1.getName());
setLayout(new FlowLayout()); try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.75.213:1525:fyerp","fyerp","fyerp" );
//"jdbc:odbc:corejava", "Steve", "password");
statement = connection.createStatement();
String SQL = "SELECT userid,password,username FROM sys_user order by userid";
ResultSet resultset = statement.executeQuery(SQL);
while (resultset.next()) {
students.addItem(resultset.getString(1));
grades.addItem(resultset.getString(2));
//String tempStr = new String(resultset.getString(3).getBytes("ISO8859_1"),"GB2312");
name.addItem(resultset.getString(3));
System.out.println(resultset.getString(3));
}
//String tempStr = new String(resultset.getString(1).getBytes(),"GB2312");
//grades.addItem(tempStr);
connection.close();
}
catch(SQLException ex){
System.out.println("**************sql exception*********");
while (ex!=null){
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("Message: "+ex.getMessage());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex = ex.getNextException();
System.out.println("");
}
}
add(students);
add(grades);
add(name);
add(text);
//System.getProperties().list(System.out);
}
public static void main (String args[])
{
Frame f = new jdbc(); f.setSize(300, 300); f.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}}); f.show();
}
}