/**
* 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 ==>我的Model类 /
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;
}
}
//我正在尝试从模型中获取值的服务类,但是我提取的是null值,这将进一步传递给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返回null值?
答案 0 :(得分: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
。我希望你现在能得到它。