Post
KO

삽질 2 ㅡㅡ

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

 package ext;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.*;

public class dbConnection {

 static Connection conn;

 static Statement stmt;

 static ResultSet rs;

 public static void main(String[] args) throws Exception {

  String inputPath =”d:\temp\”;

  File file = new File(inputPath);

  File [] fileName = file.listFiles(); // 파일 목록 가져와

  int maxNum=758;

  int dateCnt=0;

  int j=1;

  try{

   Class.forName(“oracle.jdbc.driver.OracleDriver”);

   System.out.println(“로딩 성공”);

   List list = getInfoList();

   System.out.println(“리스트 사이즈는? = ” +list.size());

   List list2 = getDate();

   //System.out.println(“날짜는 몇개? = ”+ list2.size());

   for(int i=0; i<list.size();i++){

     //list.get(i).put(“newNum”, maxNum);

     //list.get(i).put(“newOpenDate”,list2.get(dateCnt));

    //if(fileName[i].getName().indexOf(list.get(i).get(“openDate”).toString())>0){

     String name = fileName[i].getName();

     System.out.println(“—– ori = ”+fileName[i].getName()+” —>”+ list2.get(dateCnt).toString()+

       name.substring(name.indexOf(“_”),name.length()));

     fileCopy(inputPath+name,inputPath+list2.get(dateCnt).toString()+

       name.substring(name.indexOf(“_”),name.length()));

    //}

     if(j%3==0){

      dateCnt++;

      maxNum++;

     }

    j++;

    //System.out.println(fileName[i].getName().indexOf(list.get(i).get(“openDate”).toString()));

    //System.out.println( list.get(i).get(“num”) +”, ”+ list.get(i).get(“openDate”) +”, ” + list.get(i).get(“div”) );

    //int temp = insertData(list.get(i));

    /*

    if(temp==0){

     break;

    }

    */

   }

  }catch(Exception e){

   System.out.println(“로딩 실패”+ e);

   return;

  }

 }

 public static List getInfoList(){

  List list = new ArrayList();

  HashMap map = null;

  try{

   conn = DriverManager.getConnection(“odbc:oracle:thin:@...**:1521:”,””,”**”);

   }catch(Exception e){

    e.printStackTrace();

    System.out.println(“접속 실패”);

   }

  try{

   stmt = conn.createStatement();

   StringBuffer sb = new StringBuffer(“ SELECT   to_char(open_date,’yyyyMMdd’) as openDate   \n”)

        .append(“FROM cel.reuters_info \n”)

        .append(“WHERE open_date between to_date(‘2011-12-31’) and  to_date(‘2012-04-30’) \n”)

        .append(“ORDER BY  open_date,div \n”);

   System.out.println(sb.toString());

   rs = stmt.executeQuery(sb.toString());

   if(rs!=null && rs.next()){

    do{

     map = new HashMap();

     //map.put(“num”, rs.getInt(“num”));

     //map.put(“div”, rs.getString(“div”));

     //map.put(“subType”, rs.getString(“sub_type”));

     //map.put(“title”, rs.getString(“title”));

     //map.put(“readCnt”, rs.getString(“read_cnt”));

     //map.put(“titleKr”, rs.getString(“title_kr”));

     //map.put(“category”, rs.getString(“category”));

     map.put(“openDate”, rs.getString(“openDate”));

     //map.put(“openYn”, rs.getString(“open_yn”));

     //map.put(“marker”, rs.getString(“marker”));

     //map.put(“playTime”, rs.getString(“play_time”));

     list.add(map);

    }while(rs.next());

   }

  }catch(Exception e){

   e.printStackTrace();

   System.out.println(“리스트 담다가 에러 터짐”);

  }finally{

   try {

    conn.close();

   } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

   }

  }

  System.out.println(“성공!”);

  return list;

 }

 public static int insertData(HashMap map) throws Exception{

  conn = DriverManager.getConnection(“odbc:oracle:thin:@.169..:1521:”,””,”**”);

  int result=0;

  try{

   stmt = conn.createStatement();

   String sql = ”insert into reuters_info ” +

      ” select ’“+map.get(“newNum”)+”‘,div,sub_type,title,0,’’,’’,’“+map.get(“newOpenDate”)+”‘,open_yn,marker,play_time from reuters_info ”+

                        ” where num = ’“+map.get(“num”)+”’“ +

      ” and div = ’“+map.get(“div”)+”’”;

     //+”(‘“+map.get(“num”) +”’,’“+map.get(“div”)+”’,’“+map.get(“subType”)+”’,’“+map.get(“title”)

    +”’,’0’,’“+map.get(“titleKr”)+”’,’“+map.get(“category”)+”’,’“+map.get(“openDate”)

    +”’,’“+map.get(“openYn”)+”’,’“+map.get(“marker”)+”’,’“+map.get(“playTime”)+”’)”;

   System.out.println(sql);

   result = stmt.executeUpdate(sql);

  }catch(Exception e) {

   e.printStackTrace();

   System.out.println(“ 입력하다가 실패 했다.”);

  }

  return result;

 }

 public static List getDate(){

        // 1     2     3     4     5     6     7

      final String[] week = { ”일”, ”월”, ”화”, ”수”, ”목”, ”금”, ”토” };

         SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMdd”);

         BufferedWriter out = null;

         String currentTime = sdf.format(new java.util.Date());

          Calendar c = Calendar.getInstance(TimeZone.getTimeZone(“Asia/Seoul”));

          String fromDate = sdf.format(c.getTime());

          c.set(2012, 11,31);

         int valueCount = 0;

         List list= new ArrayList();

         try{

          FileWriter fw = new FileWriter(new File(“d:\output.txt”));

         for(int i = 0 ; i < 1000 ; i++){

           c.add(Calendar.DATE, 1);

           if(c.get(Calendar.DAY_OF_WEEK)==7     c.get(Calendar.DAY_OF_WEEK)==1){

            }else{

            valueCount++;

            if(valueCount>85){

             break;

             }else{

                    list.add(sdf.format(c.getTime()));

                   //System.out.println(“해당 날짜 : ” + (c.get(c.DATE)));

                      //System.out.println(“Calendar 주말 제외 날짜 : ” + toDate + ” 카운트 : ” + valueCount);

                      //fw.write(toDate);

                      //fw.write(“\r\n”);

            }

           }

         }

         fw.close();

         }catch(IOException e){

          e.printStackTrace();

           System.exit(1);

         }finally{

    // 5. 입출력 스트림 닫기

   }

          //String toDate = sdf.format(c.getTime());

          ///System.out.println(“Calendar 이용 (연산 후) : ” + toDate);

          return list;

     }

   //파일을 복사하는 메소드

   public static void fileCopy(String inFileName, String outFileName) {

    try {

     FileInputStream fis = new FileInputStream(inFileName);

     FileOutputStream fos = new FileOutputStream(outFileName);

     System.out.println(“====” + inFileName + ”====” + outFileName);

     byte[] buffer = new byte[1024*2];

     int dataSize = 0;

     while((dataSize=fis.read(buffer))!=-1) {

      fos.write(buffer,0,dataSize);

     }

     fis.close();

     fos.close();

    } catch (IOException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

    }

   }

}

Colored by Color Scripter cs

This article is licensed under CC BY 4.0 by the author.