Gytis39 Gytis39 - 5 months ago 8
Java Question

Creating a database of foods in Java

Ive just started learning Java and I spent most of the day watching video tutorials, however now I've decided to do something practical, as I dont think I can absorb anymore information today.

I had an idea to develop a fitness app a while ago, but was hit by how difficult that actually is without any knowledge, so I thought I will start by just creating a macro counter. I created a class:

public class Food {

private String name;
public int weight;
private int calories;
private int protein;
private int fat;
private int carbs;

public int tCals = weight * calories;
public int tProtein = weight * protein;
public int tFat = weight * fat;
public int tCarbs = weight * carbs;

public void macrocounter(){
System.out.println("Total calories: " + tCals);
System.out.println("\nProtein: " + tProtein);
System.out.println("\nCarbs: " + tCarbs);
System.out.println("\nFat: " + tFat);



Now what I am having problems with is creating a database. I want to add at least 10 simple meals to begin with, they would all have the macros set in stone, the user would just type in the name of the meal and how many grams he ate, then the app would recognize the food from the database, fill in weight, and print out the total macros.

I am not asking here how to write the specific code, just can you point me to what I should read up on more to be able to do this? Java is an insanely vast language, and I am having trouble to find the actual information I need on creating a database.

Thank you.


You want just to store the data in main memory? or in a real database to disk?

If it is the first option, you can do this:

ArrayList<Food> foods =  new ArrayList<Food>();

Once you have your list, you can add things like this:

Food f = new Food(; //You need a constructor
                               //to pass the data.


To consult it you can do a number of things:

Food f0 = foods.get(0);
Food f1 = foods.get(1);

You can also query for a particular object:

if (foods.contains(f0) {
    // ...

Google for "java class ArrayList" and you'll see what you can do.

If, on the contrary, you want to create a real database, you need to read about JDBC drivers. To begin, I recommend you sqlite.

package consultarcalibredb;

import java.sql.*;

public class ConsultarCalibreDB {

     * @param args the command line arguments
    public static void main(String[] args) {

        Connection conn = null;

        int id;
        String title, author, path;
        try  {
            DriverManager.registerDriver(new org.sqlite.JDBC());
            conn = DriverManager.getConnection("jdbc:sqlite:/home/user/folder/file.db");
            String sql = "select id, title, author_sort, path from books where author_sort like '%orwell%'";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while ( {
                id = rs.getInt("id");
                title = rs.getString("title");
                author = rs.getString("author_sort");
                path = rs.getString("path");



        } catch (Exception e) {