JavaFX ComboBox SQLite数据库错误

时间:2016-08-18 00:27:50

标签: sqlite javafx combobox

我创建了一个简单的ComboBox,我试图用sqlite文件填充数据。我得到的错误是说它无法找到该文件或者找不到该表,所以我不确定它们是怎么回事。 sql文件位于我的项目文件夹中,所以我不认为这个问题和我的表看起来很好,所以不确定它是否也是。

//错误日志

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: CustomerPriceList)
Error Building ComboBox Data
    at org.sqlite.core.DB.newSQLException(DB.java:890)
    at org.sqlite.core.DB.newSQLException(DB.java:901)
    at org.sqlite.core.DB.throwex(DB.java:868)
    at org.sqlite.core.NativeDB.prepare(Native Method)
    at org.sqlite.core.DB.prepare(DB.java:211)
    at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:81)
    at supremeinkcalcmk2.MainController.buildData(MainController.java:87)
    at supremeinkcalcmk2.MainController.initialize(MainController.java:55)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at supremeinkcalcmk2.SupremeInkCalcMk2.start(SupremeInkCalcMk2.java:33)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/726585699.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1149823713.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

// SqlConnection的

package supremeinkcalcmk2;

import java.sql.Connection;
import java.sql.DriverManager;

public class SqlConnection {

    public static Connection CustomerConnection() {
        try {
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:CustomerSQLDB.sqlite");
            return conn;
        } catch (Exception e) {
            return null;
        }

    }
}

// MainController

package supremeinkcalcmk2;

import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.swing.DefaultComboBoxModel;

public class MainController implements Initializable {

    @FXML
    public ComboBox<String> ComboBoxSelectCustomer;
    Connection connection;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        buildData();


    }

    //combobox sql connection
    public void buildData() {
        ObservableList<String> data = FXCollections.observableArrayList();
        connection = SqlConnection.CustomerConnection();
        try {
            String SQL = "Select Name From CustomerPriceList";
            ResultSet rs = connection.createStatement().executeQuery(SQL);
            while(rs.next()){
                data.add(rs.getString("Name"));
            }
            ComboBoxSelectCustomer.setItems(data);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error Building ComboBox Data");
        }
        if (connection == null) {
            System.exit(1);
            System.out.println("Connection failed");
        }
    }

    public boolean isDbConnected() {
        try {
            return connection.isClosed();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

}

// Main.FXML

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="supremeinkcalcmk2.MainController">
   <left>
      <VBox prefHeight="400.0" prefWidth="152.0" BorderPane.alignment="CENTER">
         <children>
            <TableView prefHeight="578.0" prefWidth="152.0">
              <columns>
                <TableColumn editable="false" prefWidth="75.0" sortable="false" text="Formla" />
                <TableColumn editable="false" prefWidth="75.0" sortable="false" text="Price" />
              </columns>
            </TableView>
            <Button fx:id="ButtonNewPantone" mnemonicParsing="false" prefHeight="25.0" prefWidth="155.0" text="Add New Pantone" />
         </children>
      </VBox>
   </left>
   <right>
      <VBox prefHeight="400.0" prefWidth="152.0" BorderPane.alignment="CENTER">
         <children>
            <ComboBox fx:id="ComboBoxSelectCustomer" prefWidth="150.0" promptText="Select Customer" />
            <TableView fx:id="CustomerTableView" prefHeight="557.0" prefWidth="152.0">
              <columns>
                <TableColumn fx:id="BaseColor" prefWidth="75.0" sortable="false" text="Base Color" />
                <TableColumn fx:id="Price" editable="true" prefWidth="75.0" sortable="false" text="Price" />
              </columns>
            </TableView>
            <Button fx:id="ButtonSaveCustomer" mnemonicParsing="false" prefHeight="25.0" prefWidth="152.0" text="Save Customer" />
         </children>
      </VBox>
   </right>
   <center>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Label layoutX="203.0" layoutY="193.0" text="Pantone Number" />
            <TextField layoutX="173.0" layoutY="220.0" />
            <Label fx:id="PriceLabel" layoutX="132.0" layoutY="293.0" />
            <Button fx:id="ButtonCalculate" layoutX="216.0" layoutY="290.0" mnemonicParsing="false" onAction="#CalculateButton" text="Calculate" />
            <Label layoutX="234.0" layoutY="446.0" text="Label" />
         </children>
      </Pane>
   </center>
</BorderPane>

//数据库

CREATE TABLE `CustomerPriceList` (
    `Name`  TEXT NOT NULL UNIQUE,
    `Test`  INTEGER,
    PRIMARY KEY(Name)
)

1 个答案:

答案 0 :(得分:0)

问题是sql文件的扩展名是.db而不是.sqilite

Sqlconnection.java文件中的正确格式:

Connection conn = DriverManager.getConnection("jdbc:sqlite:CustomerSQLDB.db");