SQLException:没有这样的表

时间:2010-12-18 12:21:03

标签: java sqlite jdbc

现在我在连接数据库时遇到了一些麻烦。我知道我正在寻找的表存在,因为当我使用命令行访问它们时,可以查询它们。

可能是一些轻微的疏忽,但我会喜欢一些帮助。

这是我连接数据库的地方     包持久性;

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


public class DBRegistry {

    private static DBRegistry db = null;
    private static Connection connection = null;

    private DBRegistry() {};

    public static synchronized DBRegistry getUniqueInstance() {
        if (db == null) {
                db = new DBRegistry();
                return db;
        }
        else return db;
    }

    public synchronized Connection getDBConnection() {
            try {
                Class.forName("org.sqlite.JDBC");
                connection = DriverManager.getConnection("jdbc:sqlite:src/database/taskMan.db");
                return connection;
            } 
            catch (SQLException e) {e.printStackTrace();} 
            catch (ClassNotFoundException e) {e.printStackTrace();}
            return null;
    }

    public synchronized void closeConnection() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

以下是我查询的方式

public void create(UUID oid, Object obj) {
    Task t = (Task)obj;
    String statement = "INSERT INTO `complexTask` (`oid`,`description`,`status`) VALUES (?, ?, ?)";
    try {
        PreparedStatement dbStatement = db.prepareStatement(statement);
        dbStatement.setString(1, oid.toString());
        dbStatement.setString(2, t.getDescription());
        dbStatement.setBoolean(3, t.getStatus());
        dbStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

最后是一个堆栈跟踪:

java.sql.SQLException: no such table: complexTask
    at org.sqlite.DB.throwex(DB.java:288)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:114)
    at org.sqlite.PrepStmt.<init>(PrepStmt.java:37)
    at org.sqlite.Conn.prepareStatement(Conn.java:231)
    at org.sqlite.Conn.prepareStatement(Conn.java:224)
    at org.sqlite.Conn.prepareStatement(Conn.java:213)
    at persistence.framework.ComplexTaskRDBMapper.create(ComplexTaskRDBMapper.java:23)
    at persistence.PersistanceFacade.create(PersistanceFacade.java:49)
    at persistence.persistanceStates.NewState.commit(NewState.java:10)
    at persistence.PersistentObject.commit(PersistentObject.java:23)
    at domain.objects.Task.commitToDB(Task.java:89)
    at domain.TaskRepository.commitToDB(TaskRepository.java:60)
    at domain.TaskController.persistanceCommit(TaskController.java:97)
    at presentation.TaskControlsJPanel$3.actionPerformed(TaskControlsJPanel.java:127)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6175)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5940)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4536)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

并且有一些JUnit代码用于测量,第一次测试通过,第二次测试失败并出现与上面相似的错误

package test.persistence;

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

import persistence.DBRegistry;
import junit.framework.TestCase;

public class TestDBRegistry extends TestCase {

    public void testDBRegistryConnection() {
        Connection con =  DBRegistry.getUniqueInstance().getDBConnection();
        assertNotNull(con);
    }

    public void testTableQuery() throws SQLException {
        Connection con =  DBRegistry.getUniqueInstance().getDBConnection();
        PreparedStatement dbStatement = con.prepareStatement("SELECT COUNT(*) FROM `singleTask`");
        assertEquals("should be 1 for successful query", 1, dbStatement.executeQuery());
    }

6 个答案:

答案 0 :(得分:6)

我注意到在你的单元测试和你的其他代码中,你正在使用表名周围的反向标记。在最新版本的sqlite中,这很好,但在较旧的版本中它没有处理我相信。您是否可以尝试删除表名周围的刻度,或者可以将它们更改为常规引号而不是后退?

如果这没有解决它,我会检查以确定您指向正确的db文件。如果指定不存在的文件名,则不会出现错误,只会在那里创建一个新数据库。我不确定“当前目录”在您的应用程序或单元测试的上下文中是什么,但请确保它指向您认为的位置。要对此进行测试,您可以将db文件名更改为foo.db,运行单元测试,然后在计算机上搜索foo.db以查看其创建位置。这将告诉您应用的工作地点。

答案 1 :(得分:1)

我不确定你的JDBC连接字符串是否正确。您使用的连接字符串以taskMan.db结尾,但上面的注释暗示数据库文件的名称为taskManDb.db(请注意额外的Db)。

答案 2 :(得分:1)

它可能无法解决您的SQL INSERT问题,但我根本不关心您的DBRegistry实现。我会这样写:

package persistence;

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


public class DBRegistry
{
    public static Connection getConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
    {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection)
    {
        try
        {
            if (connection != null)
            {
                connection.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }


    public static void close(Statement statement)
    {
        try
        {
            if (statement != null)
            {
                statement.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet)
    {
        try
        {
            if (resultSet != null)
            {
                resultSet.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}

答案 3 :(得分:0)

也许你的桌子在一个包里。所以你可能会做类似

的事情
select * from Tasks.ComplextTask

('任务'是包裹)

答案 4 :(得分:0)

现在只是尝试在c:\ windows \ system32中找到一个具有相同名称的文件,你会发现它。它告诉我们你的路径不正确。祝你有愉快的一天;

答案 5 :(得分:0)

请在创建表格后及插入后提交您的连接。