2

I am currently working on a project that is based on a simple SQL Database. What I need to happen is have the end-user prompted to enter their warehouse then from their pull a list of trucks from the DataBase and present the trucks one at a time so they user can input current mileage. I have the warehouse input working fine and I am able to retrieve the truck list from the input as well. I just can't find a way to present it one at a time for current mileage input. Below is the code I have so far:

public class PreventativeMaintenance {

public static void main(String[] args) throws Exception 
{
    Scanner kb = new Scanner (System.in);
    System.out.print("Warehouse? ");

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");

    String warehouse = kb.next();

    String query = "SELECT * FROM `new_schema`.`trucks` WHERE `warehouse` = '" + warehouse + "'";

    Statement st = con.createStatement();


    ResultSet rs = st.executeQuery(query);
  }
}

I have attempted to use an ArrayList of Objects called Truck and built the objects from the SQL Query. However, I ran into the same issue I am now where I cannot find a way to present the different truck numbers one at a time so the user may input their current mileage.

3
  • Try this tutorial about retrieving results from a ResultSet. Commented May 14, 2015 at 15:11
  • You should create a Truck class that represents a truck. That class should implement methods to get the current mileage from the user and save that value back to the database. You'd simply iterate over the ArrayList and, for each Truck instance, call the necessary methods to do that. Commented May 14, 2015 at 15:27
  • I would like to keep the current mileage stored to an integer or something of the sort to do some checks before doing updates. So, when the current mileage is inputted it will then be compared against another field for the next mileage for a PM, then another field is checked to see if it is marked yes or no for PM due and updated if needed. Commented May 14, 2015 at 15:34

1 Answer 1

1

You can try something like this:

    //once you get resultset rs after executing query
    while(rs.next()){  
        String truckName = rs.getString("<name of truck column>");
        setTruckMileage(truckName);
    }

    private void setTruckMileage(String truckName){  
        Scanner kb = new Scanner (System.in);
        System.out.print("Enter mileage for truck " + truckName +" : ");

        Integer mileage = kb.nextInt();

        //save mileage entered by user
    }
Sign up to request clarification or add additional context in comments.

2 Comments

After reading through the tutorial posted by RealSkeptic I have begun to go down a similar path to this. I will post shortly if I am still stuck.
This was the general route I went and it solved what I needed to do. Thanks again for everyone's help!

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.