【问题标题】:Values Not Being Inserted with prepareStatement in Derby Database在 Derby 数据库中未使用 prepareStatement 插入值
【发布时间】:2015-05-02 22:24:43
【问题描述】:

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

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);
        }

【问题讨论】:

    标签: java prepared-statement derby


    【解决方案1】:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 2021-04-05
      • 2019-10-13
      • 1970-01-01
      相关资源
      最近更新 更多