将数据库数据导入TableView

时间:2013-10-02 06:53:18

标签: java sql javafx tableview

我在以下代码处获得了错误突出显示:

ResultSet rs = stmt.executeQuery(sql);

'executeQuery'在netbeans中获得“红色错误突出显示”

如何使其正常工作并使应用程序正常工作,并使用数据库中的数据填充JavaFx TableView。

以下是本守则的其余部分:

控制器类:

import java.beans.Statement;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class TesterUIController implements Initializable {

    static String JDBC_DRIVER = "org.h2.Driver";
    static String DB_URL = "jdbc:h2:file:C:/MyBeautifulCherrishabledb";

    static final String USER = "sa";
    static final String PASS = "";
    public static Connection conn = null;

    @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");
    }

    @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());

            }
        });
    }

    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() {
        Statement stmt = null;
        List ll = new LinkedList();

        try {
            // STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            // STEP 3: Open a connection
            System.out.println("Connecting to a selected database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            System.out.println("Connected database successfully...");

            // STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = (Statement) conn.createStatement();

            String sql = "SELECT id, LovelyGuy FROM LOVELYPEOPLE";
            ResultSet rs = stmt.executeQuery(sql);

            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();
                roww.setKey(Key);
                roww.setSalesNo(SalesNo);
                ll.add(roww);

            }
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(TesterUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ll;
    }
}

主要类

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TesterUI extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("TesterUI.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXML:

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

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

<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="700.0" xmlns:fx="http://javafx.com/fxml" fx:controller="testerui.TesterUIController">
  <children>
    <Button fx:id="TestButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="Button" />
    <Pane layoutY="35.0" prefHeight="565.0" prefWidth="700.0">
      <children>
        <TableView fx:id="Table" layoutX="14.0" layoutY="14.0" prefHeight="226.0" prefWidth="672.0">
          <columns>
            <TableColumn prefWidth="75.0" text="Column X" fx:id="colKey" />
            <TableColumn prefWidth="75.0" text="Column X" fx:id="colSalesNo" />
          </columns>
        </TableView>
      </children>
    </Pane>
  </children>
</AnchorPane>

更新

ErrorStackTrace:

ResultSet rs = stmt.executeQuery(sql);
  symbol:   method executeQuery(String)
  location: variable stmt of type Statement
Note: C:\Users\Revilo\Downloads\GitHub\Tables\Tablas-JavaFX--FXML--master\src\TesterUI\src\testerui\TesterUIController.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

1 个答案:

答案 0 :(得分:0)

您已导入

import java.beans.Statement;

你应该使用

import java.sql.Statement;