JDBC的XML编码和Delphi融合时的编码问题

guowenrui
03-07-01 225

我使用JDBC进行数据表访问时,由于需要同DELPHI进行整合,因此使用了XML文件作为数据传输媒介,但是在进行编码转换时中文没有转换,请大家看一下该如何做。
下面的代码请大家看一下,该如何将中文转化成正常的UTF-8编码方式。转化编码方式在程序的未尾处。

这里容我声明一点,我使用的开发环境是JBuilder 8,后台数据库是SQL Server 2000,JDBC驱动是微软自带的SQL Server 2000 JDBC Driver。下面是我写的程序,请仔细看一下,有没有问题:

//====================Code Start Here============================
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;

public class JavaDelphi {
private static final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String strUrl = "jdbc:microsoft:sqlserver://devserver:1433;DatabaseName=Java";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private FileWriter fw = null;
private String[] fieldNames = null;

public static void main(String[] args) throws SQLException, IOException {
JavaDelphi jdbcXml = new JavaDelphi();
jdbcXml.transform("test.xml");
}

private void transform(String filename) throws SQLException, IOException {
try {
Class.forName(dbDriver);
}catch(ClassNotFoundException e) {
System.err.println("ClassNotFoundException :" + e.getMessage());
System.exit(1);
}

try {
conn = DriverManager.getConnection(strUrl, "SA", "");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM coffees");
fw = new FileWriter(filename);
/*
while(rs.next()) {
System.out.println(rs.getString("cof_name"));
}
*/
writeXml();
}finally{
try {
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
System.err.println("SQLException :" + e.getMessage());
}

try {
fw.close();
}catch(IOException e){
System.err.println("IOException :" + e.getMessage());
}

}
}

private void writeXml() throws UnsupportedEncodingException, IOException, SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
String s = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>";
fw.write(new String(s.getBytes("utf-8"), "utf-8"));
s = "<DATAPACKET version=\"2.0\">";
s += "<METADATA>";
s += "<FIELDS>";
fieldNames = new String[rsmd.getColumnCount()];
for(int i=1;i<=rsmd.getColumnCount();i++){
String attrname = rsmd.getColumnName(i);
fieldNames[i-1] = attrname;
String fieldtype = rsmd.getColumnTypeName(i);
//int width = rsmd.getColumnDisplaySize(i);
int width = rsmd.getPrecision(i);
s += "<FIELD attrname=\"" + attrname + "\" fieldtype=\"" + fieldtype + "\" WIDTH=\"" + width + "\"/>";
}
s += "</FIELDS>";
s += "<PARAMS/>";
s += "</METADATA>";
s += "<ROWDATA>";
//rs.beforeFirst();
while(rs.next()){
s += "<ROW ";
for(int j=0;j<fieldNames.length;j++){
s += fieldNames[j] + "=\"" + rs.getObject(j+1) + "\" ";
}
s += "/>";
}
s += "</ROWDATA>";
s += "</DATAPACKET>";
String s2 = new String(s.getBytes("gb2312"), "utf-8");
//String s2 = new String(s.getBytes("iso8859-1"), "utf-8");
fw.write(s2);
fw.flush();
}


}