2017-08-23 4 views
0

私は、タイトル、ISBNなどのプロパティと共に本の画像を表示する将来の目的でJavaFXでUIを作成しています。ボタンを押したときにUIにプッシュするために、結果セットを保存しますか?

私は動作していて、 PreparedStatements。

私が確信しているのは、結果セットと1行のデータ(ブックのプロパティ)をUIに取り込む方法です。次のボタンを押すと、次の行のデータが表示されます。

私は2次元ArrayListでこれを実行しようとしましたが、これは取るべきアプローチです。

これは、これまでの私のコードです:

package libraycollection; 

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 
import javafx.scene.image.ImageView; 

import java.net.URL; 
import java.sql.*; 
import java.util.ArrayList; 
import java.util.ResourceBundle; 

public class ViewCollectionController implements Initializable{ 

    @FXML Label collection_id_label; 
    @FXML Label title_label; 
    @FXML Label subject_label; 
    @FXML Label isbn_label; 
    @FXML Label description_label; 
    @FXML ImageView imageView; 

    //Edit 
    TreeMap<Integer, Book> books = new TreeMap<Integer, Book>(); 

    public void nextBook(ActionEvent event){ 
     //This is a placeholder for Next button event. 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     try{ getCollection();} catch (Exception e){ 
      System.out.println(e); 
     } 
     //Edit 
     collection_id_label.setText(Integer.toString(books.size())); 
     showBook(); 
    } 

    //Edit 
    public void getCollection() throws SQLException{ 

     String host = "jdbc:mysql://127.0.0.1:55556/librarycollection"; 
     Connection con = DriverManager.getConnection(host,"root","password"); 
     PreparedStatement stat = con.prepareStatement("SELECT * FROM book_collection WHERE user_id=?"); 
     stat.setInt(1,MainSelectorController.userID); 

     ResultSet rs = stat.executeQuery(); 

     while (rs.next()){ 
      String title = rs.getString("book_title"); 
      String ISBN = rs.getString("isbn"); 
      String subject = rs.getString("subject"); 
      String description = rs.getString("description"); 
      String image = rs.getString("file"); 
      int book_id = rs.getInt("collection_id"); 

      Book book = new Book(title,ISBN, subject, description,image); 
      books.put(book_id,book); 
     }      
    } 

    //Edit 
    public void showBook(){ 
     for(Map.Entry<Integer,Book> bookEntry : books.entrySet()){ 

     Integer bookId = bookEntry.getKey(); // this is your key - the id of the book 
     Book book = bookEntry.getValue(); // this is the book with its properties 

     //set the title to display 
     title_label.setText(book.getTitle().toString()); 
     } 
    }  
} 

は、データとアクセスのみ1行にボタンが押されるたびに取得するための良い方法はありますか?

EDIT - 答え に基づき、これは理想的である:

public class Book { 

    private String title; 
    private String ISBN; 
    private String description; 
    Private String image; 

    public Book(String title, String ISBN, String Subject, String description, String image){ 
     this.title = title; 
     this.ISBN = ISBN; 
     this.subject = subject; 
     this.Description = descritption; 
     this.image = image; 
    } 
} 
+0

コードについてのヘルプは必要ですか? – Assafs

+0

@Assafs私はデータを反復して抽出するために多くの方法を試しましたが、何か間違っていなければなりません...私はTreeMap を作成しました。 TreeMapをトラバースしてブックオブジェクトの値をトラバースするには、ネストされた 'each for'ループが必要ですか? – ArcherGilly

+0

正確ではありません。質問を更新し、あなたが書いた新しいコードを追加すると、おそらく私は助けることができますか? – Assafs

答えて

0

私はあなたが望む属性としたいプレゼンテーションと、ブックを表す新しいクラスを作成します。

書籍をマップブックとして保存することができます(ブックごとに一意であると仮定して、識別子としてisbnを使用します)。

次に、本が存在するかどうかを効率的にチェックし、リストを繰り返して次の本を簡単に表示できます。ボタンのプレスをマップエントリのiterator.next()に接続するだけで、次の本。

+0

私は編集を追加しました。 – ArcherGilly

+0

@ArcherGillyはい、正確です。 ArrayList >をMap またはArrayList のいずれかに置き換えることができます。それはあなたのコードをはるかに管理しやすくなります。 – Assafs

関連する問題