好的,所以我是新手,我已经搜索了两个星期,试图让 SQL 结果集打印到由 JavaFX Scene Builder 制作的 TableView。我可以使用 System.out.println 打印我的结果集并且数据是正确的,但是当我尝试将其放入列表时,我的表格视图显示一列带有 0,另一列空白。我正在尝试打印列表(println),但我得到了对象指针(?)而不是数据,所以我看不到我是否错误地从 ResultSet 传递到列表。
任何帮助将不胜感激......我想了解这个概念,而不仅仅是得到答案。
这是我的控制器代码:
public class TesterUIController implements Initializable {
@FXML
private TableView<dataClass> Table;
@FXML
private Button TestButton;
@FXML
private TableColumn<dataClass, Integer> colKey;
@FXML
private TableColumn<dataClass, String> colSalesNo;
public static void main (String[] args) {
System.out.println("Main has run");
}
/**
*
* @param fxmlFileLocation
* @param resources
*/
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
TestButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Button Pressed");
colKey.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("Key"));
colSalesNo.setCellValueFactory(new PropertyValueFactory<dataClass, String>("SalesNo"));
Table.getItems().setAll(gobbledyGook());
}
} );
}
/*
* dataClass is a (sub)class representing the data for TableView "Table"
*/
public class dataClass {
private IntegerProperty Key;
public void setKey (int value) {KeyProperty().set(value);}
public int getKey() {
return KeyProperty().get();
}
public IntegerProperty KeyProperty() {
if (Key == null) Key = new SimpleIntegerProperty(this, "Key");
return Key;
}
private StringProperty SalesNo;
public void setSalesNo(String value) {
SalesNoProperty().set(value);
}
public String getSalesNo() {
return SalesNoProperty().get();
}
public StringProperty SalesNoProperty() {
if (SalesNo == null) SalesNo = new SimpleStringProperty(this, "SalesNo");
return SalesNo;
}
}
private List<dataClass> gobbledyGook() { //ObservableList<dataClass> gobbledyGook() { // ResultSet getData() {
Connection conn;
String dbDriver;
Statement st;
ResultSet rs;// = null;
List ll = new LinkedList();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
+ "DBQ=Inventario.mdb;";
conn = DriverManager.getConnection(dbDriver, "", "");
st = conn.createStatement();
String getSalesNumber = "SELECT * FROM SalesNumber " // TOP 1 * FROM ...
+ "ORDER BY SalesNumber.Key DESC";// LIMIT 1";
rs = st.executeQuery(getSalesNumber);
while (rs.next()) {
int Key = rs.getInt(1);
double saleNo = rs.getDouble(2);
NumberFormat formatter = new DecimalFormat("###########");
String SalesNo = formatter.format(saleNo);
System.out.println(Key + ", " + SalesNo); //key + ", " + saleNo);
dataClass roww = new dataClass();
ll.add(roww);
}
}
catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(TesterUIController.class.getName()).log(Level.SEVERE, null, ex);
}
return ll;
}
}