JDBC的XML編碼和Delphi融合時的編碼問題
我使用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();
}
}
下面的程式碼請大家看一下,該如何將中文轉化成正常的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();
}
}
相關文章
- lxml處理xml時的字元編碼問題XML字元
- jdbc編碼?JDBC
- maven的編碼問題、解決和疑問Maven
- jsp的編碼問題JS
- JAVA的中文編碼問題Java
- php編碼問題PHP
- 字元編碼問題字元
- PHP 與 JS 的編碼問題PHPJS
- Python的中文編碼問題Python
- 關於xml編碼問題在VB,PHP,JAVA下的解決方案XMLPHPJava
- PHP和mySQL的字符集編碼問題(1)PHPMySql
- Ubuntu中 MySQL 的中文編碼問題UbuntuMySql
- Delphi編碼標準——過程和函式 (轉)函式
- Delphi編碼標準——元件命名 (轉)元件
- 關於JS的編碼轉換問題JS
- 關於Java編碼規範的問題Java
- Java編碼易疏忽的十個問題Java
- 資訊系統中的編碼問題
- [CareerCup] 17.10 Encode XML 編碼XMLXML
- XML入門指南(15)XML編碼(轉)XML
- java連線資料庫時jdbc設定編碼Java資料庫JDBC
- MySQL 中字元編碼問題MySql字元
- 字元編碼問題記錄字元
- python 中文編碼問題Python
- 轉python編碼問題Python
- jdom解析中文編碼問題
- C++windows編碼問題C++Windows
- xml+xsl應用,包含中文字元的URL編碼問題 (轉)XML字元
- 命令列的亂碼以及編碼的問題的解決方法命令列
- Python 字元編碼問題和其他一些問題Python字元
- 【字元編碼】Java字元編碼詳細解答及問題探討字元Java
- Delphi編碼標準——一般的原始碼格式規則 (轉)原始碼
- Delphi編碼標準——檔案命名 (轉)
- Unity使用VS開發編碼格式的問題Unity
- 對於一個編解碼問題的思考
- iOS編碼需要注意的幾點問題iOS
- 深入分析 Java 中的中文編碼問題Java
- [golang]一個複雜的中文編碼問題Golang