首页 > 解决方案 > statement.executeQuery)在 NetBeans java 中不起作用

问题描述

我正在尝试在我的数据库中输出我的表 (StudentInfo) 的第一列 (nameID)。但是 statement.executeQuery 不起作用并且 ResultSet.next() 为假。

    private static Connection connection = null;
    private static PreparedStatement stmt = null;
    private static Statement statement = null;
    private static ResultSet results = null;

    public static void createNewDatabase() {
        String sqlData = "SELECT * FROM StudentInfo";
        String url;
        try {
            connection = DriverManager.getConnection(url);
            if (connection != null) {
                statement = connection.createStatement();
                stmt = connection.prepareStatement(sqlData);
                statement.executeUpdate(sqlData);
                results = statement.executeQuery(sqlData);
                while (results.next()) {
                   print(results.getInt("nameID"));
                }
                stmt.close();
                results.close();
            }
        } catch (SQLException e) {
            print(e.getMessage());
        }
    }

    public static void main(String[] args) {
       createNewDatabase();
    }

它只是打印出 BUILD successful 而没有实际显示数据库中的第一列。

标签: javanetbeans

解决方案


推荐阅读