우편번호 zipcode.csv 로 zipcode.sql 파일 생성
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/*
* Created on 2004. 1. 9.
* ㅡㅡ; 잠깐 맹글어본겁니다.. 우편번호 입력할라구...
* 자바하는게 좋습니다... 뛰어나진 못하더라도... ㅋㅋ
*/
/**
* @author KimTaeBum
*
* mysql 입력방법.
* D:\mysql\bin>mysql -uuser -ppassword DBName < D:\webapps\test\cl_zipcodet.sql
*/
//create table cl_zipcodet (
// zipcode varchar(7) not null default '', /* 우편번호 */
// sido varchar(4) not null default '', /* 특별시,광역시,도 */
// gugun varchar(13) not null default '', /* 시,군,구 */
// dong varchar(43) not null default '', /* 읍,면,동,리,건물명 */
// bunji varchar(17) not null default '', /* 번지,아파트동,호수 */
// seq varchar(5) not null default '', /* 데이터 순서 */
//)
public class ParseZipCode {
public static void main(String[] args) {
String zipCodeFileSrc = "D:\\webapps\\test\\zipcode.csv";
String outputZipCode = "D:\\webapps\\test\\cl_zipcodet.sql";
System.out.println(" zipCodeFileSrc >> ["+zipCodeFileSrc+"]");
try {
BufferedReader is = new BufferedReader(new FileReader(zipCodeFileSrc));
String line = null;
line = is.readLine();
System.out.println(" line >> ["+line+"]");
StringTokenizer st = null;
StringBuffer sb = null;
int index = 0;
int countToken = 0;
int cnt = 0;
while ( true ) {
// cnt++;
index=1;
line=is.readLine();
if (line==null || line.equals("") || cnt>=100) {
break;
} else {
// System.out.println(" ("+index+")line >> ["+line+"]");
st = new StringTokenizer(line, ",");
sb = new StringBuffer();
sb.append("insert into cl_zipcode values ( ");
countToken = st.countTokens();
System.out.print( countToken );
while (st.hasMoreTokens()) {
//System.out.print( index );
if (index>1){ sb.append(","); }
if ( countToken==index && index==5 ) {
sb.append("'',");
}
sb.append("'").append(st.nextToken()).append("'");
index++;
}
sb.append(" );");
PrintWriter pw = null;
try{
pw = new PrintWriter(new FileWriter(outputZipCode, true), false);
pw.println( sb.toString() );
pw.flush();
} finally {
if ( pw != null) try{pw.close();}catch(Exception e){}
}
System.out.println( sb.toString() );
}
}
System.out.println(" END!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}