0

I'm using JavaFX to create a basic GUI. I have a database created in MySQL to store the data. (DBConnect) (connector/j)

This is my very first attempt at connecting the two, as well as using ResultSets/DBConnect

Currently, I have 3 Classes: my Game class, my GameUI class (main), and my DBConnect class.

I am attempting to Reference the ResultSet in my GameUI class, that was originally declared in Game class.

public class Game {

    private static DBConnect dbc;
    private static Connection conn;
    public ResultSet rs;
    int id;
    String name;
    float price;
    String vendor;
    int rating;


    public Game() {

        try {
            conn = dbc.connect();

            String SQL = "Select * from Person";

            rs = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE).executeQuery(SQL);

            System.out.println("list result set for record..");
            printRs(rs);
        } catch(SQLException ignore) {

        }
    }

My User Defined moveNext() method:

   public Game moveNext() {
        Game g = new Game();
        try {

            if(rs.next() == false)
                rs.previous();
            g.setGameId(rs.getInt("gameId"));
            g.setGameName(rs.getString("gameName"));
            g.setPrice(rs.getFloat("price"));
            g.setVendor(rs.getString("vendor"));
            g.setRating(rs.getInt("rating"));

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

        return g;
    }

The GameUI class i am trying to reference it in:

public class GameUI extends Application {
    private Button firstButton = new Button("First");
    private Button createButton = new Button("Create");
    private Button updateButton = new Button("Update");
    private Button deleteButton = new Button("Delete");
    private Button lastButton = new Button("Last");
    private Button nextButton = new Button("Next");
    private Button prevButton = new Button("Prev");
    GridPane grid = new GridPane();
    HBox hbox = new HBox(14);






    private static DBConnect dbc;
    private static Connection conn;
   // private static



    private Pane initButtons() {
        hbox.getChildren().addAll(firstButton, createButton, updateButton, deleteButton, lastButton, nextButton, prevButton);

        nextButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {

                Game.rs.moveNext();
            }
        });

    return hbox;

    }

My Question is, can I Reference my ResultSet (Game class) in my nextButton event handler (my GameUI class) OR do I have to declare a new result set?

Is this the right place for my user defined moveNext() method, or should I use next()?

I will post more code on request.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TJ Zimmardi
  • 35
  • 2
  • 10
  • Your post includes three different questions. It would be better to have them as separate questions. – hotzst Nov 08 '15 at 08:58

1 Answers1

1

You can pass the result set further, but it is bad practice (See Is it Ok to Pass ResultSet?). From what I see from the code you posted it is mainly an issue with the layering of the different parts of the application, that should be separated:

Database related code belong into one layer. No other layer should directly talk to the DB or handle any DB specific objects. To achieve this the DBConnection class should handle all database related stuff. To pass data from the database to the other layers and vice versa you use a data transfer object (DTO) which is basically an object representation of a specific result set. Usually this boils down to one DTO per database table.

Then you have an intermediate layer, lets call it service layer, which is used to communicate from the UI to the database and do some computation.

On top of that you have the UI layer where your application lives. Here you have to consider which actions belong into that layer and what should better be delegated to the service layer.

Take also a look at this wikipedia article: https://en.wikipedia.org/wiki/Multitier_architecture

Community
  • 1
  • 1
hotzst
  • 7,238
  • 9
  • 41
  • 64