0

I get an error highlight with this code at:

ResultSet rs = stmt.executeQuery(sql);

'executeQuery' gets "red error highlight" in netbeans

How do I get it to work properly and get the application to work and populate the JavaFx TableView with data from the database.

Here's the rest of the Code:

The Controller Class:

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;
    }
}

The main Class

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

The 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>

Update:

The 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
2
  • What does this ['executeQuery' gets "red error highlight" in netbeans] mean? That Netbeans IDE cannot compile your code? Commented Oct 2, 2013 at 7:25
  • Hey @ahaaman. Thanks for looking at my question. Just added the error message. I think my code won't compile because of the way I coded 'executeQuery'. That's what I meant. Commented Oct 2, 2013 at 7:30

1 Answer 1

1

you have imported

import java.beans.Statement;

You should use

import java.sql.Statement;
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @ahaaman. Thanks man. Now the code runs! But, the database data does not get printed in the TableView. Any ideas?
Thanks for the reply ahaaman. I made the other part work. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.