B
B
barmatograf30002019-02-19 16:52:08
Java
barmatograf3000, 2019-02-19 16:52:08

Why can't the compiled .exe connect to SQLite?

I am writing a project in java using javafx in intellij idea. The problem is that when run from ide, the program reads / writes to the database and everything is fine, there are no errors / warnings. But the compiled .exe refuses to work.
The DATA.db file is located at the root of drive D.
It is opened and edited through the DB Browser (SQLite)
The table in the database is not empty. But .exe refuses to read it in the updateListDatabase() method.
I would be grateful for the help/hint.
My class:

package sample;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

import java.io.IOException;
import java.sql.*;

public class Controller {

    public static Stage stage1, stage2;
    public static String dbPath;
    private boolean newDatabaseStatus = createDatabase.newDatabaseStatus;

    private Statement statement;
    private Connection connection;

    @FXML
    private ListView<String> listDB;

    public void initialize() throws ClassNotFoundException, SQLException, IOException {
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:D:/DATA.db");
            statement = connection.createStatement();
            statement.execute("CREATE TABLE if not exists 'databaselist' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' text, 'path' text);");


            updateListDatabase();

            System.out.println("Таблица выведена");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Thread myThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    Platform.runLater(new Runnable() {
                        @Override public void run() {
                            newDatabaseStatus = createDatabase.newDatabaseStatus;
                            if(newDatabaseStatus == true){
                                createDatabase.newDatabaseStatus = false;
                                try {
                                    updateListDatabase();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }
        });
        myThread.start();

    }

    @FXML
    private void addDatabaseToList(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("createDatabase.fxml"));
            Parent root1 = fxmlLoader.load();
            stage1 = new Stage();
            stage1.setScene(new Scene(root1, 456, 150));
            stage1.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void openDatabase() {
            System.out.println(listDB.getSelectionModel().getSelectedItem());
            String path = listDB.getSelectionModel().getSelectedItem().substring(listDB.getSelectionModel().getSelectedItem().indexOf('-') + 2);
            dbPath = "jdbc:sqlite:" + path.replace('\\', '/');
            System.out.println(dbPath);

            try {
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("databaseView.fxml"));
                Parent root1 = fxmlLoader.load();
                stage2 = new Stage();
                stage2.setTitle(listDB.getSelectionModel().getSelectedItem());
                stage2.setScene(new Scene(root1, 1200, 800));
                stage2.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public void updateListDatabase() throws SQLException {
        listDB.getItems().clear();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM databaselist");

        while(resultSet.next())
        {
            int id = resultSet.getInt("id");
            String  name = resultSet.getString("name");
            String  path = resultSet.getString("path");

            listDB.getItems().add(name + " - " + path.replace("jdbc:sqlite:", "").replace("/", "\\"));
        }
    }
}

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <ListView fx:id="listDB" layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0" />
      <Button layoutX="14.0" layoutY="230.0" mnemonicParsing="false" onAction="#addDatabaseToList" prefHeight="20.0" prefWidth="150.0" text="Новая база данных" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="220.0" />
      <Button onAction="#openDatabase" layoutX="10.0" layoutY="259.0" mnemonicParsing="false" prefHeight="20.0" prefWidth="150.0" text="Открыть" AnchorPane.topAnchor="260.0" />
      <Button layoutX="10.0" layoutY="301.0" mnemonicParsing="false" prefHeight="20.0" prefWidth="150.0" text="SQL" AnchorPane.topAnchor="300.0" />
      <Button layoutX="10.0" layoutY="339.0" mnemonicParsing="false" prefHeight="20.0" prefWidth="150.0" text="Формы" AnchorPane.topAnchor="340.0" />
   </children>
</AnchorPane>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-02-19
@tsklab

"jdbc:sqlite:D:/DATA.db"
"jdbc:sqlite:D:\DATA.db"

R
rPman, 2019-02-20
@rPman

Check whether you are running the application from a network or webdaw drive or from a local drive (a connected vhd from a network location is also considered a local drive), especially if you use javafxpackager or analogues that wrap a java virtual machine in an exe application. Launching with a java - jar is considered local (and the debugger does so), since the exe application is a java virtual machine, and you most likely have it installed locally.
When running applications over the network, separate policy settings and file access apply, incl. over the net, especially I met a lot of problems when using .net compiled applications (not your case, but the problems are similar).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question