package stress; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.URL; import java.net.URLClassLoader; import java.sql.Connection; import java.sql.Driver; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; public class TableStreamer2 { public static void main(String[] args) throws Exception { if(args.length<1) { System.err.println("Option: "); System.err.println("File content should be like this:"); System.err.println(); System.err.println("user=________"); System.err.println("password=________"); System.err.println("path=c:/temp/mysql-connector-java-5.1.34-bin.jar"); System.err.println("driverclass=com.mysql.jdbc.Driver"); System.err.println("url=jdbc:mysql://127.0.0.1:3306/_______"); System.err.println("sqlquery=SELECT * FROM ________"); System.err.println("useUnbufferedInput=false"); System.exit(1); } String filename = args[0]; int fs = Integer.MIN_VALUE; //mysql streaming trick int fd = ResultSet.FETCH_FORWARD; int rt = ResultSet.TYPE_FORWARD_ONLY; int rc = ResultSet.CONCUR_READ_ONLY; int rh = ResultSet.CLOSE_CURSORS_AT_COMMIT; Properties props = new Properties(); props.load(new FileInputStream(filename)); props.store(new FileOutputStream(FileDescriptor.err), "config file: "+filename); System.err.println("------------"); String path = (String)props.remove("path"); String url = (String)props.remove("url"); String driverclass = (String)props.remove("driverclass"); String sql = (String)props.remove("sqlquery"); props.store(new FileOutputStream(FileDescriptor.out), "actual parameters"); System.out.println("------------"); Connection conn = null; try { File f = new File(path.replace('/', File.separatorChar)); System.out.println(f + " : " + f.exists()); URLClassLoader uc = new URLClassLoader(new URL[]{f.toURL()}); Driver d = (Driver)uc.loadClass(driverclass).newInstance(); conn = d.connect(url, props); queryIt(conn, rt, rc, rh, fs, fd, sql); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } } static void queryIt(Connection conn, int rstype, int rsconc, int rshold, int fs, int fd, String sql) throws Exception { conn.setAutoCommit(false); System.out.println("Driver version = "+conn.getMetaData().getDriverVersion()); Statement st = conn.createStatement(rstype, rsconc, rshold); try { //st.setMaxRows(100); st.setFetchDirection(fd); st.setFetchSize(fs); System.out.println(sql); ResultSet rs = st.executeQuery(sql); try { StringBuilder sb = new StringBuilder(); int cnt = 0; while(rs.next()) { cnt++; if(cnt%100==0) { System.out.print("."); if(cnt%10000==0) System.out.println(cnt+" rows."); } Thread.sleep(5); } System.out.println(cnt+" rows."); } catch(Exception e) { e.printStackTrace(); } finally { rs.close(); } } catch(Exception e) { e.printStackTrace(); } finally { st.close(); } } }