0

次のコードでエラーが強調表示されます。

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>

アップデート:

エラースタックトレース:

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
4

1 に答える 1

1

あなたが輸入した

import java.beans.Statement;

使用する必要があります

import java.sql.Statement;
于 2013-10-02T08:08:15.737 に答える