2017-04-01 59 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