如何从mysql数据库填充我的表视图javafx

时间:2016-04-29 04:46:09

标签: javafx

我一直在寻找答案,但我仍然无法找到正确的方法(或者可能是我的代码中的错误)。我正在使用javafx和scenebuilder。 mySQL是我正在使用的数据库。请帮助:(

package cedproject;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import java.sql.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;


/**
 * FXML Controller class
 *
 * @author My Lightstream
 */
public class FXMLController implements Initializable {
 public java.sql.PreparedStatement ps;
 public java.sql.ResultSet rs;
    @FXML
    private AnchorPane donationWindow;
    //@FXML
    //private TableView<?> tblView;

    @FXML
    TableView<userdata> tblView= new TableView<>();
    @FXML
    private TableColumn<?, ?> colOR;
    @FXML
    private TableColumn<?, ?> colAmount;
    @FXML
    private Button btnNew;
    @FXML
    private TextField txtSearch;
    private static java.sql.Connection con;

    private static Statement stat;

    private PreparedStatement prep;

    private ObservableList<userdata> data;
    @FXML
    private Button btnLoad;
    @FXML
    private Group gp;

   // </userdata>

/**
    @FXML
    private Button btnNew;
    @FXML
    private AnchorPane donationWindow;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

         try{
            ps = new Connect().connectDatabase();

            ps.execute();

            tblView.getItems().setAll(this.data);
        }catch(Exception e){  
        }

        lagay();
    }

    public void lagay(){

       try{ 
           String query = "select * from receipt";

            tblView.getItems().clear();

            ps = con.prepareStatement("select * from receipt");

            rs = ps.executeQuery(query);

       }catch(Exception e){
           System.out.println("mali ka shunga");
       }

        ObservableList<userdata> data = FXCollections.observableArrayList();

            try{

            while(rs.next()){

                data.add(new userdata(

                        rs.getInt("ORNUM"),
                        rs.getInt("AMOUNT")
                    ));}
            }catch(Exception e){}
    }
    public void tableView()throws Exception{
        tblView.getItems().clear();

        rs = ps.executeQuery("SELECT ORNUM,AMOUNT FROM RECEIPT");

        ObservableList<userdata> data = FXCollections.observableArrayList();

        TableColumn column1 = new TableColumn("ORNUM");

        column1.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("ornum"));


        TableColumn column2 = new TableColumn("Amount");

        column2.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("amount"));


        tblView.getColumns().addAll(column1,column2);

        tblView.getChildrenUnmodifiable();
    }    

    @FXML
    public void handleButtonAction2(ActionEvent event) {

    }

    @FXML
    public void activated(MouseEvent event) {

    }

    private void onClicked(ActionEvent event) throws SQLException {
      /*  tblView.getItems().clear();
        rs = ps.executeQuery("SELECT ORNUM,AMOUNT FROM RECEIPT");
        ObservableList<userdata> data = FXCollections.observableArrayList();
        TableColumn column1 = new TableColumn("ORNUM");
        column1.setCellValueFactory(new PropertyValueFactory<>("ORNUM"));

        TableColumn column2 = new TableColumn("Amount");
        column2.setCellValueFactory(new PropertyValueFactory<>("Amount"));

        tblView.getColumns().addAll(column1,column2);*/
     ObservableList<userdata> data = FXCollections.observableArrayList();
     try{ String query = "select * from receipt";
            tblView.getItems().clear();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery("SELECT * from receipt");
           while(rs.next()){
                tblView.getItems().add(new userdata(
                        rs.getInt("ORNUM"),
                        rs.getInt("AMOUNT")
                    ));
           tblView.setItems(data);
           }
           ps.close();
           rs.close();
       }catch(Exception e){

       }

    }

    @FXML
    private void onClicked(MouseEvent event) throws Exception {
     System.out.println("gawin mo");
     ObservableList<userdata> data = FXCollections.observableArrayList();
     System.out.println("ginawa");
     tableView();
     try{   

            String query = "select * from receipt";

            tblView.getItems().clear();

            ps = new Connect().connectDatabase();

            ps = new Connect().connectDatabase();

            rs = ps.executeQuery(query);

           while(rs.next()){

                tblView.getItems().add(new userdata(
                        rs.getInt("ORNUM"),
                        rs.getInt("AMOUNT")
                    ));


               tblView.setItems(data);
           }

       }catch(Exception e){
           System.out.print("asdqweasd");

       }
    }


    /**
     *
     * @param event
     */
   /* @FXML
    public void konekplis(SortEvent<C> event) {

    }*/


}
package cedproject;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
 *enter code here
 * @author My Lightstream
 */
public class userdata {
     public SimpleIntegerProperty Ornum;
     public SimpleIntegerProperty Amount;

        public userdata(Integer ornum, Integer amount) {
          this.Ornum = new SimpleIntegerProperty(ornum);
            this.Amount = new SimpleIntegerProperty(amount);
       }

    userdata(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    userdata(String string, String string0, String string1, String string2, String string3, String string4) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


        public Integer getOrnum() {
            return Ornum.get();
        }

        public Integer getAmount() {
            return Amount.get();
        }


        public void setOrnum(Integer ornum) {

            this.Ornum.set(ornum);
        }

        public void setAmount(Integer amount) {
           this.Amount.set(amount);
        }

}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.effect.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" fx:id="donationWindow" onDragDetected="#activated" prefHeight="482.0" prefWidth="696.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cedproject.FXMLController">
    <stylesheets>
        <URL value="@fxml.css" />
    </stylesheets>
   <children>
      <AnchorPane layoutY="8.0" prefHeight="482.0" prefWidth="696.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <TableView fx:id="tblView" layoutX="21.0" layoutY="87.0" prefHeight="181.0" prefWidth="655.0">
              <columns>
                <TableColumn fx:id="colOR" prefWidth="326.0" text="OR Number" />
                <TableColumn fx:id="colAmount" prefWidth="325.0" text="Amount" />
              </columns>
            </TableView>
            <Button fx:id="btnNew" layoutX="500.0" layoutY="35.0" mnemonicParsing="false" onAction="#handleButtonAction2" prefHeight="25.0" prefWidth="78.0" text="NEW" />
            <Button layoutX="587.0" layoutY="35.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="78.0" text="EDIT" />
            <TextField fx:id="txtSearch" layoutX="40.0" layoutY="35.0" prefHeight="25.0" prefWidth="204.0" promptText="Search" />
            <TextArea editable="false" layoutX="19.0" layoutY="336.0" prefHeight="132.0" prefWidth="655.0">
               <effect>
                  <DropShadow height="14.0" radius="6.5" width="14.0" />
               </effect>
            </TextArea>
            <Label layoutX="26.0" layoutY="305.0" prefHeight="17.0" prefWidth="68.0" text="Description:" />
            <Button fx:id="btnLoad" layoutX="424.0" layoutY="35.0" mnemonicParsing="false" onMouseClicked="#onClicked" prefHeight="25.0" prefWidth="68.0" text="Load" />
            <Group fx:id="gp" />
         </children>
         <cursor>
            <Cursor fx:constant="DEFAULT" />
         </cursor>
      </AnchorPane>
   </children>
</AnchorPane>

请帮助:(

0 个答案:

没有答案