程序背景及问题描述:一个小小外包项目(更确切是私活),需要对数据库数据做下备份(简单备份,数据量不大,保证数据不丢失就ok),能够从备份文件恢复数据到数据库。
环境:数据量---MySQL 5.0;Tomcat 6.0;B/S模式
解决方案:哥想到解决方案就是将数据从数库读取出来以一定格式写成xml文件,保存在文件系统中;然后再提供一个从备份文件中恢复的功能,读取备份文件,解析数据,再插入回数据库中。
可以这样做的前提貌似是:1、数据库数据不能有 < > 这样的特殊字符。假设有的话,xml解析貌似会有问题
具体实现:利用dom4j的强大加牛逼功能。
想要利用dom4j的强大牛逼能力,需要 dom4j-x.x.x.jar这个包,google下能找到下载地址。
写了个备份文件工具类BackupFileUtil
异常处理那块可以做些优化
代码如下:
package wuhuiwen.backup; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import wuhuiwen.conn.ConnectionSource; public class BackupFileUtil { public static void createBackupFile(String path){ File file = new File(path); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Connection conn = null; PreparedStatement psmt = null; java.sql.ResultSet rs = null; try { XMLWriter writer = new XMLWriter(new FileOutputStream(path)); Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("backup"); Element customers = root.addElement("customers"); Element inRecords = root.addElement("inRecords"); Element inRecordItems = root.addElement("inRecordItems"); Element outRecords = root.addElement("outRecords"); Element outRecordItems = root.addElement("outRecordItems"); conn = ConnectionSource.getConnection(); String sql = "select id,name,phone,address from customer"; psmt = conn.prepareStatement(sql); rs = psmt.executeQuery(); while(rs.next()){ Element customer = customers.addElement("customer"); customer.addElement("id").addText(rs.getString("id")); customer.addElement("name").addText(rs.getString("name")); customer.addElement("phone").addText(rs.getString("phone")); customer.addElement("address").addText(rs.getString("address")); } sql = "select id,sum_price,date,status from in_record"; psmt = conn.prepareStatement(sql); rs = psmt.executeQuery(); while(rs.next()){ Element inRecord = inRecords.addElement("inRecord"); inRecord.addElement("id").addText(rs.getString("id")); inRecord.addElement("sum_price").addText(rs.getString("sum_price")); inRecord.addElement("date").addText(rs.getString("date")); inRecord.addElement("status").addText(rs.getString("status")); } sql = "select id,in_id,goods_name,unit_price,quantity,sum_price from in_record_item"; psmt = conn.prepareStatement(sql); rs = psmt.executeQuery(); while(rs.next()){ Element inRecordItem = inRecordItems.addElement("inRecordItem"); inRecordItem.addElement("id").addText(rs.getString("id")); inRecordItem.addElement("in_id").addText(rs.getString("in_id")); inRecordItem.addElement("goods_name").addText(rs.getString("goods_name")); inRecordItem.addElement("unit_price").addText(rs.getString("unit_price")); inRecordItem.addElement("quantity").addText(rs.getString("quantity")); inRecordItem.addElement("sum_price").addText(rs.getString("sum_price")); } sql = "select id,customer_id,customer_name,sum_price,date,pay,nopay,status from out_record"; psmt = conn.prepareStatement(sql); rs = psmt.executeQuery(); while(rs.next()){ Element outRecord = outRecords.addElement("outRecord"); outRecord.addElement("id").addText(rs.getString("id")); outRecord.addElement("customer_id").addText(rs.getString("customer_id")); outRecord.addElement("customer_name").addText(rs.getString("customer_name")); outRecord.addElement("sum_price").addText(rs.getString("sum_price")); outRecord.addElement("date").addText(rs.getString("date")); outRecord.addElement("pay").addText(rs.getString("pay")); outRecord.addElement("nopay").addText(rs.getString("nopay")); outRecord.addElement("status").addText(rs.getString("status")); } sql = "select id,out_id,goods_name,type,unit_price,quantity,sum_price from out_record_item"; psmt = conn.prepareStatement(sql); rs = psmt.executeQuery(); while(rs.next()){ Element outRecordItem = outRecordItems.addElement("outRecordItem"); outRecordItem.addElement("id").addText(rs.getString("id")); outRecordItem.addElement("out_id").addText(rs.getString("out_id")); outRecordItem.addElement("goods_name").addText(rs.getString("goods_name")); outRecordItem.addElement("type").addText(rs.getString("type")); outRecordItem.addElement("unit_price").addText(rs.getString("unit_price")); outRecordItem.addElement("quantity").addText(rs.getString("quantity")); outRecordItem.addElement("sum_price").addText(rs.getString("sum_price")); } writer.write(doc); writer.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (psmt != null) { try { psmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static boolean recoveryFromBackup(String path){ Connection conn = null; PreparedStatement psmt = null; java.sql.ResultSet rs = null; try { conn = ConnectionSource.getConnection(); SAXReader reader = new SAXReader(); Document document = reader.read(new File(path)); Element rootElm = document.getRootElement(); Element customers = rootElm.element("customers"); // "member"是节点名 String sql ="delete from customer"; psmt = conn.prepareStatement(sql); psmt.executeUpdate(); sql ="insert into customer(id,name,phone,address) values(?,?,?,?)"; psmt = conn.prepareStatement(sql); List customerNodes = customers.elements("customer"); for(Iterator it=customerNodes.iterator();it.hasNext();) { Element customer = (Element)it.next(); psmt.setInt(1, Integer.parseInt(customer.element("id").getTextTrim())); psmt.setString(2, customer.element("name").getTextTrim()); psmt.setString(3, customer.element("phone").getTextTrim()); psmt.setString(4, customer.element("address").getTextTrim()); psmt.executeUpdate(); } Element inRecords = rootElm.element("inRecords"); // "member"是节点名 sql ="delete from in_record"; psmt = conn.prepareStatement(sql); psmt.executeUpdate(); sql ="insert into in_record(id,sum_price,date,status) values(?,?,?,?)"; psmt = conn.prepareStatement(sql); List inRecordNodes = inRecords.elements("inRecord"); for(Iterator it=inRecordNodes.iterator();it.hasNext();) { Element inRecord = (Element)it.next(); psmt.setInt(1, Integer.parseInt(inRecord.element("id").getTextTrim())); psmt.setDouble(2, Double.parseDouble(inRecord.element("sum_price").getTextTrim())); psmt.setString(3, inRecord.element("date").getTextTrim()); psmt.setInt(4, Integer.parseInt(inRecord.element("status").getTextTrim())); psmt.executeUpdate(); } Element inRecordItems = rootElm.element("inRecordItems"); // "member"是节点名 sql ="delete from in_record_item"; psmt = conn.prepareStatement(sql); psmt.executeUpdate(); sql ="insert into in_record_item(id,in_id,goods_name,unit_price,quantity,sum_price) values(?,?,?,?,?,?)"; psmt = conn.prepareStatement(sql); List inRecordItemNodes = inRecordItems.elements("inRecordItem"); for(Iterator it=inRecordItemNodes.iterator();it.hasNext();) { Element inRecordItem = (Element)it.next(); psmt.setInt(1, Integer.parseInt(inRecordItem.element("id").getTextTrim())); psmt.setInt(2, Integer.parseInt(inRecordItem.element("in_id").getTextTrim())); psmt.setString(3, inRecordItem.element("goods_name").getTextTrim()); psmt.setDouble(4, Double.parseDouble(inRecordItem.element("unit_price").getTextTrim())); psmt.setInt(5, Integer.parseInt(inRecordItem.element("quantity").getTextTrim())); psmt.setDouble(6, Double.parseDouble(inRecordItem.element("sum_price").getTextTrim())); psmt.executeUpdate(); } Element outRecords = rootElm.element("outRecords"); // "member"是节点名 sql ="delete from out_record"; psmt = conn.prepareStatement(sql); psmt.executeUpdate(); sql ="insert into out_record(id,customer_id,customer_name,sum_price,date,pay,nopay,status) values(?,?,?,?,?,?,?,?)"; psmt = conn.prepareStatement(sql); List outRecordNodes = outRecords.elements("outRecord"); for(Iterator it=outRecordNodes.iterator();it.hasNext();) { Element outRecord = (Element)it.next(); psmt.setInt(1, Integer.parseInt(outRecord.element("id").getTextTrim())); psmt.setInt(2, Integer.parseInt(outRecord.element("customer_id").getTextTrim())); psmt.setString(3, outRecord.element("customer_name").getTextTrim()); psmt.setDouble(4, Double.parseDouble(outRecord.element("sum_price").getTextTrim())); psmt.setString(5, outRecord.element("date").getTextTrim()); psmt.setDouble(6, Double.parseDouble(outRecord.element("pay").getTextTrim())); psmt.setDouble(7, Double.parseDouble(outRecord.element("nopay").getTextTrim())); psmt.setInt(8, Integer.parseInt(outRecord.element("status").getTextTrim())); psmt.executeUpdate(); } Element outRecordItems = rootElm.element("outRecordItems"); // "member"是节点名 sql ="delete from out_record_item"; psmt = conn.prepareStatement(sql); psmt.executeUpdate(); sql ="insert into out_record_item(id,out_id,goods_name,type,unit_price,quantity,sum_price) values(?,?,?,?,?,?,?)"; psmt = conn.prepareStatement(sql); List outRecordItemNodes = outRecordItems.elements("outRecordItem"); for(Iterator it=outRecordItemNodes.iterator();it.hasNext();) { Element outRecordItem = (Element)it.next(); psmt.setInt(1, Integer.parseInt(outRecordItem.element("id").getTextTrim())); psmt.setInt(2, Integer.parseInt(outRecordItem.element("out_id").getTextTrim())); psmt.setString(3, outRecordItem.element("goods_name").getTextTrim()); psmt.setString(4, outRecordItem.element("type").getTextTrim()); psmt.setDouble(5, Double.parseDouble(outRecordItem.element("unit_price").getTextTrim())); psmt.setInt(6, Integer.parseInt(outRecordItem.element("quantity").getTextTrim())); psmt.setDouble(7, Double.parseDouble(outRecordItem.element("sum_price").getTextTrim())); psmt.executeUpdate(); } return true; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (psmt != null) { try { psmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return false; } public static void main(String[] args) { recoveryFromBackup("F://xmlText.xml"); } }
学习交流><欢迎拍砖