无法使用java.sql连接到java中的sqlite数据库

问题描述 投票:0回答:1

我正在尝试建立与 SQLite 数据库的连接。但是,在尝试运行该文件时,我总是遇到相同的异常。这是我的代码:

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


/* Disclaimer:
 *This is just a snippet of my code and does not represent the whole sourcecode itself. 
 *It shows only the relevant part of the whole code.  
 */
private void ideaconvertion(String idea) throws SQLException {
    String url; 
    url = "C:/Users/user/eclipse-workspace/ContentCreationToolYT/src/Ideadatabase.db";
    Connection connection1 = null;
    try {
        connection1 = DriverManager.getConnection(url);
        String sql1 = "INSERT INTO Ideas (spaltenname) VALUES (?)";
        Statement statement1 = connection1.createStatement();
        statement1.executeQuery(sql1);
    } catch (SQLException e) {
        e.printStackTrace();
        label2.setText("Error with connecting to the idea database");
    }
}

这是我尝试执行我的方法时总是发生的异常:

java.sql.SQLException: No suitable driver found for C:/Users/user/eclipse-workspace/ContentCreationToolYT/src/Ideadatabase.db
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
    at Project/contentcreationtool.fx.MainProgram.ideaconvertion(MainProgram.java:55)
    at Project/contentcreationtool.fx.MainProgram.lambda$0(MainProgram.java:43)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
    at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
    at [email protected]/javafx.scene.Node.fireEvent(Node.java:8875)
    at [email protected]/javafx.scene.control.Button.fire(Button.java:203)
    at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
    at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
    at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
    at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3984)
    at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
    at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2708)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:833)

向 JDBC JAR 类路径提供信息也很重要:

我使用 sqlite-jdbc-3.44.1.0.jar 及其在库类路径中的项目中配置。我将其添加为外部 JAR。

经过研究,我尝试了一些想法。例如,我尝试将 URL 更改为给定的数据库。

  1. jdbc:sqlite:D:\\\\db\\\\Ideadatabase.db
  2. jdbc:sqlite:Ideadatabase.db
java database jdbc classpath
1个回答
0
投票

您需要使用 url 来指定驱动程序中的 sqlite,如 Mark Rotteveel 所评论。验证数据库文件路径的一个简单方法是添加 .toRealPath() ,如下所示:

Path db = Path.of(whatever);
String url = "jdbc:sqlite:"+db.toRealPath();
© www.soinside.com 2019 - 2024. All rights reserved.