以XML格式提取数据库表

时间:2017-11-20 12:23:28

标签: java mysql xml

我正在尝试创建一个Java类,它将连接到MySQL数据库并从表中提取数据。

我跟着this tutorial。但问题是我不知道来自表格的数据如下:

  1. 我们有超过1个表格和
  2. 我们正在处理退出的大桌子。
  3. 建立连接但我无法将数据写入XML文件。

    到目前为止我的连接部分的代码是:

    public class ExtractTo {
        static String driverName = "com.mysql.jdbc.Driver";
        static String connectURL = "jdbc:mysql://localhost/[database_name]?";
       // static String user="root";
        //static String password="";
        static Statement stmt=null;
        static Connection db = null; 
        static ResultSet rslt=null;
        private static String SQLquery="SELECT * FROM [TABLE_NAME]";
        static ResultSetMetaData resultmetadata = null;
        static Document dataDoc=null;
        static Document Doc=null;
     public static void main(String [] args) {
        try {
    
           Class.forName(driverName).newInstance();
           db = DriverManager.getConnection(connectURL+"user=root&password=");
    
           stmt=db.createStatement();
           rslt=stmt.executeQuery(SQLquery);
        } [all the catch] ...
    }
    

1 个答案:

答案 0 :(得分:0)

如果有人和我有同样的问题,我会发布自己的答案。

我的最终代码是:

 try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder        = factory.newDocumentBuilder();
       Document doc  = builder.newDocument();
    File f = new File(enter_path_here);
    FileWriter writer = new FileWriter(f); 
   Class.forName(driverName).newInstance();
   db = DriverManager.getConnection(connectURL+"user=root&password=");

   Element results = doc.createElement("Final");
   doc.appendChild(results);    
   stmt=db.createStatement();
   rslt=stmt.executeQuery(SQLquery);
   resultmetadata=rslt.getMetaData();

   while(rslt.next()) {
       Element row = doc.createElement("Information");
         results.appendChild(row);

         for(int i=1;i<=resultmetadata.getColumnCount();i++) {
           String name=resultmetadata.getColumnName(i);
           Object value= rslt.getObject(i);
           Element node=  doc.createElement(name);
           node.appendChild(doc.createTextNode(value == null ? "" : value.toString()));
           row.appendChild(node);
       }

   }
   StreamResult result= new StreamResult(writer);
   DOMSource source = new DOMSource(doc);
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   transformer.transform(source, result);

} catch (ClassNotFoundException e) {
   System.out.println("Error creating class: "+e.getMessage());
} catch (SQLException e) {
   System.out.println("Error creating connection: "+e.getMessage());
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (TransformerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
finally {
    System.out.println("Closing Database Connection...");
    try {
        rslt.close();
        stmt.close();
        db.close();
    }catch(SQLException e){
        System.out.println("Can't close connection!" + e);
    }
}

}

上面的代码将创建一个.xml文件,其格式如上:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Final><Information><TABLE_ID>321231</TABLE_ID></Information></Final>

希望它有所帮助!