我在javaFX中开发了表格视图,该表格视图从数据库中检索数据并显示,但是它只显示价格列中的双精度值,我在此处附加了代码。
public class stockViewController implements Initializable{
@FXML
private TextField search_txt;
@FXML
private TableView<stock> tableView;
@FXML
private TableColumn<stock, String> name12;
@FXML
private TableColumn<stock, String> categ12;
@FXML
private TableColumn<stock, Double> price12;
@FXML
private TableColumn<stock, Integer> qty12;
@FXML
private TableColumn<stock, Integer> bill12;
@FXML
private Button addNew;
@FXML
private Button additem;
@FXML
void additems(ActionEvent event) throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/stockMain.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
}
@FXML
void viewCategory(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/pieChart.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
}
@FXML
void stockmain(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/stockMain.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
}
//new one
ObservableList<stock>filltable = FXCollections.observableArrayList();
public void fillTableValues(){
try {
Connection con = null;
PreparedStatement preparedStatement = null;
ResultSet rs =null;
con = DBConnection.getDBConnection();
stock st1 = new stock();
String query = "select * from stock3";
preparedStatement = con.prepareStatement(query);
rs = preparedStatement.executeQuery();
while(rs.next()){
int bill12 = rs.getInt("bill");
String categ12 = rs.getString("categ");
String name12 = rs.getString("name");
int qty12 = rs.getInt("qty");
Double price12 = rs.getDouble("price");
stock st2= new stock();
st2.setbill(bill12);
st2.setcategory(categ12);
st2.setname(name12);
st2.setqty(qty12);
st2.setPrice(price12);
filltable.add(new stock(bill12,categ12,name12,qty12,price12));
tableView.setItems(filltable);
// tableView.setItems(st2.getbill(),st2.getcategory(),st2.getname(),st2.getqty(),st2.getPrice());
System.out.println(bill12);
System.out.println(categ12);
System.out.println(qty12);
System.out.println(price12);
//System.out.println(st2);
}
} catch (SQLException ex) {
Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(StockFinalFinal.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void onSearch(ActionEvent event) {
Connection con = null;
PreparedStatement preparedStatement = null;
ResultSet rs =null;
if(search_txt.getText().equals("")){
fillTableValues();
JOptionPane.showMessageDialog(null, "Error! No value searched!!");
}else{
try {
filltable.clear();
con = DBConnection.getDBConnection();
String searchquery = "SELECT * from stock3 where price LIKE '%"+search_txt.getText()+"%'";
preparedStatement = con.prepareStatement(searchquery);
//preparedStatement.setString(1,search_txt.getText());
rs = preparedStatement.executeQuery();
//preparedStatement = con.prepareStatement(searchquery);
if(rs.next()){
filltable.add(new stock(rs.getInt(2),rs.getString(4)," "+rs.getString(5),rs.getInt(6),rs.getDouble(7)));
}
tableView.setItems(filltable);
} catch (ClassNotFoundException ex) {
Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
bill12.setCellValueFactory(new PropertyValueFactory<>("categ"));
name12.setCellValueFactory(new PropertyValueFactory<>("categ"));
categ12.setCellValueFactory(new PropertyValueFactory<>("name"));
qty12.setCellValueFactory(new PropertyValueFactory<>("qty"));
price12.setCellValueFactory(new PropertyValueFactory<>("price"));
fillTableValues();
}
}
这仅显示表列价格,但我想显示包括String
(categ12
,name12
)和Integer
(qty12
,bill12
)。那我该怎么办,我应该改变什么?