这是我的DAO。它由public static method for presentationBean
,public 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;
}
我的问题是,我无法将讲师ID的值从
public static method for lecturerBean
传递给public static PresentationBean addavailable2(PresentationBean ADbean)
,因为lecturerID
在PresentationBean
中不存在。我知道这一点。因此,如何将
lecturerID
从public static method for lecturerBean
传递到public static PresentationBean addavailable2(PresentationBean ADbean)
。我试图在LecturerBean
中声明新的public static PresentationBean addavailable2(PresentationBean ADbean)
,但仍然得到空值。 最佳答案
将adBean和讲者Bean都传递给方法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代表两个不同的实体/对象,因此,如果要将讲师与演示文稿关联,则需要访问这两个实体。希望有道理。