Java code for connecting Mysql database and using Arraylist type

To get to use JDBC and ArrayList I make the following example to help myself.  The following is the steps:

  • Create a database "travel" with a table named "person"
  • Define a class named "Person" for mapping the person table's columns
  • Connecting Mysql database
  • Select some records from the database "travel"
  • Make objects based on each record¬†extracted from ¬†"person" table
  • Using ArrayList of object to store the objects of Person.
  • Print the data in ArrayList

The following is the code:

Person.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package javaapplication1;
 
/**
 *
 * @author wongwong
 */
public class Person {
    private String name;
    private String jobtitle;
    private int frequentflyer;
 
    public int getFrequentflyer() {
        return frequentflyer;
    }
 
    public void setFrequentflyer(int frequentflyer) {
        this.frequentflyer = frequentflyer;
    }
 
    public String getJobtitle() {
        return jobtitle;
    }
 
    public void setJobtitle(String jobtitle) {
        this.jobtitle = jobtitle;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

Main.java

package javaapplication1;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javaapplication1.Person;
 
public class Main {
    public static void main(String[] args) throws SQLException {
        ArrayList<Person> personlist = new ArrayList<Person>();
        //List<Person> personlist = new List<Person>();
        try {
            // Step 1: Load the JDBC driver. jdbc:mysql://localhost:3306/travel
            Class.forName("com.mysql.jdbc.Driver");
 
            // Step 2: Establish the connection to the database.
            String url = "jdbc:mysql://localhost:3306/travel";
            Connection conn = DriverManager.getConnection(url, "root", "admin");
            Statement st = conn.createStatement();
            ResultSet srs = st.executeQuery("SELECT * FROM person");
            while (srs.next()) {
                Person person = new Person();
                person.setName(srs.getString("name"));
                person.setJobtitle(srs.getString("jobtitle"));
                person.setFrequentflyer(srs.getInt("frequentflyer"));
                personlist.add(person);
            }
 
            System.out.println(personlist.size());
            System.out.println(personlist.get(1).getName());
            System.out.println(personlist.get(2).getName());
            System.out.println(personlist.get(3).getName());
            System.out.println(personlist.get(4));
 
        //System.out.println(namelist.);
        } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }
    }
}
Category >> Database  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Ende on 2013-3-15

    Dear Sir
    In your introduction you mentioned this:

    Using ArrayList of object to store the objects of Person.

    However, your code does not reflect this. Why?

  2. Jez Shanes on 2014-12-14

    thanks you so much!!

  3. Vila Rica on 2016-10-7

    Hey wongwong!!
    good job simple and efficient, thart’s the way that I like.

    Congratulation.

Leave a comment

*