Results 1 to 5 of 5
  1. #1
    thor2014 is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Insert ArrayList in a Table (MySQL)

    Hello,

    I am new in this forum and beginner in Java. I am facing the following scenario:

    1- Take information from distinct columns from two different tables - OK
    2- Insert this information into a new table.

    I have created a ArrayList and inserted all the information that I need but now I am trying to put this ArrayList into the new table and I am not sure how to do it. I have done research and tests but unsuccessfully.

    Can anyone help me please? I appreciate it.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Insert ArrayList in a Table (MySQL)

    - do you know how to iterate through an arraylist? If not -> learn about using collection classes
    - do you know how to insert data into a table? If not -> learn about SQL insert statements

    And knowing all that, you need to investigate how to execute an insert statement using JDBC (hint: you use a PreparedStatement)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Insert ArrayList in a Table (MySQL)

    Just as a side note:
    If all that is done in one database then you might check if it is possible with SQL commands.

    Also be aware: even with separate databases it might be possible to link them.

    With kind regards,

    Konrad

  4. #4
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Insert ArrayList in a Table (MySQL)

    Is it an ArrayList of objects? If it is then you just need to define a persist(Connection c) and update(Connection c) functions in that object. Then in the class with ArrayList iterate through like

    Java Code:
    private ArrayList<Ticket> tickets
    
    void putDataInArrayList(){
    for(Ticket t: tickets){
    t.store(); 
    }
    }
    Java Code:
    //This function store the object by persist or update in correct circumstances.
      public void store(Connection conn) throws SQLException {
            if (isDirty()) {
                if (isPersist()) {
                    persist(conn);
                } else {
                    update(conn);
                }
            }
        }

  5. #5
    thor2014 is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Insert ArrayList in a Table (MySQL)

    Thanks everyone for the answer.
    Last edited by thor2014; 05-02-2014 at 04:55 PM.

Similar Threads

  1. insert to MySQL table
    By asai in forum New To Java
    Replies: 2
    Last Post: 02-19-2012, 02:56 PM
  2. Replies: 5
    Last Post: 07-13-2011, 01:34 PM
  3. Not able to insert data into mysql table
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 02-16-2011, 02:46 PM
  4. Replies: 1
    Last Post: 06-08-2010, 10:58 AM
  5. How to insert DATE into mysql using JSP?
    By sivakumar_sakam in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-27-2009, 08:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •