【问题标题】:Not getting MySQL SELECT LPAD() in ResultSet未在 ResultSet 中获取 MySQL SELECT LPAD()
【发布时间】:2014-12-26 19:09:43
【问题描述】:

我正在尝试从 MySQL 的不同表中选择多个列。

MySQL:

CREATE TABLE IF NOT EXISTS movie(
    movie_id TINYINT(3) UNSIGNED AUTO_INCREMENT,
    movie_title VARCHAR(255) NOT NULL,
    movie_genre VARCHAR(255) NOT NULL,
    movie_rating ENUM('G', 'PG', 'R-13', 'R-16', 'R-18', 'PENDING') NOT NULL,
    movie_cast VARCHAR(255) NOT NULL,
    movie_runtime TINYINT(3) UNSIGNED NOT NULL,
    movie_poster VARCHAR(255) NOT NULL,
    PRIMARY KEY(movie_id)
);

CREATE TABLE IF NOT EXISTS mall(
    mall_id VARCHAR(255),
    mall_name VARCHAR(255) NOT NULL,
    PRIMARY KEY(mall_id)
);

CREATE TABLE IF NOT EXISTS schedule(
    schedule_id TINYINT(3) UNSIGNED AUTO_INCREMENT,
    movie_id TINYINT(3) UNSIGNED NOT NULL,
    mall_id VARCHAR(255) NOT NULL,
    schedule_cinema TINYINT(2) UNSIGNED NOT NULL,
    schedule_price DECIMAL(5, 2) NOT NULL,
    schedule_date DATE NOT NULL,
    schedule_time TIME NOT NULL,
    schedule_seats TINYINT(2) UNSIGNED NOT NULL,
    PRIMARY KEY(schedule_id),
    FOREIGN KEY(movie_id) REFERENCES movie(movie_id),
    FOREIGN KEY(mall_id) REFERENCES mall(mall_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS transaction(
    transaction_id SMALLINT(5) UNSIGNED AUTO_INCREMENT,
    user_id VARCHAR(255) NOT NULL,
    schedule_id TINYINT(3) UNSIGNED NOT NULL,   
    transaction_date DATE NOT NULL,
    PRIMARY KEY(transaction_id),
    FOREIGN KEY(user_id) REFERENCES user(user_id),
    FOREIGN KEY(schedule_id) REFERENCES schedule(schedule_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

我直接在 XAMPP MySQL 中测试了这个查询,它返回了所有需要的列。 transaction_id 也按预期左填充。

选择 SQL:

SELECT LPAD(transaction_id, 5, 0), transaction_date, movie_title, schedule_price, mall_name, schedule_cinema, schedule_date, schedule_time FROM transaction INNER JOIN schedule ON transaction.schedule_id = schedule.schedule_id INNER JOIN movie ON schedule.movie_id = movie.movie_id INNER JOIN mall ON schedule.mall_id = mall.mall_id WHERE user_id = 'admin';

这个方法应该返回一个历史对象列表,使用上面的 SELECT SQL。

public List<History> getTransactionHistory(String currentUserId){
    History history;    
    List<History> historyList = new ArrayList<History>();

    sql = "SELECT LPAD(transaction_id, 5, 0), transaction_date, movie_title, schedule_price, "
        + "mall_name, schedule_cinema, schedule_date, schedule_time FROM transaction "
        + "INNER JOIN schedule ON transaction.schedule_id = schedule.schedule_id "
        + "INNER JOIN movie ON schedule.movie_id = movie.movie_id "
        + "INNER JOIN mall ON schedule.mall_id = mall.mall_id "
        + "WHERE user_id = ?";

    try {       
        ps = conn.prepareStatement(sql);
        ps.setString(1, currentUserId);

        rs = ps.executeQuery();

        while(rs.next()) {
            history = HistoryAssembler.getInstance(
                rs.getString("transaction_id"),
                rs.getDate("schedule_date"),
                rs.getString("movie_title"),
                rs.getBigDecimal("schedule_price"),
                rs.getString("mall_name"),
                rs.getInt("schedule_cinema"),
                rs.getDate("schedule_date"),
                rs.getTime("schedule_time")
            );

            System.out.println(history.getTransactionId());

            historyList.add(history);
        }

    } catch(SQLException se) {
        se.printStackTrace();
    }

    return historyList; 
}

历史(豆):

public class History {

    private String transactionId;   
    private Date transactionDate;
    private String movieTitle;
    private BigDecimal schedulePrice;
    private String mallName;
    private Integer scheduleCinema;
    private Date scheduleDate;
    private Time scheduleTime;

    // getters and setters
}

HistoryAssembler:

public static History getInstance(String transactionId, Date transactionDate, String movieTitle, BigDecimal schedulePrice,
    String mallName, Integer scheduleCinema, Date scheduleDate, Time scheduleTime) {

    History history = new History();

    history.setTransactionId(transactionId);
    history.setTransactionDate(transactionDate);
    history.setMovieTitle(movieTitle);
    history.setSchedulePrice(schedulePrice);
    history.setMallName(mallName);
    history.setScheduleCinema(scheduleCinema);
    history.setScheduleDate(scheduleDate);
    history.setScheduleTime(scheduleTime);

    return history;
}

但是,当我确实有上述专栏时,我收到了 java.sql.SQLException: Column 'transaction_id' not found
据我了解,LPAD() 应该返回一个字符串,这就是我在 bean 中设置 transactionId 的原因。
非常感谢您的帮助。

【问题讨论】:

    标签: java mysql sql select resultset


    【解决方案1】:

    在“LPAD”功能之后,列名已更改,“XAMPP”已为您修复。您必须在“LPAD(transaction_id, 5, 0) as transaction_id”之后在此查询中输入alias

    SELECT LPAD(transaction_id, 5, 0) as transaction_id
    , transaction_date
    , movie_title
    , schedule_price
    , mall_name
    , schedule_cinema
    , schedule_date
    , schedule_time 
    FROM transaction INNER JOIN schedule ON transaction.schedule_id = schedule.schedule_id
    INNER JOIN movie ON schedule.movie_id = movie.movie_id
    INNER JOIN mall ON schedule.mall_id = mall.mall_id
    WHERE user_id = 'admin';
    

    【讨论】:

    • 非常感谢!我不知道 XAMPP 有这样的行为。
    猜你喜欢
    • 2018-06-11
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2010-11-13
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多