이 기사에서는 데이터베이스에서 다량의 데이터 레코드를 내보내고 파일에 저장하는 Java 구현 방법에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
데이터베이스 스크립트 :
- 테이블 "t_test"ddlcreate 테이블`t_test` (`id` int (11) NOT NULL AUTO_INCREMENT,`TITLE` varchar (255) 기본 널,`CreateTime` bigint (20) 기본 널, primary 키 (`id` )) 엔진 = innodb 기본 charset = utf8;
암호:
com.yanec.test java.io.fileoutputstream; Java.sql.sql.sql.sql.resultset; (string [] args) {test (); // exp (); } / *** 내보내기 데이터* / public static void exp () {connection conn = null; "com.mysql.jdbc.driver") ://127.0.0.1:3306/testcms?characterencoding=gbk "; String jdbcusername ="rit "; String jdbcpassword ="root "; conn = drivermanager.getConnection (j dbcurl, jdbcusername, jdbcporsword); system.out.out.println "Conn +Conn); Sqlexception e) {instantiationException e) {// todo a auto-regended catch (); 불법 행사 e) {// TODE 자동 생성 된 캐치 블록 e.printstacktrace (); } catch (sqlexception e) {// todo auto-reconeated catch block e.printstacktrace ()} public static void exp (int startid) {connection conn = null ( "class.forname .Driver ")) .NewInstance (); String JDBCURL ="JDBC : MySQL : //127.0.0.1 : 3306/testCMS? charac getConnection (JDBCURL, jdbcusername). {// TODO 생성 캐치 블록 E.PrintStackTrace ()} Catch (// TODO 자동 생성 된 캐치 블록 E.PrintstackTrace () {ClassNo TfoundException CATCH E.PrintStackTrace (); startId * @param start_id */ public static void exp (int counter = 0; = "ID asc asc Limit 50에 의한 t_test>" + STIME.PRINTLN에서 선택하십시오 rs = sql (rs.next); 카운터 ++; writecontent (c ounter+"-"+id+"-"+title+"/r/n", "/opt/"log.txt" atrue); println ( "i ="+cound+"-"+id+"-"+title); ();} writeContent ( "+startId,"/opt/".txt",}* @param conn*/public static void exp (Connection Conn); ) {int counter = 0; T_TEST에서 CreateTime> " + Timestamp +"Limit 50 "; System.out.println ("SQL === " + SQL); try {state stmt = conn.createstatement (); resultSet rs = stmt.executeQuery (s ql while (rs.next ()) {flag = rs.getint ( "id"); TimeStamp = la stmodifytime; )} catch (sqlexception e) {e.printstacktrace ()} connection conn = null ( "com.mysql.jdbc.driver"). str ing jdbcurl = "mysql : //127.0.1 : 3306? gbk"; jdbcpassword); System.out.println (int i = 1; i <= 10000; }} catch (sqlexception e) {e .printstacktrace (instantiationException e) {// todo a autocessexception e.printstacktrace ()} catch (classnotfoundException e) {// todo auto-rected catch e.printstacktrace (); 자동 생성 된 캐치 블록 E.printstacktrace ()} 공개 정적 void Add (Connection Conn, String Title) {pstmt = null; ; ret = pstmt.executeupdate ()} catch (sqlexception e) {// todo auto-rected catch e.printstacktrace (); 자동 생성 캐치 B Bloc K E.PrintStackTrace ()}}}} * 파일에 컨텐츠를 작성하십시오 * @param filename * @return * / public static boolean writecontent (문자열 c, String dirname, String filename , boolean i sappend) {file f = new 파일 (dirname); outputStreamwr writer (fos). } /** * Read content from file* * @param : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : filename * @return */ public static String readText (string filename) {string content = ""; if (filename) {fi lereader fr = 새 filereader br = new bufferedReader (fr); String str = ""; fr.close ()} catch (ioexception e) {e.printstacktrace}};기본 아이디어 : 많은 양의 데이터로 인해 SQL을 여러 번 실행해야합니다. 그렇지 않으면 메모리와 오류가 충분하지 않습니다.
주요 용도 : 인터페이스 개발 중에 데이터 증분 출력이 제 3 자에게 제공되는 시나리오에서 사용할 수 있습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.