JDBC的XML編碼和Delphi融合時的編碼問題

guowenrui發表於2003-07-01
我使用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();
}


}

相關文章