java—如何调用我准备好的语句从数据库中提取具有特定id的文章?

xmd2e60i  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(235)
public static final String TABLE_ARTICLE = "Article";
    public static final String COLUMN_ARTICLE_ID = "id";
    public static final String COLUMN_ARTICLE_TITLE = "title";
    public static final String COLUMN_ARTICLE_CONTENT = "content";
    public static final String COLUMN_ARTICLE_CREATION_DATE = "creationDate";
    public static final String COLUMN_ARTICLE_EXPIRE_DATE = "expireDate";
    public static final String COLUMN_ARTICLE_LAST_EDIT = "lastEdit";
    public static final String COLUMN_ARTICLE_STATUS = "status";
    public static final String COLUMN_ARTICLE_TOPIC = "topic";
    public static final String COLUMN_ARTICLE_AUTHOR_ID = "authorId";
    public static final String COLUMN_ARTICLE_PUBLISHER_ID= "publisherId";
    public static final String COLUMN_ARTICLE_PUBLISHER_COMMENT= "publisherComment";
    public static final int INDEX_TABLE_ARTICLE = 1;
    public static final int INDEX_ARTICLE_ID = 2;
    public static final int INDEX_ARTICLE_TITLE = 3;
    public static final int INDEX_ARTICLE_CONTENT = 4;
    public static final int INDEX_ARTICLE_CREATION_DATE = 5;
    public static final int INDEX_ARTICLE_EXPIRE_DATE = 6;
    public static final int INDEX_ARTICLE_LAST_EDIT = 7;
    public static final int INDEX_ARTICLE_STATUS = 8;
    public static final int INDEX_ARTICLE_TOPIC = 9;
    public static final int INDEX_ARTICLE_AUTHOR_ID = 10;
    public static final int INDEX_ARTICLE_PUBLISHER_ID = 11;
    public static final int INDEX_ARTICLE_PUBLISHER_COMMENT = 12;

public static final String LOAD_ARTICLE = "SELECT " + COLUMN_ARTICLE_TOPIC + ", " + COLUMN_ARTICLE_TITLE + ", " +
    COLUMN_ARTICLE_CONTENT + ", " + COLUMN_ARTICLE_PUBLISHER_COMMENT + ", " + COLUMN_ARTICLE_EXPIRE_DATE + " FROM " + TABLE_ARTICLE +
            " WHERE " + COLUMN_ARTICLE_ID + " = ?";

private PreparedStatement DBLoadArticle;

这里我已经初始化了表和准备好的语句。数据库也已设置。

public Article DBLoadArticle(int id) {
        try {
            Article article = DBLoadArticle. 

            if (article == null) {
                throw new SQLException("Couldn't get Article!");
            } else
                System.out.println("Article successfully loaded!");

            return article;
        } catch (SQLException e) {
            System.out.println("Couldn't get Article: " + e.getMessage());

        }
    }

如何创建此方法?我不知道是否应该使用resultset或其他查询,但我想使用我准备好的语句来拉取它。

7rfyedvj

7rfyedvj1#

假设您有一个db连接,并且您的连接中有一个初始化的prepared语句,下一步可能是填充任何参数(查询中的“?”),执行该语句,然后解析输出。
(我将preparedstatement对象名称更改为以小写字母开头。)

dbLoadArticle.setInt(1, id); // Set the ID
dbLoadArticle.execute(); // Run the statement
ResultSet rs = dbLoadArticle.getResultSet(); // Get the result set
article = null; // Initialize
if(rs.next()) {
    // Found something! Map the Article
    article = new Article(
        rs.getString(1),
        rs.getString(2),
        ...
    );
}
// Possibly check for multiple hits?
rs.close(); // Don't forget to close
dbLoadArticle.close(); // and close

相关问题