java使用Sqlite 出现类似java.sql.SQLException: no such table: employee错误

来源:互联网 发布:收银软件培训视频 编辑:程序博客网 时间:2024/05/22 04:46

今天在尝试使用java掉访问sqlite数据库老是报java.sql.SQLException: no such table: employee错误。

但是用sqlite命令行是可以做访问employee那张表的。

最后发现原来在定义dataSource的时候必须制定db的完整路径:

String dataSource = "jdbc:sqlite:C:\\。。。\\SqliteDev\\XXDemoa.db";

红色标注的必须写上。

完整测试例子如下:

注意必须引用sqlitejdbc-v037-nested.jar(首先下载该jar)

package com.hg.sqlite;


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


public class HelloSqlite {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        HelloSqlite hell = new HelloSqlite();
        hell.testSelect();
    }


    public void testSelect() {
        String dataSource = "jdbc:sqlite:C:\\.....\\SqliteDev\\XXDemoa.db";
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(dataSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (statement == null){
            try {
                statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("SELECT * FROM  employee;");
                while(resultSet.next()){
                    System.out.println(resultSet.getString("first_name"));
                }
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

原创粉丝点击