- 论坛徽章:
- 0
|
jdbc连接db2(DB2 v8.1.1.60)后getString("XXX" 出来的字符串是乱码,无论我怎么转换,都是乱码,
我的数据库配置:
Database territory = CN
Database code page = 1386
Database code set = GBK
Database country/region code = 86
下面是源代码,有能力人士帮忙解答,谢谢:
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class NewData {
/**
* @param args
*/
public static void main(String[] args) {
NewData d = new NewData();
System.out.println(d.exec());
}
private String toGB2312(String str) {
byte gb2312[]=new byte[str.length()];
for(int i=0;i<str.length();i++)
gb2312[i]=(byte)str.charAt(i);
return new String(gb2312);
}
private String getObj(String ret)
{
//return new String(o.getBytes("iso-8859-1" , "GB2312" ;
String value="";
try {
value=new String(ret.getBytes("iso-8859-1" ,"gb2312" ;
//上面这个地方无论怎么换都不行
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
public String getStr(String str)
{
try{
String temp_p = str;
byte [] temp_t = temp_p.getBytes("UTF-16" ;
String temp = new String(temp_t);
return temp;
}
catch(Exception e){
return "";
}
}
private boolean exec()
{
boolean f=true;
Connection conn = getConn();
Statement stmt=null;
ResultSet rs=null;
try {
stmt=conn.createStatement();
String sql="select * from dcx.d_billcode fetch first 5 rows only";
rs=stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(getObj(rs.getString("brand_name" ));
}
/*
conn.setAutoCommit(false);
//Integer value=new Integer(200601);
CallableStatement cs=conn.prepareCall("{call dcx.p_vpmn_temp(?,?,?)}" ;
cs.setInt(1,200601);
cs.registerOutParameter(2,Types.INTEGER);
cs.registerOutParameter(3,Types.OTHER);
cs.executeQuery();
System.out.println("??" ;
System.out.println(cs.getInt(2));
System.out.println(cs.getObject(3));
*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
f=false;
}
finally
{
if(null!=rs)
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(null!=stmt)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(null!=conn)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return f;
}
private Connection getConn() {
Connection conn = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver" .newInstance();
String user = "xxxx";
String password = "xxxx";
String url = "jdbc:db2 xx";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
conn = null;
}
return conn;
}
} |
|