<%@ page contentType="text/html; charset=utf-8" %> <%@ page language="java" %> <%@ page import="java.sql.*" %> <% //author: By Gavin //Usage: wget "http://xxx.com/wget_db.jsp?sn=0&en=5000000&ln=50000" -O gavin.sql out.clear(); //分段每次limit查询出来的条数,根据实际情况调整,默认为2w int MAX_LIMIT_NUM = 20000; //最大缓存条数,防止占用过多内存,根据每条数据大小调整 int MAX_CACHE_NUM = 5000; //驱动程序名 String driverName="com.mysql.jdbc.Driver"; // 数据库地址 String dbAddress = "127.0.0.1:3306"; //数据库用户名 String userName="root"; //密码 String userPasswd="root"; //数据库名 String dbName="DBName"; // 查询字段 String columns[] = "username,password".split(","); //表名 String tableName="table_name"; // 接受参数 int startNum = Integer.valueOf(request.getParameter("sn")); //接收起始条数 int endNum = Integer.valueOf(request.getParameter("en")); //接收结束条数 String ln = request.getParameter("ln"); if (ln != null && ln != "") MAX_LIMIT_NUM = Integer.valueOf(ln); //接收每次分段查询的条数 int gavin_downNum = endNum - startNum; //计算总下载条数 if (endNum < MAX_LIMIT_NUM) MAX_LIMIT_NUM = endNum; int multiple = gavin_downNum/MAX_LIMIT_NUM; int complement = gavin_downNum%MAX_LIMIT_NUM; // 连接数据库 String url="jdbc:mysql://"+dbAddress+"/"+dbName+"?user="+userName+"&password="+userPasswd; Class.forName(driverName).newInstance(); Connection connection=DriverManager.getConnection(url); Statement statement = connection.createStatement(); // 拼装前半部分sql String sql = "SELECT "; for(int i=0;i"); } } if (num >= MAX_CACHE_NUM) { out.flush(); num = 0; } } rs.close(); } statement.close(); connection.close(); %>