0

So I have this code, and I want it to print out a question in the console for each value for me to fill in and later then update that in the database, for example:

--- Insert required information ---
Supplier name: Frank & CO
Supplier email: frankandco@franks.com
Supplier phonenumber: 0702943814
Supplier contactperson: Frank Earnest

I am not sure though as to how to do it, could I do this with an if loop? and if, how?

public void insertSupplier() {
    try {
        
        cn = DriverManager.getConnection(DB_ADDRESS, DB_USER, DB_PASS);
        pstat = cn.prepareStatement("INSERT INTO supplier (supplier_name, supplier_email, supplier_phonenumber, supplier_contactperson) VALUES (?,?,?,?)");     
        String supplier = scan.nextLine();

        pstat.setString(1, supplier);
        pstat.executeUpdate();
        System.out.println("Supplier added to database");
        cn.close();
        
        
    } catch (SQLException e) {
        System.out.println(e);
    }
}
Pawpaw
  • 1
  • 1
  • Sorry for being overly didactic, but there is no such thing as an "if loop" – Hovercraft Full Of Eels Sep 06 '21 at 13:43
  • Forgive me, I'm quite new :) – Pawpaw Sep 06 '21 at 13:44
  • No problem, but do consider clarifying your question a bit. How is the data being entered? What specifically has you stuck? – Hovercraft Full Of Eels Sep 06 '21 at 13:44
  • What I need more specifically would be a sort of scanner loop that wont exit until all the value requirements are met. So if I enter supplier name succesfully in the console, I want it to ask me the next question and add that to the database aswell. When all the questions/values are met I want it to exit. At the moment I have just one for the supplier_name, but I don't know how to proceed and get it to print a question for each value in the console and also add the answers. – Pawpaw Sep 06 '21 at 13:50
  • Then you need to use a loop, a for-loop if you know in advance how many entries will be entered, or a while-loop, perhaps one that quits when a sentinel String is entered, and then use Prepared Statement's add batch and execute batch update. – Hovercraft Full Of Eels Sep 06 '21 at 13:53
  • You're asking about a multi-step process, getting user input repeatedly, then updating database with this input, and so I advise you to divide and conquer: solve each step, one at a time, and then put them together. – Hovercraft Full Of Eels Sep 06 '21 at 13:54

0 Answers0