2017-04-01 96 views
-1
/** 
* ConnectDB2.java , i'm fetching data from database and setting values to model class. 
*/ 
    package org.com.repair.spotify.repair.db; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

import javax.ws.rs.Path; 

import org.com.repair.spotify.repair.model.RepairDetails; 

/** 
* @author www.javaworkspace.com 
* 
*/ 
@Path("/connectDB2") 
public class ConnectDB2 { 
    Connection connection = null; 
    ResultSet resultSet = null; 
    Statement statement = null; 
    String deviceName; 
    String deviceModel; 
    String ticketId; 
    String issue; 
    String deviceType; 

    public ConnectDB2() { 
     try { 

      Class.forName("com.ibm.db2.jcc.DB2Driver"); 
      connection = DriverManager.getConnection(
        "jdbc:db2://localhost:50000/HELLO", "db2admin", "admin"); 
      statement = connection.createStatement(); 

      resultSet = statement.executeQuery("SELECT * FROM DEVICE "); 
      while (resultSet.next()) { 
       System.out.println("DEVICE BRAND:" + resultSet.getString(1) 
         + " || ISSUE: " + resultSet.getString(2) + " ||MODEL:" 
         + resultSet.getString(3) + "||TYPE:" 
         + resultSet.getString(4)); 
       RepairDetails Rd = new RepairDetails(); 
       Rd.setDeviceModel(resultSet.getString(1)); 
       Rd.setIssue(resultSet.getString(2)); 
       Rd.setDeviceType(resultSet.getString(3)); 
       Rd.setDeviceType(resultSet.getString(4)); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       resultSet.close(); 
       statement.close(); 
       connection.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

} 

/RepairDetails.java ==>我的模型類/試圖值傳遞給從模型類

package org.com.repair.spotify.repair.model; 
import javax.xml.bind.annotation.XmlRootElement; 
@XmlRootElement 
public class RepairDetails { 

    String deviceName; 
    String deviceModel; 
    String ticketId; 
    String issue; 
    String deviceType; 

    public RepairDetails() { 

    } 

    public RepairDetails(String deviceName, String deviceModel, 
      String ticketId, String issue, String deviceType) { 
     super(); 
     this.deviceName = deviceName; 
     this.deviceModel = deviceModel; 
     this.ticketId = ticketId; 
     this.issue = issue; 
     this.deviceType = deviceType; 
    } 

    public String getDeviceName() { 
     System.out.println("getter" + deviceName); 
     return deviceName; 
    } 

    public void setDeviceName(String deviceName) { 
     System.out.println("setter" + deviceName); 
     this.deviceName = deviceName; 
    } 

    public String getDeviceModel() { 
     return deviceModel; 
    } 

    public void setDeviceModel(String deviceModel) { 
     System.out.println("setter" + deviceModel); 
     this.deviceModel = deviceModel; 
    } 

    public String getTicketId() { 
     return ticketId; 
    } 

    public void setTicketId(String ticketId) { 
     this.ticketId = ticketId; 
    } 

    public String getIssue() { 
     return issue; 
    } 

    public void setIssue(String issue) { 
     System.out.println("setter" + issue); 
     this.issue = issue; 
    } 

    public String getDeviceType() { 
     return deviceType; 
    } 

    public void setDeviceType(String deviceType) { 
     System.out.println("setter" + deviceType); 
     this.deviceType = deviceType; 
    } 

} 

//服務從那裏類的Web服務XML響應時越來越空值我是來自模型試圖獲取值,但我取這是對getRepairdetails()

package org.com.repair.spotify.repair.service; 
import java.util.ArrayList; 
import java.util.List; 

import org.com.repair.spotify.repair.db.ConnectDB2; 
import org.com.repair.spotify.repair.model.*; 

public class RepairService { 

    public RepairService() { 
     ConnectDB2 db = new ConnectDB2(); 
    } 

    public List<RepairDetails> getRepairService() 

    { 
     System.out.println("getRepairDetails-->2"); 
     RepairDetails Rd = new RepairDetails(); 

     System.out.println("hey im firing"); 
     RepairDetails RD1 = new RepairDetails(Rd.getDeviceName(), 
       Rd.getDeviceModel(), Rd.getIssue(), Rd.getDeviceType(), 
       "Mobile"); 
     List<RepairDetails> list = new ArrayList<RepairDetails>(); 
     list.add(RD1); 
     return list; 

    } 

} 

進一步傳遞空值請幫助我,爲什麼空值由getter返回???

+0

我會建議**通過您的IDE調試器代碼步進**。我相信它會很快泄露原因。 – DevilsHnd

+0

在函數的最後一個返回空值處添加一個斷點,然後您將能夠觀察堆棧跟蹤。稍微移動一下,然後你會發現你的錯誤。 –

+0

@DevilsHnd謝謝respond.yeah,即使在調試setter設置的值,而我很驚訝爲什麼getter返回空值:( –

回答

0

讓我們從研究RepairDetails類開始。這個實現了一個POJO(普通的舊Java對象)幷包含兩個構造函數。

  • public RepairDetails()
  • public RepairDetails(String, String, String, String, String)

所以,當你創建了第一個構造函數,這意味着你不設定任何的領域,這意味着,值初始化null的對象。

現在讓我們來看看RepairService類。你在getRepairService()方法中有這個代碼。

RepairDetails Rd = new RepairDetails(); 

RepairDetails RD1 = new RepairDetails(Rd.getDeviceName(), 
     Rd.getDeviceModel(), Rd.getIssue(), Rd.getDeviceType(), 
     "Mobile"); 

List<RepairDetails> list = new ArrayList<RepairDetails>(); 
list.add(RD1); 

在這裏,我們有以下看法:

  • Rd是第一個構造函數創建一個對象,因此有效地Rd值會null
  • 您正在構建RD1,其值爲Rd,這意味着它們也將是null

我希望你現在就明白。

+0

我明白了,但如何調用POJO的值(getter())類的屬性,我設置從數據庫中提取的值,並將它們發送到構造函數並以輸出響應(XML)顯示當前輸出:從以下構造函數調用RepairDetails RD1 = new RepairDetails(Rd.getDeviceName(), Rd。 MobileDeviceModel(),Rd.getIssue(),Rd.getDeviceType(), 「Mobile」);只顯示硬編碼的「mobile」值,而其餘參數傳遞爲null –

+0

Current Output:<?xml version =「 1.0「encoding =」UTF-8「standalone =」true「?> - Mobile