2012-09-13 3 views
0

QueryRunner 클래스를 사용하여 mysql 데이터베이스에서 레코드를 삭제하는 방법은 무엇입니까? ID는 String의 배열로서 건네받습니다. 또한 apache를 사용하면 DML 작업에 공통 db utils를 사용하는 것이 좋습니다. 대안이나 모범 사례는 무엇입니까?QueryRunner 클래스를 사용하여 레코드를 삭제하는 방법

아래는 내 StudentDAO.java 클래스에서 추출한 것입니다.

  public boolean deleteStudent(String [] ids) { 

      Connection connection = null; 
      String query; 
      boolean result = false; 

      try { 
       Context initCtx = new InitialContext(); 
       Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
       DataSource ds = (DataSource) envCtx.lookup("jdbc/cmsDB"); 
       connection = ds.getConnection(); 

       QueryRunner run = new QueryRunner(ds); 
       query = "delete tbl_student where student_id";// what should i put here??? 
       int nor = run.update(query, ids); //nor = no of records 

       if (nor > 0) { 
        result = true; 
       } else { 
        result = false; 
       } 
      } catch (NumberFormatException e) { 
       // e.getMessage(); 
       e.printStackTrace(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       DbUtils.closeQuietly(connection); 
      } 
      return result; 
     } 

답변

0
public static String join(String[] s, String delimiter) { 
    int imax = s.length; 
    String strJoin=""; 
    if (imax > 1) { 
     int i = 0; 
     for (i = 0; i < imax-1; i++) { 
      strJoin += s[i] + delimiter; 

     } 
     strJoin += s[i];    
    } 
    else 
     strJoin += s[0]; 
    return strJoin; 
} 

public boolean deleteStudent(String [] ids) { 
    ... 
    ... 
    ... 
    query = "delete tbl_student where student_id in ("+ join(ids,",") +")"; 
    ... 
    ... 
    ... 
] 
관련 문제