Programming/Java / Jsp2017. 4. 13. 12:29


import java.io.BufferedReader;

import java.io.File;

import java.io.FileInuptStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;


public class readFile {


public void run() thorws Exception {

BufferedReader br = null;

Connection con = null;

PreparedStatement stmt = null;

try {

int count = 0;

File file = new file("/file/test.txt");

br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-                                                                                                                                 8"));

//br = new BufferedReader( new FileReader(file) );

String line = null;

StringBuffer sbDel = new StringBuffer();

sbDel.append("DELETE FROM TABLE                 ");

sbDel.append(" WHERE BASE_YMD = TRUNC(SYSDATE)  ");

con = DBUtil.getConnection();

con.setAutoCommit(false);

stmt = con.prepareStatement(sbDel.toString());

stmt.executeUpdate();

stmt.clearParameters();

StringBuffer sb = new StringBuffer();

sb.append("INSERT INTO TABLE (   ");

sb.append("       BASE_YMD       ");

sb.append("     , RECV_DATA      ");

sb.append(") VALUES (            ");

sb.append("       TRUNC(SYSDATE) ");

sb.append("     , ?              ");

sb.append(")                     ");

stmt = con.prepareStatement(sb.toString());

while( (line = br.readLine()) != null ){

count++;

String recvData = line;

stmt.setString(1, recvData);

stmt.executeUpdate();

stmt.clearParameters();

}

String rtn_code = null;

String trn_message = null;

CallableStatement cs = con.prepareCall("{ call P_TABLE_INSERT(?,?)}");

cs.registerOutParameter(1,java.sql.Types.VARCHAR);

cs.registerOutParameter(2,java.sql.Types.VARCHAR);

cs.execute();

rtn_code = cs.getString(1);

rtn_message = cs.getString(2);

System.out.println("\n rtn_code : " + rtn_code);

System.out.println("\n rtn_message : " + rtn_message);

if( cs != null) {

cs.close();

cs = null;

}

if("FAILURE!".equals(rtn_code)){

throw new Exception(rtn_message);

}

con.commit();

System.out.println("\n DATA INSERT "+count+"건");


} catch (Exception e){

e.printStackTrace();

System.out.println("exception 발생");

} finally {

if(br != null)   try{br.close();}   catch (IOException e) {}

if(stmt != null) try{stmt.close();} catch (SQLException e) {}

if(con != null)  try{con.close();}  catch (SQLException e) {}

}

}


}

'Programming > Java / Jsp' 카테고리의 다른 글

java/jsp 기타  (0) 2017.04.17
java 메일발송  (0) 2017.04.17
Posted by jvvj