我正在使用JavaFX創建一個基本的GUI。我有一個在MySQL中創建的數據庫來存儲數據。 (數據庫連接)(連接器/ J)引用ResultSet /使用Next()方法
這是我在連接兩個,以及使用結果集的第一個嘗試/ DBCONNECT
目前,我有3類:我的遊戲類,我GameUI類(主)和我的DBConnect類。
我正在嘗試引用最初在Game類中聲明的GameUI類中的ResultSet。
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) {
}
}
我的用戶自定義的MoveNext()方法:
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;
}
的GameUI類我試圖引用它:
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;
}
我的問題是,我可以在我的nextButton
事件處理程序(我的GameUI類)中引用我的ResultSet(遊戲類),還是必須聲明一個ne w結果集?
這是我的用戶定義的moveNext()
方法的正確位置,還是應該使用next()
?
我會根據要求發佈更多代碼。
您的文章包括三個不同的問題。將它們作爲單獨的問題會更好。 – hotzst