2016-05-05 4 views
0

Dies ist mein DAO. Es besteht aus public static method for presentationBean, public static method for lecturerBean und zuletzt public static PresentationBean addavailable2(PresentationBean ADbean) für den Zugriff auf die Datenbank.Wert kann nicht in eine andere öffentliche statische Methode (Java) übergeben werden

public class AddAvailableDAO2 { 

     static Connection currentCon = null; 
     static ResultSet rs = null; 


     public static PresentationBean getPresentation(String id) throws SQLException, ClassNotFoundException 
     { 
      currentCon = JavaConnectionDB.getConnection() ; 
      PreparedStatement ps = currentCon.prepareStatement("SELECT * FROM presentation WHERE presentationid = ?") ; 
      ps.setString(1, id) ; 
      PresentationBean pb = new PresentationBean() ; 
      ResultSet rs = ps.executeQuery() ; 
      while(rs.next()) 
      { 
       pb.setPresentationID(rs.getString(1)) ; 
       pb.setPresentationDay(rs.getString(2)) ; 
       pb.setPresentationStart(rs.getDate(3)) ; 
       pb.setPresentationEnd(rs.getDate(4)) ; 
      } 

      return pb ; 
     } 
     public static LecturerBean getLecturer(String lectID) throws SQLException, ClassNotFoundException 
     { 
      currentCon = JavaConnectionDB.getConnection() ; 
      PreparedStatement ps1 = currentCon.prepareStatement("SELECT * FROM lecturer WHERE lecturerid = ?") ; 
      ps1.setString(1, lectID) ; 
      LecturerBean lb = new LecturerBean() ; 
      ResultSet rs1 = ps1.executeQuery() ; 
      while(rs1.next()) 
      { 
       lb.setLecturerID(rs1.getString(1)) ; 

      } 

      return lb ; 
     } 
     public static PresentationBean addavailable2(PresentationBean ADbean) { 

      System.out.println("JIJIJI"); 

      AvailabilityBean available = new AvailabilityBean(); 

      String availableID = ADbean.getPresentationID(); 
      String availableDay = ADbean.getPresentationDay(); 
      String availID = available.getAvailableID(); 

      LecturerBean lb = new LecturerBean(); 
      String lecturerID = lb.getLecturerID(); 

     try{ 

       currentCon = JavaConnectionDB.getConnection(); 


        SimpleDateFormat date1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
        Date availStart = ADbean.getPresentationStart(); 
        Date availEnd = ADbean.getPresentationEnd(); 

        String avs = date1.format(availStart); 
        String ave = date1.format(availEnd); 

        String start = "02-05-2016 " + avs.substring(11); 
        String end = "02-05-2016 " + ave.substring(11); 



        Date StartTime = date1.parse(start); 
        Date EndTime = date1.parse(end); 

        java.util.Date availableStart = new java.util.Date(); 
        java.sql.Date avStart = new java.sql.Date(StartTime.getTime()); 
        java.util.Date availableEnd = new java.util.Date(); 
        java.sql.Date avEnd = new java.sql.Date(EndTime.getTime()); 

       PreparedStatement ps=currentCon.prepareStatement("Insert into free (freeID,lecturerID,availableID) select free_seq.nextval,?,availableID from availability where availableday=? AND availableStart=?"); 

       ps.setString(1,lecturerID); //PASS THE VALUE TO HERE 
       ps.setString(2,availableDay); 
       ps.setDate(3, avStart); 


       ps.executeUpdate(); 


      } 

     catch(Exception e){ 
      System.out.println("add availability failed 2: An Exception has occurred! " + e); 
      e.printStackTrace() ; 

       System.out.println("Your availability Day is " + availableDay); 



     } 

    return ADbean; 
    } 

Mein Problem ist, ich nicht den Wert von lecturerID von public static method for lecturerBean zu public static PresentationBean addavailable2(PresentationBean ADbean) weil lecturerID in PresentationBean existiert nicht passieren kann. Ich bin mir dessen bewusst. Wie kann ich die lecturerID von public static method for lecturerBean zu public static PresentationBean addavailable2(PresentationBean ADbean) übergeben. Ich habe versucht, LecturerBean in public static PresentationBean addavailable2(PresentationBean ADbean) neu zu deklarieren, aber ich bekomme immer noch null Wert.

+2

Es ist nicht notwendig, den gesamten Methodenkopf anzugeben. Methodennamen sind in Ordnung. – DarkV1

+1

Deklarieren Sie es als eine Klassenvariable – DarkV1

+1

Es sieht aus wie 'Addavailable2' benötigt eine Dozenten ID, und Sie geben es nicht eine. Die naheliegende Lösung besteht also darin, einen zu geben, indem der Methode ein weiterer Parameter hinzugefügt wird. – ajb

Antwort

1

Pass sowohl adBean und lecturerBean zu Verfahren addavaialable2 heißt

public static PresentationBean addavailable2(final PresentationBean adBean, final LectureBean lecturerBean) { 

     AvailabilityBean available = new AvailabilityBean(); 

     String availableID = adBean.getPresentationID(); 
     String availableDay = adBean.getPresentationDay(); 
     String lecturerID = lecturerBean.getLecturerID(); 
     String availID = available.getAvailableID(); 
     ....... 

In Ihrem Anrufer im Gegensatz zu, bevor Sie die folgenden

//Get input presentation bean, using your desired id 
PresentationBean presentationBean = AddAvailableDAO2.getPresentation("123"); 
//Get input lecturer bean, using your desired id 
LecturerBean lecturerBean = AddAvailableDAO2.getLecturer("456"); 
//Add availability 
AddAvailableDAO2.addavailable2(presentationBean, lecturerBean); 
... 

Ich glaube, Sie hier auf die Idee kommen ausführen müssen. PresentationBean und LecturerBean stellen zwei verschiedene Entitäten/Objekte dar. Wenn Sie also einen Vortragenden einer Präsentation zuordnen möchten, benötigen Sie Zugriff auf beide Entitäten. Hoffnung, die Sinn macht.