1

I'm currently doing a mini parking system for our JAVA bootcamp.

I'm trying to get the value of the integer variables into a different class. These variables are results of the sql counts of total parked (countCar,countMotor, and countVan).

This is what I did:

I stored them in a class called SQLCountResult:

public class SQLConnections {
    public Connection con = null;

    public void parkInSQL() {
        con = dbConnect.con();

        int countCar;
        int countMotor;
        int countVan;

        int parkCar;
        int parkMotor;
        int parkVan;

        int[] vehicleTotal = new int[3];

        String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
        String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
        String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";

        try {
            Statement stmtCar = con.createStatement();
            Statement stmtMotor = con.createStatement();
            Statement stmtVan = con.createStatement();

            ResultSet rsCar = stmtCar.executeQuery(qCar);
            ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
            ResultSet rsVan = stmtVan.executeQuery(qVan);

            rsCar.next();
            rsMotor.next();
            rsVan.next();

            countCar = rsCar.getInt(1);
            countMotor = rsMotor.getInt(1);
            countVan = rsVan.getInt(1);

        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
}

Now I want to call countCar, countMotor, and countVan to another class called Park. My code for the Park class is below:

public class Park {

    public Connection con = null;

    public void parkMethod() {
        SQLConnections sqlConnections = new SQLConnections();
        sqlConnections.parkInSQL();
    }

}

I also tried using extend but this didn't work either. How can I call these variables to the Park class?

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Sue Lee
  • 53
  • 6

4 Answers4

2

I suggest you first do some supplementary reading on class members and using objects. If you are not familiar with objects, read this comprehensive article called What Is an Object?. I cannot stress how important it is to understand this. The very concept of "Object Oriented Programming" is centered around objects. Once you have familiarized yourself with this, then go on and read the rest of my answer.


Now that you are familiar with the articles I cited above, I will begin by setting up some fields, which are explained in the "Declaring Member Variables" article. First we need ask ourselves, "Which variables are we trying to retrieve in our Park class?" As you mentioned in your question, the concerned variables are countCar, countMotor, and countVan.

We have identified the variables we are after. Now what? The next step is to change these variables to fields so that they don't get gobbled up by the garbage collector when your parkInSQL method returns. This is a simple task. Let's start by removing the variables from your parkInSQL method:

public void parkInSQL() {
        con = dbConnect.con();

        ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶C̶a̶r̶;̶
        ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶M̶o̶t̶o̶r̶;̶
        ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶V̶a̶n̶;̶

        //...
}

Next we need to declare these variables as fields. To do that, simply place the variable declarations at the top of your class, just like you did with your Connection variable:

public class SQLConnections {
    public Connection con = null;
    public int countCar;
    public int countMotor;
    public int countVan;

    //...
}

Notice that I used the public access modifier. This is to make sure that these fields will be visible to our Park class (We could make them private, but that would get us into getters and setters, which is a lesson for another time).

Once we have our variables declared as fields, we should be all set with the SQLConnections class. The rest is easy as pie.

You already have half of the work done. If you look at your parkMethod, you'll see that you constructed an SQLConnections object:

SQLConnections sqlConnections = new SQLConnections();

Now all we need to do is reference the newly created fields, which is explained in the "using objects" article I cited above. After you call sqlConnections.parkInSQL(), you can reference the fields with sqlConnections.countCar, sqlConnections.countMotor, and sqlConnections.countVan:

public class Park {

    public Connection con = null;

    public void parkMethod() {
        SQLConnections sqlConnections = new SQLConnections();
        sqlConnections.parkInSQL();

        int countCar = sqlConnections.countCar;
        int countMotor = sqlConnections.countMotor;
        int countVan = sqlConnections.countVan;
    }

}

Now you can operate on these values accordingly.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
1

You could change the scope of the variables.

Move this:

    int countCar;
    int countMotor;
    int countVan;

outside of the parkInSQL method, and add the access modifier, public:

    public int countCar;
    public int countMotor;
    public int countVan;

Then you can access it from the Park class like this:

public class Park {

    public Connection con = null;

    public void parkMethod() {
        SQLConnections sqlConnections = new SQLConnections();
        int iAmUsingCountCar = sqlConnections.countCar;
    }

}
Allison B
  • 166
  • 9
0

Variables defined inside a method are local to that method.
If you want to share variables between class/methods, then you'll need to specify them as member variables of the class. You should initialise it outside the methods.

public class SQLConnections { 

    public int countCar;
    public int countMotor;
    public int countVan;

    //...
}
Metris Sovian
  • 254
  • 4
  • 20
0
public class VehicleStatistics {
  private int countCar;
  private int countMotor;
  private int countVan;

  public VehicleStatistics(int countCar, int countMotor, int countVan) {
    this.countCar = countCar;
    this.countMotor = countMotor;
    this.countVan = countVan;
  }

  public int getCountCar() {
    return countCar;
  }

  public void setCountCar(int countCar) {
    this.countCar = countCar;
  }

  public int getCountMotor() {
    return countMotor;
  }

  public void setCountMotor(int countMotor) {
    this.countMotor = countMotor;
  }

  public int getCountVan() {
    return countVan;
  }

  public void setCountVan(int countVan) {
    this.countVan = countVan;
  }
}

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class VehicalesDAOImpl {
  private Connection connection;

  public VehicalesDAOImpl(Connection connection) {
    this.connection = connection;
  }

  public VehicleStatistics getVehicleStatistics() {
    String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
    String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
    String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";
    VehicleStatistics result = null;
    try {
      Statement stmtCar = connection.createStatement();
      Statement stmtMotor = connection.createStatement();
      Statement stmtVan = connection.createStatement();

      ResultSet rsCar = stmtCar.executeQuery(qCar);
      ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
      ResultSet rsVan = stmtVan.executeQuery(qVan);

      rsCar.next();
      rsMotor.next();
      rsVan.next();

      int countCar =rsCar.getInt(1);
      int countMotor =rsMotor.getInt(1);
      int countVan =rsVan.getInt(1);
      result = new VehicleStatistics(countCar, countMotor, countVan);
    } catch(SQLException e) {
      //log error
    } finally {
      //close connections etc...
    }
    return result;
  }
}
Bhukailas
  • 47
  • 7