将sqlite数据库添加到jar时出现sql错误或缺少数据库

Posted

技术标签:

【中文标题】将sqlite数据库添加到jar时出现sql错误或缺少数据库【英文标题】:Sql error or missing database when adding sqlite database to jar 【发布时间】:2018-01-14 11:00:12 【问题描述】:

我正在尝试打包一个包含 sqlite 数据库的 jar 文件,以便我的应用程序在另一台计算机上运行。打包应用程序后,我尝试在另一台计算机上运行它并抛出此错误:

我想创建一个 exe 文件或一个 jar 文件,以便我可以将文件提供给其他用户,以便在这些计算机上运行。

我使用 sql DBbrowser 在外部创建了我的数据库,并将其添加到我的应用程序的资源文件夹中。

SQL 错误或缺少数据库(没有这样的表:用户)

这些是我数据库中的表:

这是我的代码:

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

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

public class DBConnection 
    private static Connection conn;
    private static String url = "jdbc:sqlite:mydb.db";
    private static String user = "root";//Username of database
    private static String pass = "yes";//Password of database

    public static Connection connect() throws SQLException 
        try 
            Class.forName("org.sqlite.JDBC").newInstance();
         catch (ClassNotFoundException cnfe) 
            System.err.println("Error: " + cnfe.getMessage());
         catch (InstantiationException ie) 
            System.err.println("Error: " + ie.getMessage());
         catch (IllegalAccessException iae) 
            System.err.println("Error: " + iae.getMessage());
        
        conn = DriverManager.getConnection(url);
        System.out.println("CONNECTED!!");
        return conn;
    

    public static Connection getConnection() throws SQLException, ClassNotFoundException 
        if (conn != null && !conn.isClosed())
            return conn;
        connect();
        return conn;
    

    public static void infoBox(String infoMessage, String titleBar, String headerMessage) 

        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle(titleBar);
        alert.setHeaderText(headerMessage);
        alert.setContentText(infoMessage);
        alert.showAndWait();
    

这是我访问名为 users 的表的地方:

import java.sql.ResultSet;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import static pkg3treka.DBConnection.getConnection;

public class FXMLDocumentController implements Initializable 


@FXML
private Button button;

@FXML
private Label label;

@FXML
private TextField textField;

@FXML
private TextField textPassword;


Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;



public FXMLDocumentController() throws SQLException 
    try 

       connection = DBConnection.getConnection();
     catch (ClassNotFoundException ex) 
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
    



@FXML
void handleButtonAction(ActionEvent event) 
      boolean isAdmin = false;
    String username = textField.getText();
    String password = textPassword.getText();

    if( username.length()>=6 && "admin/".equals(username.substring(0,6)))
        isAdmin = true;
        username = textField.getText().substring(6);
        System.out.println("---->"+username);
    


    String sql = "SELECT * FROM users WHERE U_username = ? and U_password = ?";

try

    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, username);
    preparedStatement.setString(2, password);
    resultSet = (ResultSet) preparedStatement.executeQuery();

    if(!resultSet.next())
    DBConnection.infoBox("Enter Correct Email and Password", "Failed", null);
      else
    try  


    Stage stage = (Stage) button.getScene().getWindow();
    stage.close();
    FXMLLoader fxmlLoader = new FXMLLoader();  

    if(isAdmin)

        fxmlLoader.setLocation(getClass().getResource("ChooseRouteToOpen.fxml"));
    else

        fxmlLoader.setLocation(getClass().getResource("UserTimeSheetsView.fxml"));
        
    Scene scene = new Scene(fxmlLoader.load());//next page size
    stage.setScene(scene);
    stage.show();

 catch (IOException e) 
    Logger logger = Logger.getLogger(getClass().getName());
    logger.log(Level.SEVERE, "Failed to create new Window.", e);





catch(Exception e)
e.printStackTrace();







@Override
public void initialize(URL url, ResourceBundle rb) 
    try 
        // TODO
        if(DBConnection.getConnection() == null)
            label.setText("Status: DATABASE NOT CONNECTED!");
        
        else 
            label.setText("Status: DATABASE CONNECTED!");
        
     catch (SQLException ex) 
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
     catch (ClassNotFoundException ex) 
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
    
    ;

    


这是错误日志:

Jan 14, 2018 12:31:04 PM javafx.fxml.FXMLLoader$ValueElement 

processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.66
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: users)
    at org.sqlite.core.DB.newSQLException(DB.java:909)
    at org.sqlite.core.DB.newSQLException(DB.java:921)
    at org.sqlite.core.DB.throwex(DB.java:886)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
    at org.sqlite.core.DB.prepare(DB.java:227)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:41)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:48)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:263)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:235)
    at pkg3treka.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: users)
    at org.sqlite.core.DB.newSQLException(DB.java:909)
    at org.sqlite.core.DB.newSQLException(DB.java:921)
    at org.sqlite.core.DB.throwex(DB.java:886)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
    at org.sqlite.core.DB.prepare(DB.java:227)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:41)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:48)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:263)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:235)
    at pkg3treka.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

【问题讨论】:

你能分享完整的代码吗? 【参考方案1】:

在第 117 行的 FXMLDocumentController.java 文件中,您正在对名为“users”的 SQL 表运行查询。您的数据库中缺少此表。

【讨论】:

数据库中有我检查过的表 不,您使用“mydb.db”作为数据库而没有设置完整路径,如果它不存在,SQLite 将创建一个空数据库。这就是为什么桌子不见了。检查你正在运行你的应用程序的patu,你会发现空数据库。 PS:当 SQLite 说 "no such table: users" 时,表 users 丢失。没有魔法。 我使用 sqlDB Browser 创建了一个数据库并将其添加到资源文件中。 使用“jdbc:sqlite:mydb.db”将在您运行“java”的目录中打开/创建“mydb.db”文件,它不会神奇地从其他地方加载数据库。 有道理那么我如何在资源文件夹中引用我的数据库。

以上是关于将sqlite数据库添加到jar时出现sql错误或缺少数据库的主要内容,如果未能解决你的问题,请参考以下文章

添加ORDER BY时出现“SQL0802 - 数据映射错误的数据转换”异常

滚动时将新项目添加到列表时出现数组索引超出范围异常

Netbeans 连接到 SQLite 时出现 SQLite 错误或缺少数据库(没有这样的表)

ORA-06502: PL/SQL: 尝试将 XML 保存到文件时出现数字或值错误

vs添加数据库连接 在与sql server 建立连接时出现与网络相关或特定于实例的错误 40-无法打开到Sql server的连接

vs添加数据库连接 在与sql server 建立连接时出现与网络相关或特定于实例的错误 40-无法打开到Sql server的连接