连接到数据源 SQLite 失败

Posted

技术标签:

【中文标题】连接到数据源 SQLite 失败【英文标题】:Connection to Data Source SQLite failed 【发布时间】:2014-08-08 13:38:57 【问题描述】:

我使用ormlitexerial sqlite jdbc 驱动程序sqlite-jdbc-3.7.2.jar 并在C:\work\ 中有空文件sample.db,但我无法连接到sqlite 数据库。我的数据库网址是jdbc://jdbc:sqlite:C:/work/sample.db

错误:

Connection to Data Source SQLite failed
java.sql.SQLException: invalid database address: jdbc://jdbc:sqlite:C:/work/sample.db
    at org.sqlite.JDBC.createConnection(JDBC.java:74)
    at org.sqlite.JDBC.connect(JDBC.java:64)
    at com.intellij.persistence.jdbc.impl.RemoteDriverImpl.connect(RemoteDriverImpl.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy154.connect(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.execution.rmi.RemoteUtil$RemoteInvocationHandler.invoke(RemoteUtil.java:211)
    at $Proxy155.connect(Unknown Source)
    at com.intellij.javaee.module.view.dataSource.LocalDataSource.getConnection(LocalDataSource.java:163)
    at com.intellij.javaee.module.view.dataSource.LocalDataSource.getConnection(LocalDataSource.java:140)
    at com.intellij.javaee.module.view.dataSource.LocalDataSource.getConnection(LocalDataSource.java:133)
    at com.intellij.javaee.dataSource.DataSource.performJdbcOperation(DataSource.java:288)
    at com.intellij.javaee.dataSource.AbstractDataSource.refreshMetaData(AbstractDataSource.java:25)
    at com.intellij.javaee.module.view.dataSource.DataSourceUiUtil$3.run(DataSourceUiUtil.java:140)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:495)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$6.run(ProgressManagerImpl.java:304)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:185)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:226)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:175)
    at com.intellij.openapi.application.impl.ApplicationImpl$10$1.run(ApplicationImpl.java:681)
    at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:454)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:152)

如何解决?

更新 代码:

package com.company;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


public class Main 

    private final static String DATABASE_URL = "jdbc:sqlite:C:/work/sample.db";

    private Dao<Account, Integer> accountDao;

    public static void main(String[] args) throws Exception 
        new Main().doMain(args);
    

    public void doMain(String[] args) throws Exception 

        ConnectionSource connectionSource = null;
        try 

            // create our data-source for the database
            connectionSource = new JdbcConnectionSource(DATABASE_URL);
            // setup our database and DAOs
            setupDatabase(connectionSource);

            System.out.println("\n\nIt seems to have worked\n\n");
         finally 
            // destroy the data source which should close underlying connections
            if (connectionSource != null) 
                connectionSource.close();
            
        
    
    /**
     * Setup our database and DAOs
     */
    private void setupDatabase(ConnectionSource connectionSource) throws Exception 

        accountDao = DaoManager.createDao(connectionSource, Account.class);

        // if you need to create the table
        TableUtils.createTable(connectionSource, Account.class);
    

它返回:

 Exception in thread "main" java.lang.NoSuchMethodError: com.j256.ormlite.table.DatabaseTable.daoClass()Ljava/lang/Class;
        at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:63)
        at com.company.Main.setupDatabase(Main.java:62)
        at com.company.Main.doMain(Main.java:44)
        at com.company.Main.main(Main.java:26)

更新 2 ormliteExample code.

我不明白为什么这不起作用。

更新 3

在调试时很明显,出于某种奇怪的原因,connectionSource.connection 等于 null

【问题讨论】:

它是空的,因为你立即在 finally 块中关闭它。 这怎么可能? finally 在 try 块中的所有调用完成后执行。 是的,对不起,你是对的。我跳过了 setupDatabase 方法调用。 【参考方案1】:

xerial bitbucket page

如何指定数据库文件

这里是选择文件 C:\work\mydatabase.db(在 Windows 中)的示例

Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");

一个 UNIX(Linux、Mac OS X 等)文件 /home/leo/work/mydatabase.db

Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");

您的数据库网址错误。试试

jdbc:sqlite:C:/work/sample.db

而不是jdbc://jdbc:sqlite:C:/work/sample.db

【讨论】:

【参考方案2】:

Xerial Bitbucket

您能否向您展示您的连接代码,因为没有您的代码我们无法为您提供帮助。错误是不够的。

这是Bitbucket Xerial连接数据库的代码:

public static void main(String[] args) throws ClassNotFoundException

// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");

Connection connection = null;
try

  // create a database connection
  connection = DriverManager.getConnection("jdbc:sqlite:C:/work/sample.db");
  Statement statement = connection.createStatement();
  statement.setQueryTimeout(30);  // set timeout to 30 sec.

  statement.executeUpdate("drop table if exists person");
  statement.executeUpdate("create table person (id integer, name string)");
  statement.executeUpdate("insert into person values(1, 'leo')");
  statement.executeUpdate("insert into person values(2, 'yui')");
  ResultSet rs = statement.executeQuery("select * from person");
  while(rs.next())
  
    // read the result set
    System.out.println("name = " + rs.getString("name"));
    System.out.println("id = " + rs.getInt("id"));
  

catch(SQLException e)

  // if the error message is "out of memory", 
  // it probably means no database file is found
  System.err.println(e.getMessage());

finally

  try
  
    if(connection != null)
      connection.close();
  
  catch(SQLException e)
  
    // connection close failed.
    System.err.println(e);
  

确保您的代码与此处相同(仅用于连接)

【讨论】:

我需要用 xerial 连接 ormlite。我试图将 2 个示例合二为一。 Xerial 是 JDBC 驱动程序。 ormlite 是位于 Xerial 之上的 ORM...

以上是关于连接到数据源 SQLite 失败的主要内容,如果未能解决你的问题,请参考以下文章

PyCharm 无法连接到 SQLite:SQLITE_BUSY,(数据库已锁定)

sqlite3:连接到云中的数据库(S3)

如何将 SQlite 数据库连接到 devcpp? [关闭]

使用 SqlBulkCopy 连接到 SQLite 数据库

pyodbc 连接到 sqlite 数据库

QSqlDatabase 并连接到 .sqlite 文件