Java-无法从检索到的序列化对象获取值

Java-无法从检索到的序列化对象获取值,java,oracle,io,oracle11g,Java,Oracle,Io,Oracle11g,到目前为止,这段代码给我带来了很多麻烦,但它确实是我编写的第一个使用preparedStatement、Oracle和序列化的类。所以我请求你的耐心。 代码如下: public static boolean storeInfo(Reservation rsv) throws IOException, SQLException{ try { //Connection to DB Connection con = null;

到目前为止,这段代码给我带来了很多麻烦,但它确实是我编写的第一个使用preparedStatement、Oracle和序列化的类。所以我请求你的耐心。 代码如下:

 public static boolean storeInfo(Reservation rsv) throws
        IOException, SQLException{
    try {
          //Connection to DB
          Connection con = null;
          Class.forName("oracle.jdbc.driver.OracleDriver");
          con=DriverManager.getConnection(
            "jdbc:oracle:thin:@odsdsd",
            "SMBDB",
            "hpdbsmb");
          con.setAutoCommit(false);
          ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
          ObjectOutputStream out = new ObjectOutputStream(bos);
          out.writeObject(rsv);
          out.flush();
          out.close();
          byte[] buf = bos.toByteArray();
          //inserting into database
          PreparedStatement prepareStatement = con.prepareStatement("INSERT INTO 
           SMD_RESERVATION_INSTANCES VALUES(?,?,?)");
          localIDTest = getUniqueID();
          System.out.println("ID: "+localIDTest);
          prepareStatement.setString(1, localIDTest);
          prepareStatement.setBytes(2, buf);
          prepareStatement.setString(3, "Pending");
          prepareStatement.executeUpdate();
          prepareStatement.close();
          con.commit();
   }catch(SQLException sqle){
       System.err.print(sqle);
   }
   catch(ClassNotFoundException cnfe){
       System.err.print(cnfe);
   }
    return false;
   }


   public static Reservation retrieveReservation()throws IOException,
        SQLException{
   Reservation testRsv = new Reservation();
   try {
        Connection con = null;

        Class.forName("oracle.jdbc.driver.OracleDriver");
        con=DriverManager.getConnection(
            "jdbc:oracle:thin:@dsdsds",
            "SMBDB",
            "hpdbsmb");
        con.setAutoCommit(false);
       try {

            PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM 
             SMD_RESERVATION_INSTANCES WHERE id = ?");         
            prepareStatement.setString(1, localIDTest);
            ResultSet rset = prepareStatement.executeQuery();
            prepareStatement.executeUpdate();
            //prepareStatement.close();
            con.commit();
            if(rset.next()){
                retrievedID = rset.getString("ID");
                Blob blob = rset.getBlob("RESERVATIONINST");
                status = rset.getString("STATUS");
                long blobLength = blob.length();
                int pos = 1;   // position is 1-based
                //int len = 10;
                byte[] bytes = blob.getBytes(pos,(int) blobLength);
                InputStream is = blob.getBinaryStream();
                ObjectInputStream ois = new ObjectInputStream(is);
                testRsv=(Reservation)ois.readObject();
            }


        rset.close();
        con.close();
         }catch(IOException ioe){
              System.err.print(ioe);
          }
       }catch(ClassNotFoundException cnfe){
          System.err.print(cnfe);
      }
    return testRsv;

   }

   public static void displayRsvContent(){

    try{
        Reservation rsvtester = new Reservation();
        rsvtester.badgeNo = 750752;
        rsvtester.networkID = "GHAMKS1C";
        storeInfo(rsvtester);
        rsvReturned = retrieveReservation();
        if(rsvReturned == null){
            System.out.println("Null Reservation!");
        }else{
            System.out.println("Badge: " + rsvReturned.badgeNo);
            System.out.println("Network: " + rsvReturned.networkID);
        }

    }catch(SQLException sqle){
            System.err.print(sqle);
    }catch(IOException ioe){
        System.err.print(ioe);
    }
  }


public static void main(String[]args){
    displayRsvContent();
}
这只是测试存储和检索对象是否正确。我创建了一个Reservation实例,并分配了它的变量值,以查看它们是否被实际保存。令人惊讶的是,他们在装货时没有出现。这就是我得到的:

ID: ec561507-7138-4468-98a3-7756219f216e
Badge: 0
Network: null
为什么会发生这种情况,即使我只是在存储对象之前分配它?但id显示的是正确的

另外,预订:

public class Reservation implements
   java.io.Serializable{

String eventTitle;
public Date startDate;
public Date endDate;
String requestType;
public int terminals;
String lastName;
String firstName;
String middleInitials;
public transient int badgeNo;
public transient String networkID;
String telephoneNo;
String orgCode;
String orgName;
String justification;
String insideCheckRange;
int mapSize;



MapStorage mapStorage = new MapStorage();


public Reservation(int badgeNo, String networkID) {
    this.badgeNo = badgeNo;
    this.networkID = networkID;
}


    public Reservation(){

    }

public Reservation(String eventTitle, Date startDate, Date endDate, String requestType, int terminals, String lastName, String firstName, String middleInitials, int badgeNo, String networkID, String telephoneNo, String orgCode, String justification) {
    //create in here

    this.eventTitle = eventTitle;
    this.startDate = startDate;
    this.endDate = endDate;
    this.requestType = requestType;
    this.terminals = terminals;
    this.lastName = lastName;
    this.firstName = firstName;
    this.middleInitials = middleInitials;
    this.badgeNo = badgeNo;
    this.networkID = networkID;
    this.telephoneNo = telephoneNo;
    this.orgCode = orgCode;
    this.justification = justification;
}



 public boolean checkRange() {    



        DateTime startx = new DateTime(startDate.getTime());
        DateTime endx = new DateTime(endDate.getTime());

        //booking status
        boolean possible = false;

        //Booking type: 1 = Project, 2 = Training

        /*
        if(requestType.equals("Project")){
            bookingType = 1;
        }else if(requestType.equals("Training")){
            bookingType = 2;

        }
        */
        //produces submap
        //mapSize = bookingType;
        TreeMap<DateTime, Integer> mapLoaded = null;
        try{
         mapLoaded = mapStorage.RetrieveMap();
        }catch(IOException ioe)
        {
            System.err.print(ioe);
        }
        if(requestType.equals("Project"))
        {
            //Project
            //insideCheckRange = "In first for loop";
            //fetch all values for keys in the map between start and end
                for (Integer capacity : mapLoaded.subMap(startx, endx).values()) {


                    if(capacity >= terminals)
                        //yes then its possible, set to true
                        possible = true;
                    else if(capacity < terminals)
                        //not then set it to false
                        possible = false;

                }

                if(possible == true)
                {

                    //if it is possible to accomodate request
                    for (DateTime x : mapLoaded.subMap(startx, endx).keySet()) {
                    {
                        //for dates n, update value for next operation
                        mapLoaded.put(x, mapLoaded.get(x) - terminals);
                    }
                }
                }else{
                 //nothing now
                }
        }else if(requestType.equals("Training")){
            //Training
             for (Integer capacity : mapLoaded.subMap(startx, endx).values()) {
                    //Provides an insight into capacity accomodation possibility
                    //testValue++;
                    terminals = 1;
                    if(capacity >= terminals)
                        possible = true;
                    else if(capacity < terminals)
                        possible = false;

                }


                if(possible == true)
                {
                    for (DateTime x : mapLoaded.subMap(startx, endx).keySet()) {
                    {
                        //46 so that all seats are reserved
                        mapLoaded.put(x, mapLoaded.get(x) - 46);

                    }
                }
                }else{
                 //nothing now
                }

        }


        return possible;
}

}
公共类保留实现
java.io.Serializable{
字符串事件标题;
公开日期开始日期;
公开日期和截止日期;
字符串请求类型;
公共互联网终端;
字符串lastName;
字符串名;
字符串首字母;
公共交通管理局局长:否;
公共临时字符串networkID;
串电话号码;
字符串代码;
字符串orgName;
字符串对齐;
弦乐;
int-mapSize;
MapStorage MapStorage=新的MapStorage();
公共预订(int-badgeNo,字符串networkID){
this.badgeNo=badgeNo;
this.networkID=networkID;
}
公众保留(){
}
公共保留(字符串eventTitle、日期startDate、日期endDate、字符串requestType、int terminals、字符串lastName、字符串firstName、字符串middleInitials、int badgeNo、字符串networkID、字符串电话号码、字符串orgCode、字符串对正){
//在这里创建
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.requestType=requestType;
这个。终端=终端;
this.lastName=lastName;
this.firstName=firstName;
this.middleInitials=middleInitials;
this.badgeNo=badgeNo;
this.networkID=networkID;
this.telephoneNo=telephoneNo;
this.orgCode=orgCode;
这是合理的;
}
公共布尔值checkRange(){
DateTime startx=newdatetime(startDate.getTime());
DateTime endx=newdatetime(endDate.getTime());
//预订状态
布尔可能=假;
//预订类型:1=项目,2=培训
/*
if(requestType.equals(“项目”)){
bookingType=1;
}else if(requestType.equals(“培训”)){
bookingType=2;
}
*/
//生成子映射
//mapSize=bookingType;
TreeMap mapload=null;
试一试{
mapLoaded=mapStorage.RetrieveMap();
}捕获(ioe异常ioe)
{
系统错误打印(ioe);
}
if(requestType.equals(“项目”))
{
//计划
//Insideecheckrange=“在第一个循环中”;
//获取开始和结束之间映射中键的所有值
for(整数容量:mapload.subMap(startx,endx).values()){
如果(容量>=终端)
//是的,那么它是可能的,设置为true
可能=正确;
else if(容量<端子)
//然后将其设置为false
可能=错误;
}
如果(可能==true)
{
//如果有可能满足要求
for(DateTime x:mapload.subMap(startx,endx.keySet()){
{
//对于日期n,更新下一个操作的值
mapLoaded.put(x,mapLoaded.get(x)-终端);
}
}
}否则{
//现在什么都没有
}
}else if(requestType.equals(“培训”)){
//训练
for(整数容量:mapload.subMap(startx,endx).values()){
//提供对容量容纳可能性的深入了解
//testValue++;
终端=1;
如果(容量>=终端)
可能=正确;
else if(容量<端子)
可能=错误;
}
如果(可能==true)
{
for(DateTime x:mapload.subMap(startx,endx.keySet()){
{
//46这样所有的座位都被预订了
mapload.put(x,mapload.get(x)-46);
}
}
}否则{
//现在什么都没有
}
}
可能返回;
}
}

您的
预订中有临时字段
类:

public transient int badgeNo;
public transient String networkID;
Java序列化将:

必须标记任何无法序列化或 不希望序列化的任何字段。序列化并不重要 关于访问修饰符(如private)——所有非临时字段都是 被认为是对象持久状态的一部分,并且符合 坚持


一个简单的解决方法是使这些字段非瞬态

如果在
Reservation
中有
transient
字段,可以解释这一点

但是,我认为问题在于,您实际上正在显示在
retrieveReservation()
开始时创建的虚拟
Reservation

为什么??因为
rset.next()
正在返回
false

为什么??我们来看看这个,

   PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM 
         SMD_RESERVATION_INSTANCES WHERE id = ?");         
   prepareStatement.setString(1, localIDTest);
   ResultSet rset = prepareStatement.executeQuery();
   prepareStatement.executeUpdate();
   //prepareStatement.close();
   con.commit();
这是很随意的。您不需要对同一条准备好的语句同时执行
executeQuery
executeUpdate
。此时不需要
commit()
,因为这是一个查询

因此,我认为正在发生的是,您对
executeUpdate
的伪调用正在关闭
ResultSet
,因此当您调用
rset.next()
时,它将返回
false
。(另p