我创建了一个非常简单的表,并尝试将其插入其中。该表存在,但是我没有尝试插入木棍; resultSet始终为null。我认为我正在正确自动增加主键。有任何想法吗?谢谢!

String createGenreTableSQL = "CREATE TABLE Genre (GenreID INTEGER NOT NULL GENERATED ALWAYS " +
                "AS IDENTITY (START WITH 1, INCREMENT BY 1), genreName varchar(60))";

statement.executeUpdate(createGenreTableSQL);

String prepGenreInsert = "INSERT INTO Genre(genreName) VALUES (?)";

psInsert = conn.prepareStatement(prepGenreInsert);
        allStatements.add(psInsert);

        psInsert.setString(1,"Television");
        psInsert.setString(1,"Movies");
        psInsert.setString(1,"VideoGames");
        psInsert.setString(1,"Animes");


String fetchAllDataSQL = "SELECT * from Genre";

        resultSet = statement.executeQuery(fetchAllDataSQL);
        while (resultSet.next()) {  //resultSet shows null
            int genreID = resultSet.getInt("genreID");
            String genreName = resultSet.getString("genreName");

            System.out.println("GenreID:" + genreID + " Name: " + genreName);
        }

最佳答案

setString方法仅将给定值绑定到给定参数索引。它实际上并不执行查询。我相信你想做的是

psInsert.setString(1,"Television");
psInsert.execute();
psInsert.setString(1,"Movies");
psInsert.execute();
psInsert.setString(1,"VideoGames");
psInsert.execute();
psInsert.setString(1,"Animes");
psInsert.execute();

10-04 14:10