XML에서 데이터베이스의 데이터를 내보내는 코드가 있습니다.Java에서 전체 데이터베이스의 데이터를 내보내는 방법은 무엇입니까?
public class DataExtractor{
private final String login, passwd, host;
public DataExtractor(String login, String passwd, String host){
this.login = login;
this.passwd = passwd;
this.host = host;
}
public String getTableData(String tableName) throws SQLException, ClassNotFoundException {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///" + host, login, passwd);
st = con.createStatement();
rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
StringBuilder b = new StringBuilder("<table>\n");
int num = 1;
while (rs.next()) {
b.append("<row>");
b.append("<num>").append(num++).append("</num>");
for (int i = 1; i <= colCount; i++) {
String columnName = rsmd.getColumnName(i);
b.append('<').append(columnName).append('>');
b.append(rs.getObject(i));
b.append("</").append(columnName).append('>');
}
b.append("</row>\n");
}
b.append("</table>");
return b.toString();
} catch (SQLException e) {
throw e;
} catch (ClassNotFoundException e) {
throw e;
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (st != null)
try {
st.close();
} catch (SQLException e) {
}
if (con != null)
try {
con.close();
} catch (SQLException e) {
}
}
}
}
그러나 데이터가 특정 테이블에서 전체 데이터베이스 (즉, 모든 테이블)에서 내 보낸, 그리고 있는지 확인하는 방법 데이터 만 주어진 테이블에서 수출의 클래스입니다. 즉, 출력 XML 파일 형식 가야한다 :
<database>
<table1>
<id> </ id>
<Name> </ Name>
...
</ table1>
<table2>
...
</ table2>
</ database>
DBMS 문서를보고 싶을 수 있습니다. 대부분은 CVS, XML 또는 바이너리에 내장 된 내보내기 기능을 제공합니다 ... – home