Mooney_S
Mooney_S

Reputation: 27

How to store 60 integers in a class to be written/read from a database?

I'm setting up an application to keep track of scores of an archery shoot. Each shoot consists of 60 arrows ranging from 0-10. What would be the best way of setting up the class to store every arrows score, to then be written into an SQLite database?

I could set up a variable for every single arrow but this seems inefficient. What is the better way of doing this?

public class Portsmouth {

    private int id;
    Date date;
    private int arrow1;
    private int arrow2;
    private int arrow3;
    ...
    private int arrow60;

}

Upvotes: 1

Views: 101

Answers (5)

Andrew
Andrew

Reputation: 307

If you plan on pushing/pulling all of the information to a database, one way to move this data would be to use Java Persistence API (JPA). This way you can push or pull the data easily. One limitation of JPA is that each column in a row must have its own variable, so you would end up with many strings, as you said. Do note that JPA mandates a database key of some sort.

Fortunately, if you wanted to push your data up, it would be as easy as:

Class.forName("your.database.driver");
Connection con = DriverManager.getConnection("url", "username", "password");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnitName");
EntityManager em = emf.createEntityManager();

PersistenceEntity persistenceEntity; //This is what holds all of your data

em.persist(persistenceEntity); //Push a full row of data
em.getTransaction.commit(); 
con.close();

You can also employ some rather clever ways to display/get access to this information when not using it directly with the database, to prevent you from calling something like Java Reflection API.

Upvotes: 0

Anubhav Singh
Anubhav Singh

Reputation: 8699

Simply use an integer array like this:

import java.lang.System; //imported by default

public class Portsmouth {

    private int id;
    Date date;
    private int[] arrow; 

    public Portsmouth(int id, Date data, int[] arr){
      this.id = id;
      this.date = date;
      arrow = new int[60];

      System.arraycopy(arr, 0, arrow, 0, 60);

      /**for(int i=0;i<60;i++){
         arrow[i] = arr[i];
      }**/
    }

}

Upvotes: 3

PrzemyslawP
PrzemyslawP

Reputation: 340

But why did you named your class Portsmouth ? It's better to make everything more usable. Maybe three classes (3 database tables).

Class Tournament{
    private int id;
    Date date;
    String name;
    String place
    Shoot shoot;
}

Class Shoot {
     private int id;
     @OneToMany     
     List<Arrow> arrowList;
}

Class Arrow {
     private int id;
     @ManyToOne
     private Shoot shoot;
     private Integer orderNumber; (1-60)  
     private Integer result (1-10)  instead of Integer can be modified to ENUM ArrowResult from 1-10
}

Upvotes: -1

Luis Vargas
Luis Vargas

Reputation: 13

I suggest an Arrow class which will have your score for that arrow 1-10. Then an ILIST of arrow in another class called Shoot. Then I will imagine you have another tournament and/or shooter class for which you include properties in the Shoot class. Give it a try if you like it and I can expand.

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

Use an int[] array or List<Integer> collection instead of defining the fields one by one.

The SQL that you run to instert rows into the database depends on your schema e.g. table being de-normalized with 60 columns vs 60 rows, one for each arrow. Usually one separates the domain model from the schema with a pattern like DAO.

Upvotes: 2

Related Questions