I can return a single object into json format from the returnMachineTable method. But when i try to return all the objects from the Manufacturer table. i cannot return it as json format. Any suggestions?
@Path("/test")
public class testWS {
// private static final String api_version = "00.01.00";
@GET
@Path("/database")
// @Produces(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public Machine returnMachineTable() throws Exception {
// public String returnDatabaseStatus() throws Exception{
PreparedStatement query = null;
String myString = "";
String returnString = null;
Connection conn = null;
Machine u = null;
// returnString = "<p>data base context: "+"shit ass"+"</p>";
try {
conn = db.DBConn().getConnection();
query = conn.prepareStatement("SELECT * FROM MACHINE");
ResultSet rs = query.executeQuery();
while (rs.next()) {
myString = myString + rs.getString("LOCATION") + "!!!!!"
+ rs.getInt("MACHINEID") + "!!!!!"
+ rs.getInt("MACHINEID");
Long maintainenceDate = rs.getDate("MAINTDATE").getTime();
Long dateInstalled = rs.getDate("DATEINSTALLED").getTime();
u = new Machine();
u.setMachineId(rs.getInt("MACHINEID"));
u.setLocation(rs.getString("LOCATION"));
u.setMaintainenceDate(maintainenceDate);
u.setDateInstalled(dateInstalled);
u.setInstaller(rs.getString("INSTALLER"));
u.setMachineCode(rs.getString("MACHINECODE"));
u.setModel(rs.getString("MODEL"));
u.setManufacturerID(rs.getString("MANUFACTURERID"));
u.setName(rs.getString("NAME"));
u.setSoftware(rs.getString("SOFTWARE"));
myString = u.toString();
}
returnString = "---" + myString + "---\n";
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
}
// return returnString;
return u;
}
@GET
@Path("/ManufacturerTable")
@Produces(MediaType.APPLICATION_JSON)
public List<Manufacturer> returnManufacturerTable() throws Exception {
PreparedStatement query = null;
String myString = "";
String returnString = null;
Connection conn = null;
Manufacturer u = null;
List<Manufacturer> ulist = null;
int i = 0;
String uList = "";
try {
conn = db.DBConn().getConnection();
query = conn.prepareStatement("SELECT * FROM MANUFACTURER");
ResultSet rs = query.executeQuery();
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
myString = myString + rs.getString("MANUFACTURERNAME")
+ "!!!!!" + rs.getInt("MANUFACTURERID");
u = new Manufacturer();
u.setManufacturerName(rs.getString("MANUFACTURERNAME"));
u.setManufacturerId(rs.getInt("MANUFACTURERID"));
ulist.add(i, u);
i++;
myString = myString+"\n"+u.toString();
}
returnString = "---" + myString + "---\n";
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
}
// return returnString;
/* Gson gson = new Gson();
return gson.toJson(ulist);*/
return ulist;
}
}
I think you should clean your code first. IE: you are declaring this variable and never using it.
JSONArray jsonArray = new JSONArray();
Secondly, why you put all the object properties to a big string? Why not just add all objects to an array and then convert it to json array?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.