0

I'm working on a project where I would need to write a program to take in data from MySql into a java program then save that data into an object to be used later. I already managed to set up the JDBC connection to the MySql database and get all the information from the database as well as being able to write queries to the DB. What I want to do now is store that information into an object. So, I created a table called people with the columns name and age that contain information. In my program, I have a people class with the name and age fields shown below

public class People {
private String name;
private int age;


public people(String name, int age) {
    this.name = name;
    this.age =age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "people{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
}

}

I'm wondering how I could take the MySQL data that I imported and using this people class create objects from it?

1
  • 1
    Did you try executing a SELECT PreparedStatement (or Statement), iterating over the ResultSet and constructing People objects for each row in the result set? Commented Dec 2, 2020 at 20:35

2 Answers 2

3

You should map your Person class with the @Entity to your db table(https://www.baeldung.com/jpa-entity-table-names) and create a TypedQuery(https://www.objectdb.com/java/jpa/query/api) and select all entries in the Database. Or you could just create a normal Query and parse the resultList to a list of peoples.

Sign up to request clarification or add additional context in comments.

5 Comments

The OP is (most likely) asking on how it could be done with the very basic manner. I believe there is no need suggesting heavy-weight tools like ORMs for this case.
Yeah I agree, that's why I described the fancy and the simple way so he can choose what he wants.
Alright, thanks. I think i'll start with the simple PreparedStatement option first and see how that works
@fluffy funny, my first thought when reading the question is that o/r-mapper was exactly the thing asked here
@eis I agree that the OP was not very clear on that, but JPA is not the only ORM-related way around. But yeah, I was just guessing.
2

A very primitive solution could look like this:

    String sql  = " select name         " +
                  "      , age          " +               
                  "   from TABLE_PEOPLE ";
    
    ArrayList<People> staffs = new ArrayList<>();

    try {
        Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            People people = new People(resultSet.getString("name"),
                                       resultSet.getInt("age"));
            staffs.add(people);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();

An important note: Your constructor is wrong. A constructor starts with a capital letter.

 public People(String name, int age) {
    this.name = name;
    this.age = age;
 }

2 Comments

I would suggest using try-with-resources, because the current answer may lead to resource leaks in case of at least resultSet methods throw an exception.
@fluffy I agree. I did not program it that way for a long time. Today I do that with Spring Data JPA.

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.