私は、タイトル、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;
}
}
コードについてのヘルプは必要ですか? – Assafs
@Assafs私はデータを反復して抽出するために多くの方法を試しましたが、何か間違っていなければなりません...私はTreeMapを作成しました。 TreeMapをトラバースしてブックオブジェクトの値をトラバースするには、ネストされた 'each for'ループが必要ですか? –
ArcherGilly
正確ではありません。質問を更新し、あなたが書いた新しいコードを追加すると、おそらく私は助けることができますか? – Assafs