2014-12-21 4 views
0

여러 쿼리를 실행하려고하지만 일부 쿼리 만 실행됩니다. 여기 데이터 원본 연결을 사용하여 여러 SQL 쿼리를 실행할 수 없습니다.

내 코드입니다 :

public class PostPrReqDaoImpl implements PostPrReqDaoInterface { 

    @Override 
    public boolean validate(PostPrReqBean pprb1,PostPrReqBean pprb2,PostPrReqBean pprb3,PostPrReqBean pprb4) { 
     System.out.println("Inside PostPrReq Dao"); 
     int resstat=1; 
     //snuname,snuusrname,snupass,snuempid,snuemail,snudob,snuskill,snuexp,snudesg; 
     java.io.PrintWriter out = null; 
     try { 
      out = pprb1.getBeanresponse().getWriter(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     pprb1.getBeanresponse().setContentType("text/html"); 



     try { 
      /* get the DataSource from using the JNDI name */ 
      Class.forName("com.mysql.jdbc.Driver"); 
      InitialContext ctx = new InitialContext(); 
      DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Test"); 
      //username, pass,name, id,dob ,skillset, exp,designation ,email 
      //Create connection and then continue as usual other JDBC calls 
      Connection conn=ds.getConnection(); 
      System.out.println("Connection succesful!"); 
      Statement s1= conn.createStatement(); 
      Statement s2= conn.createStatement(); 
      Statement s3= conn.createStatement(); 
      Statement s4= conn.createStatement(); 

      if(resstat == 1) 
      { 

       String sqlproj = "INSERT INTO tblproject (name,code,location,type,lastdate) " + 
         "VALUES ('" + pprb1.getBeanprojname() + "' , " 
         + "'" + pprb1.getBeanprojcode() +"' ," 
         + "'" + pprb1.getBeanprojlocation() +"' ," 
         + "'" + pprb1.getBeanprojtype() +"' ," 
         + "'" + pprb1.getBeanprojlastdate() +"')"; 

       String sqlreq1="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" + 
         "VALUES ('" + pprb1.getBeanprojcode() + "' , " 
         + "'" + pprb2.getBeandesg() +"' ," 
         + "'" + pprb2.getBeanskill() +"' ," 
         + "'" + pprb2.getBeanexp() +"' ," 
         + "'" + pprb2.getBeanncand() +"')"; 

       String sqlreq2="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" + 
         "VALUES ('" + pprb1.getBeanprojcode() + "' , " 
         + "'" + pprb3.getBeandesg() +"' ," 
         + "'" + pprb3.getBeanskill() +"' ," 
         + "'" + pprb3.getBeanexp() +"' ," 
         + "'" + pprb3.getBeanncand() +"')"; 

       String sqlreq3="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" + 
         "VALUES ('" + pprb1.getBeanprojcode() + "' , " 
         + "'" + pprb4.getBeandesg() +"' ," 
         + "'" + pprb4.getBeanskill() +"' ," 
         + "'" + pprb4.getBeanexp() +"' ," 
         + "'" + pprb4.getBeanncand() +"')"; 


       s1.executeUpdate(sqlproj); 
       System.out.println("Proj updation successful!"); 
       s2.executeUpdate(sqlreq1); 
       System.out.println("Req1 updation successful!"); 
       s3.executeUpdate(sqlreq2); 
       System.out.println("Req2 updation successful!"); 
       s4.executeUpdate(sqlreq3); 
       System.out.println("Req3 updation successful!"); 
      } 

     } catch (Exception e){ 

      out.println("Failed!"+ e); 
      resstat=0; 
     } 

     if(resstat==1) 
      return true; 
     else 
      return false; 
    } 

} 

만 1이 쿼리를 실행하세요 (예 : 콘솔 오/피입니다 Proj를의 그 갱신에 성공 Req1의 그 갱신에 성공! "!)

여기

내입니다 이 명 테이블 구조

Table tblproject: 

Field  Type   Null  Key 
----------------------------------------- 
name  varchar(100) YES   
code  varchar(100) NO  PRI   
location varchar(100) YES   
type  varchar(100) YES   
lastdate varchar(11)  YES  

Table tblrequirements: 

Field  Type   Null  Key 
----------------------------------------- 
code   varchar(100) NO   
designation varchar(100) YES   
skillset  varchar(100) YES   
exp   int(11)   NO   
ncand  int(11)   NO   
slno   varchar(45)  NO  PRI  

는 어디서 잘못된 것입니까?

+0

오류 메시지가 나타 났습니까? –

+0

스택 추적에 오류가 전혀없고 예외가 없습니다! –

+0

명령문 실행간에 커밋을 사용해보십시오. 당신은 막혀있을 수 있습니다. –

답변

0

여러 insert 문을 일괄 처리로 실행하십시오. 아래 주어진 예제

 PreparedStatement pstmt = conn.prepareStatement("INSERT INTO .... VALUES(?,?)"); 

     pstmt.setString(1, "some value 1"); 
     pstmt.setString(2, "other value 1"); 
     pstmt.addBatch(); 

     pstmt.setString(1, "some value 2"); 
     pstmt.setString(2, "other value 2"); 
     pstmt.addBatch(); 

     pstmt.setString(1, "some value 3"); 
     pstmt.setString(2, "other value 3"); 
     pstmt.addBatch(); 

     pstmt.executeBatch(); 

코드에서 SQL 문은 아무 이유없이 여러 번 평가됩니다. 또한 정말로 읽기 쉽고 단순하지도 않습니다.

관련 문제