From c871f6eb77d9f3663e22dae577de80774b2be1b8 Mon Sep 17 00:00:00 2001
From: tennc
Date: Fri, 24 May 2013 08:13:55 +0800
Subject: [PATCH] update Drag
---
...1\345\215\225\344\270\252\350\241\250.jsp" | 83 +++++++++++++++++
...1\350\243\244\350\204\232\346\234\254.jsp" | 71 +++++++++++++++
...l\346\225\260\346\215\256\345\272\223.jsp" | 89 +++++++++++++++++++
3 files changed, 243 insertions(+)
create mode 100644 "drag/mysql\346\225\260\346\215\256\345\272\223\350\204\261\345\215\225\344\270\252\350\241\250.jsp"
create mode 100644 "drag/oracle\350\204\261\350\243\244\350\204\232\346\234\254.jsp"
create mode 100644 "drag/\350\204\261mysql\346\225\260\346\215\256\345\272\223.jsp"
diff --git "a/drag/mysql\346\225\260\346\215\256\345\272\223\350\204\261\345\215\225\344\270\252\350\241\250.jsp" "b/drag/mysql\346\225\260\346\215\256\345\272\223\350\204\261\345\215\225\344\270\252\350\241\250.jsp"
new file mode 100644
index 00000000..8cd7b868
--- /dev/null
+++ "b/drag/mysql\346\225\260\346\215\256\345\272\223\350\204\261\345\215\225\344\270\252\350\241\250.jsp"
@@ -0,0 +1,83 @@
+<%@ page import="java.sql.*" %>
+<%@ page import="java.util.*" %>
+<%@ page import="java.io.*" %>
+<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%
+try {
+
+ String table=request.getParameter("table_name");
+
+ if(table!=null&&!table.equals("")){
+
+
+ String backupDir = request.getParameter("bak_path");
+ String ex=".txt";
+ String driver =request.getParameter("driver");
+ String url = request.getParameter("url");
+ String username = request.getParameter("username");
+ String password = request.getParameter("password");
+
+ Class.forName(driver);
+ Connection conn = DriverManager.getConnection(url, username, password);
+
+ ResultSetMetaData rsmd = null;
+ ResultSet rs=null;
+ Statement stmt = conn.createStatement();
+
+ out.println("Dumping data for table " + table + "...
");
+ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(backupDir+table+ex), "UTF-8");
+ BufferedWriter bw=new BufferedWriter(osw);
+ rs = stmt.executeQuery("SELECT * FROM " + table);
+ rsmd = rs.getMetaData();
+ while (rs.next()) {
+ bw.append("INSERT INTO " + table + " VALUES(");
+ // JDBC is 1-based, Java is not !?
+ for (int col = 1; col <= rsmd.getColumnCount(); col++) {
+ bw.append("'");
+ if (rs.getString(col) == null)
+ bw.append("");
+ else
+ bw.append(rs.getString(col));
+ if (col == rsmd.getColumnCount())
+ bw.append("'");
+ else
+ bw.append("',");
+ }
+ bw.append(");");
+ bw.newLine();
+ }
+ bw.flush();
+ bw.close();
+ osw.close();
+ rs.close();
+ stmt.close();
+
+ out.println("backup is ok");
+
+ conn.close();
+
+ }
+ else{
+
+ out.println("输入表名...");
+
+ }
+
+
+} catch (Exception e) {
+ response.setStatus(200);
+ e.printStackTrace();
+}
+
+%>
+
diff --git "a/drag/oracle\350\204\261\350\243\244\350\204\232\346\234\254.jsp" "b/drag/oracle\350\204\261\350\243\244\350\204\232\346\234\254.jsp"
new file mode 100644
index 00000000..80952778
--- /dev/null
+++ "b/drag/oracle\350\204\261\350\243\244\350\204\232\346\234\254.jsp"
@@ -0,0 +1,71 @@
+<%@ page import="java.sql.*" %>
+<%@ page import="java.util.*" %>
+<%@ page import="java.io.*" %>
+<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%
+try {
+ String backupDir = "/usr/data/";
+ String ex=".txt";
+
+ String driver = "oracle.jdbc.driver.OracleDriver";
+ String url = "jdbc:oracle:thin:user/pass@localhost:1521:orcl";
+ String username = "user";
+ String password = "pass";
+
+
+ Class.forName(driver);
+ Connection conn = DriverManager.getConnection(url, username, password);
+
+ // Get tables
+ String sql_tables="select TABLE_NAME from user_tab_comments";
+ PreparedStatement ps = conn.prepareStatement(sql_tables);
+ ResultSet rs = ps.executeQuery();
+ ArrayList tables = new ArrayList();
+ while (rs.next()) {
+ tables.add(rs.getString(1));
+ }
+ rs.close();
+
+ for(int i=0;i");
+ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(backupDir+table+ex), "UTF-8");
+ BufferedWriter bw=new BufferedWriter(osw);
+ String sql="select * from "+table;
+ PreparedStatement p = conn.prepareStatement(sql);
+ ResultSet r = p.executeQuery();
+ ResultSetMetaData rsmeta=r.getMetaData();
+
+ while(r.next()){
+ bw.append("INSERT INTO " + table + " VALUES(");
+ // JDBC is 1-based, Java is not !?
+ for (int col = 1; col <= rsmeta.getColumnCount(); col++) {
+ bw.append("'");
+ if (r.getString(col) == null)
+ bw.append("");
+ else
+ bw.append(r.getString(col));
+ if (col == rsmeta.getColumnCount())
+ bw.append("'");
+ else
+ bw.append("', ");
+ }
+ bw.append(");");
+ bw.newLine();
+ }
+
+ bw.flush();
+ bw.close();
+ osw.close();
+ r.close();
+ }
+
+ rs.close();
+ out.println("backup is ok");
+ conn.close();
+} catch (Exception e) {
+ response.setStatus(200);
+ e.printStackTrace();
+}
+out.println("finished
");
+%>
diff --git "a/drag/\350\204\261mysql\346\225\260\346\215\256\345\272\223.jsp" "b/drag/\350\204\261mysql\346\225\260\346\215\256\345\272\223.jsp"
new file mode 100644
index 00000000..3e368cf2
--- /dev/null
+++ "b/drag/\350\204\261mysql\346\225\260\346\215\256\345\272\223.jsp"
@@ -0,0 +1,89 @@
+<%@ page import="java.sql.*" %>
+<%@ page import="java.util.*" %>
+<%@ page import="java.io.*" %>
+<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%
+try {
+ //备份文件存放的绝对路径
+ String backupDir = "c:/";
+ String ex=".txt";
+ String driver = "com.mysql.jdbc.Driver";
+
+ String url = "jdbc:mysql://localhost:3306/dbname";
+ String username = "user";
+ String password = "pass";
+
+ Class.forName(driver);
+ Connection conn = DriverManager.getConnection(url, username, password);
+
+ // Get tables
+ DatabaseMetaData dmd = conn.getMetaData();
+ ResultSet rs = dmd.getTables(null, null, "%", null);
+ ArrayList tables = new ArrayList();
+ while (rs.next()) {
+ tables.add(rs.getString(3));
+ }
+ rs.close();
+
+
+
+ ResultSetMetaData rsmd = null;
+ Statement stmt = conn.createStatement();
+ for (String table : tables) {
+
+ rs = stmt.executeQuery("SHOW CREATE TABLE " + table);
+ rsmd = rs.getMetaData();
+ while (rs.next()) {
+ /*
+ * mysql> SHOW CREATE TABLE t\G
+ *************************** 1. row ***************************
+ * Table: t
+ * Create Table: CREATE TABLE t (
+ * id int(11) default NULL auto_increment,
+ * s char(60) default NULL,
+ * PRIMARY KEY (id)
+ * ) TYPE=MyISAM
+ */
+ // JDBC is 1-based, Java is not !?
+// osw.append(rs.getString(2) + "\n\n");
+ }
+ rs.close();
+
+ out.println("Dumping data for table " + table + "...
");
+ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(backupDir+table+ex), "UTF-8");
+ BufferedWriter bw=new BufferedWriter(osw);
+ rs = stmt.executeQuery("SELECT * FROM " + table);
+ rsmd = rs.getMetaData();
+ while (rs.next()) {
+ bw.append("INSERT INTO " + table + " VALUES(");
+ // JDBC is 1-based, Java is not !?
+ for (int col = 1; col <= rsmd.getColumnCount(); col++) {
+ bw.append("'");
+ if (rs.getString(col) == null)
+ bw.append("");
+ else
+ bw.append(rs.getString(col));
+ if (col == rsmd.getColumnCount())
+ bw.append("'");
+ else
+ bw.append("',");
+ }
+ bw.append(");");
+ bw.newLine();
+ }
+ bw.flush();
+ bw.close();
+ osw.close();
+ rs.close();
+ }
+ stmt.close();
+
+ out.println("backup is ok");
+
+ conn.close();
+} catch (Exception e) {
+ response.setStatus(200);
+ e.printStackTrace();
+}
+out.println("finished
");
+%>