/**
* 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返回???
我會建議**通過您的IDE調試器代碼步進**。我相信它會很快泄露原因。 – DevilsHnd
在函數的最後一個返回空值處添加一個斷點,然後您將能夠觀察堆棧跟蹤。稍微移動一下,然後你會發現你的錯誤。 –
@DevilsHnd謝謝respond.yeah,即使在調試setter設置的值,而我很驚訝爲什麼getter返回空值:( –