连接到数据源SQLite失败

时间:2014-08-08 13:38:57

标签: java sqlite jdbc intellij-idea ormlite

我使用ormlitexerial sqlite jdbc驱动程序sqlite-jdbc-3.7.2.jar并在sample.db中有空文件C:\work\但我无法连接到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 ormlite Example code

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

更新3

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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);
  }
}

}

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