值无法传递到另一个公共静态方法(java)

时间:2016-05-05 04:23:18

标签: java static

这是我的DAO。它由public static method for presentationBeanpublic static method for lecturerBean和最后public static PresentationBean addavailable2(PresentationBean ADbean)组成,用于访问数据库。

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;
    }

我的问题是,我无法将讲义的值从public static method for lecturerBean传递到public static PresentationBean addavailable2(PresentationBean ADbean),因为lecturerID中不存在PresentationBean。我知道这一点。

那么我怎样才能将lecturerIDpublic static method for lecturerBean传递给public static PresentationBean addavailable2(PresentationBean ADbean)。我尝试在LecturerBean中声明新public static PresentationBean addavailable2(PresentationBean ADbean),但我仍然获得空值。

1 个答案:

答案 0 :(得分:1)

将adBean和lecturerBean传递给方法addavaialable2,即。

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();
        .......

在您的来电者中,您需要执行以下操作

//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);
...

我想你在这里得到了这个想法。 PresentationBean和LecturerBean表示两个不同的实体/对象,因此如果要将讲师与演示文稿关联,则需要访问这两个实体。希望有道理。