简体   繁体   中英

How to store Arraylist values into database in java?

I want to store the Arraylist values into database(postgresql database) in java.

ArrayList<String> ActiveProcess = new ArrayList<String>();
        ArrayList<String> InActiveProcess = new ArrayList<String>();

I want to store the details in a database ProcessInfo table with following fields Process Name, Process Status, Email Sent, SMS Sent, LastModifiedTime. The ActiveProcess contains the process names are server.exe, Receiver.exe .etc its process status is true,Email Sent, SMS Sent is false.

The InActiveProcess contains the process names are SmsReceiver.exe, sender.exe .etc its process status is false ,Email Sent, SMS Sent is true .

How to do this..? Thank in advance.

build model class to facilitates storing process information in DB:

public class Process {
  public static enum TYPE { ACTIVE , NOT_ACTIVE } ; 

  private ArrayList<String> names ;
  private String emailSent emailSent ;
  private Date lastModification ;
  private Boolean status ;
  private TYPE type ;

  //
  // provide constructors + setter + getter methods
  //

}

to store process in DB , use:

public void saveProcess (Process p) {
  ArrayList<String> name = p.getProcessesName();  // or you can get iterator from the arraylist
  String emailSent = p.getEmailSent();
  Date lastModification = p.getLastModification();
  Boolean status = p.getStatus();

  //
  // use SQL insert statements to save info
  //

}

to get all processes:

public ArrayList<Process> getAllProcesses () {
  ArrayList<Process> processes = new ArrayList<Process>();

  //
  // use SQL select statement to get processes
  //

   return proceses ;
}

or you can pass TYPE to the method to get the active or non active processes.

The best way is to keep it simple and avoid unnormal database design. So I would suggest a new table with two column (id, value) referenced by the main table using id

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM