0

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?

7
  • diskTable in your example is not array. Commented Feb 3, 2021 at 12:45
  • @talex I thought i had declared it as an Array List? Commented Feb 3, 2021 at 12:48
  • Yes. ArrayList is not array. Here is example of array: int[] arr; Commented Feb 3, 2021 at 12:50
  • You want your diskTable list to contain both an ID, and Disk objects? Why not store the ID separately? Commented Feb 3, 2021 at 12:50
  • It's an ID for each Disk object in the Array List. When I update the information in the table of my database i need the diskID to know which Disk to update. The reason I can't assign this at the same time I create the Disk object is because there are multiple of this applicationon multiple different PCs accross a business. Therefore the counter needs to count from the database, not the application. Commented Feb 3, 2021 at 12:56

1 Answer 1

1

At 1st ArrayList doesn't support add element like this. ArrayList isn't an array. You declared arrayList to store Disk objects but use it like a Map (key, value). Try to use some implementation of map (like HashMap) to store objects like this

Map<Long, Disk> map = new HashMap();
map.put(disk.Id, disk);
Sign up to request clarification or add additional context in comments.

4 Comments

Does this syntax look correct to you: diskTable.put(rs.getString(1), diskTable.get(i));
In your case it will work but I think you should redesign this class - seems like now it has many responsibilities. Now you store not inserted to DB objects in List. It`s Okay. Then you want to insert them in DB and collect keys with objects in some structure to have access to objects via key. So store it in map and put in like you wrote in comment .
The reason the list exists is because i get the information out of powershell and I need somewhere to store the values before they are inserted into the DB. Then after inserted into the database I have their ID. Do you recommend using seperate lists for these two functions?
Use list for not inserted objects and map to inserted - using map you can get a needed object via id but using list you have only indexes. In general - read about SOLID principles. and try to redesign you app. Mostly it depends on what you gonna do with Disks after insert. You can update each record in list with not inserted objects with id like `list.get(i).setId(rs.getInt(1)) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.