After I insert data into a MySQL database using a prepared statement I retreive an ID which the database creates for me. I then want to store that retreived ID as an element of the first object of the array.
These are the properties of my class:
private String DiskID;
private String Letter;
private String Label;
private String Type;
private String Health;
private String Op;
private String Size;
private String Remaining;
private List<Disk> diskTable = new ArrayList<>();
This is the method which inserts the disk information:
public void insertDiskInfo(Connection conn){
String query = "INSERT INTO Disk(DiskLetter, DiskName, FileSystemType, HealthStatus, OperationalStatus, DiskSize, DiskSpace)" +
"VALUES(?,?,?,?,?,?,?);" ;
String ReturnDiskID = "SELECT_LAST_INSERT_ID();" ;
try{
PreparedStatement ps = conn.prepareStatement(query);
PreparedStatement ID = conn.prepareStatement(ReturnDiskID);
for(int i=0; i<diskTable.size(); i++) {
ps.setString(1, diskTable.get(i).Letter);
ps.setString(2, diskTable.get(i).Label);
ps.setString(3, diskTable.get(i).Type);
ps.setString(4, diskTable.get(i).Health);
ps.setString(5, diskTable.get(i).Op);
ps.setString(6, diskTable.get(i).Size);
ps.setString(7, diskTable.get(i).Remaining);
ps.addBatch();
ResultSet rs = ID.executeQuery();
if(rs.next()){
diskTable.add(1, DiskID);
}
else {
System.out.println("Failed to retrieve DiskID");
}
}
}catch(Exception e){
e.printStackTrace();
e.getCause();
}
How do I get it to add the retreived disk ID to the first element of the first disk?
diskTablein your example is not array.ArrayListis not array. Here is example of array:int[] arr;diskTablelist to contain both an ID, and Disk objects? Why not store the ID separately?