gpt4 book ai didi

java - 没有在 ResultSet 中获取 MySQL SELECT LPAD()

转载 作者:行者123 更新时间:2023-11-29 00:07:54 25 4
gpt4 key购买 nike

我正在尝试从 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';

enter image description here

这个方法应该使用上面的 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;
}

历史(Bean):

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
}

历史汇编器:

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 的原因。 非常感谢您的协助。

最佳答案

在“LPAD”函数之后,列名称被更改并为您修复了“XAMPP”。你必须把 alias在“LPAD(transaction_id, 5, 0) as transaction_id”之后的查询中。

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';

关于java - 没有在 ResultSet 中获取 MySQL SELECT LPAD(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26658152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com