【问题标题】:SQL query method returns nullSQL查询方法返回null
【发布时间】:2016-01-23 11:49:43
【问题描述】:

我有一个类来管理我的数据库。但是,由于我在 static 类中创建了所有方法,因此查询方法不再起作用:它返回我 null

这是 DatabaseManager 类:

public class DatabaseManager {
    public final static String DBPath = "database.db";
    private static Connection connection = null;
    private static Statement statement = null;

    /**
     * Connect to the database.
     * @return True if succeeded to connect to the database. False if not.
     */
    private static boolean connect() {
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:" + DBPath);
            statement = connection.createStatement();
            return true;
        }
        catch (ClassNotFoundException | SQLException connectException) {
            connectException.printStackTrace();
            System.out.println("Unable to connect to the database.");
            return false;
        }
    }

    /**
     * Close the connection.
     */
    private static void close() {
        try {
            connection.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Execute a query inside a database.
     * @param queryStr the SQL query
     * @return ResultSet object is the result of the query (if it succeeded)
     */
    public static ResultSet query(String queryStr) {
        connect();
        ResultSet result = null;

        try {
            result = statement.executeQuery(queryStr);
        }
        catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Impossible to execute the following query : " + queryStr);
            System.out.println("It may contains error.");
        }
        finally {
            close();
        }

        return result;

    }
}

当我将此添加到 query 方法时:

try {
    while(result.next()) {
        System.out.println(result.getString("columnTest"));
    }
}
catch (SQLException e) { e.printStackTrace(); }

我在终端中得到了很好的结果。 (数据来自数据库)。但是当我删除它并将其添加到 main 方法时:

import DatabaseManager;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {

    public static void main(String[] args) {
        ResultSet result;
        result = DatabaseManager.query("SELECT * FROM DataTest");

        try {
            while (result.next()) {
                System.out.println(result.getString("columnTest"));
            }
        }
        catch (SQLException e) {
            e.getStackTrace();
        }
    }
}

它打印null 好像查询没有返回任何内容...为什么会这样?

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    我认为问题来自您的“查询”功能。您正在关闭那里的连接。所以你不能在 main 中打印结果

    【讨论】:

    • 谁在关闭连接后告诉我们无法打印结果?
    • 据说:“JDBC 不会在 ResultSet 中返回所有查询结果”。所以我认为关闭连接后你将无法调用 next() 并获得所有结果。来源:stackoverflow.com/questions/25493837/…
    • 是的,你是对的,没有调用 close 方法,它可以工作......但是我怎样才能直接在方法查询中关闭连接?
    • @Wizix 你不能,你的代码处理 JDBC 的方式存在根本缺陷,因为它忽略了 JDBC API 的使用方式。唯一的选择是首先将结果转移到其他东西(例如对象列表)。
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多