SQL 使用 java servlet 时出现无效列错误
SQL Error Invalid column when using java servlet
这是我的主页
Main_page
出于某种原因,我可以按标题搜索列表,但无法获取所有列表。
正如我已经检查过的那样,按钮参数是可读的。
这是我在 servlet 中获取所有书籍列表的代码:
searchResults = bookDao.loadBookFromDB();
System.out.println("\nGet all book: ");
bookDao.showList(searchResults);
这是我获取所有书籍的代码:
public ArrayList<BookDTO> loadBookFromDB() throws SQLException, NamingException {
try {
connection = MyDB.createConnection();
if (connection != null) {
String sql = "Select Id, Title, Author, ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status"
+ "From " + MyConstants.TBL_BOOK
+ " Where Quantity >= ? "
+ " And Status = " + MyConstants.STATUS_STRING_ACTIVE;
statement = connection.prepareStatement(sql);
statement.setInt(1, MyConstants.MIN_BOOK_QUANTITY);
result = statement.executeQuery();
ArrayList<BookDTO> searchResult = null;
while (result.next()) {
String title = result.getString("Title");
String author = result.getString("Author");
String imageUrl = result.getString("ImageUrl");
String status = result.getString("Status");
int id = result.getInt("Id");
int price = result.getInt("Price");
int quantity = result.getInt("Quantity");
int categoryId = result.getInt("CategoryId");
Date createDate = result.getDate("CreatedDate");
if (searchResult == null) {
searchResult = new ArrayList<>();
}
searchResult.add(new BookDTO(id, categoryId, quantity, price, title, author, status, imageUrl, createDate));
}
return searchResult;
}
} finally {
checkConnection();
}
return MyConstants.FOUND_NO_LIST;
}
这是我按标题代码搜索的图书:
public ArrayList<BookDTO> getBookByLikeTitle(String searchValue) throws SQLException, NamingException {
try {
connection = MyDB.createConnection();
if (connection != null) {
String sql = "Select Id, Title, Author, ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status "
+ "From " + MyConstants.TBL_BOOK
+ "Where Title like ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "%" + searchValue + "%");
result = statement.executeQuery();
ArrayList<BookDTO> searchResult = null;
while (result.next()) {
String title = result.getString("Title");
String author = result.getString("Author");
String imageUrl = result.getString("ImageUrl");
String status = result.getString("Status");
int id = result.getInt("Id");
int price = result.getInt("Price");
int quantity = result.getInt("Quantity");
int categoryId = result.getInt("CategoryId");
Date createDate = result.getDate("CreatedDate");
if (searchResult == null) {
searchResult = new ArrayList<>();
}
searchResult.add(new BookDTO(id, categoryId, quantity, price, title, author, status, imageUrl, createDate));
}
return searchResult;
}
} finally {
checkConnection();
}
return MyConstants.FOUND_NO_LIST;
}
这是我的常量:
public final static String TBL_BOOK = "Book ";
public final static String STATUS_STRING_ACTIVE = "Active";
public final static int MIN_BOOK_QUANTITY = 1;
我展示的是标题一,因为该代码非常好。那么可以看到,两种方法中的结果集部分是完全一样的,所以结果部分没有问题。所以,我去检查了数据库。
这是我的 table 创建:
Create table [Book] (
[Id] int NOT NULL IDENTITY(1,1) primary key,
[Title] varchar(200),
[Author] varchar(50),
[ImageUrl] varchar(MAX),
[Quantity] int,
[Price] int,
[CategoryId] int,
[CreatedDate] datetime,
[Status] varchar(10))
而且我的SQL查询没有错check_get_all_query。因此,我只能认为错误的部分是 Quantity 条件部分。所以,我尝试将值直接放在字符串中而不是使用 setInt 语句:
String sql = "Select Id, Title, Author,ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status"
+ "From " + MyConstants.TBL_BOOK
+ "Where Quantity >= " + MyConstants.MIN_BOOK_QUANTITY
+ "And Status = " + MyConstants.STATUS_STRING_ACTIVE;
statement = connection.prepareStatement(sql);
结果是一样的。所以...我卡住了。日志一直显示我有这个错误:
BookMarketController SQL: Invalid column name 'Quantity'.
谁能告诉我为什么?
您在 Status"
之后错过了一个 space,因此有一个 StatusFrom 列。
在后续行的开头使用绑定变量和 space:
String selectAll =
"Select Id, Title, Author,ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status";
String sql = selectAll
+ " Where Quantity >= ? And Status = ?";
这是我的主页
Main_page
出于某种原因,我可以按标题搜索列表,但无法获取所有列表。 正如我已经检查过的那样,按钮参数是可读的。 这是我在 servlet 中获取所有书籍列表的代码:
searchResults = bookDao.loadBookFromDB();
System.out.println("\nGet all book: ");
bookDao.showList(searchResults);
这是我获取所有书籍的代码:
public ArrayList<BookDTO> loadBookFromDB() throws SQLException, NamingException {
try {
connection = MyDB.createConnection();
if (connection != null) {
String sql = "Select Id, Title, Author, ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status"
+ "From " + MyConstants.TBL_BOOK
+ " Where Quantity >= ? "
+ " And Status = " + MyConstants.STATUS_STRING_ACTIVE;
statement = connection.prepareStatement(sql);
statement.setInt(1, MyConstants.MIN_BOOK_QUANTITY);
result = statement.executeQuery();
ArrayList<BookDTO> searchResult = null;
while (result.next()) {
String title = result.getString("Title");
String author = result.getString("Author");
String imageUrl = result.getString("ImageUrl");
String status = result.getString("Status");
int id = result.getInt("Id");
int price = result.getInt("Price");
int quantity = result.getInt("Quantity");
int categoryId = result.getInt("CategoryId");
Date createDate = result.getDate("CreatedDate");
if (searchResult == null) {
searchResult = new ArrayList<>();
}
searchResult.add(new BookDTO(id, categoryId, quantity, price, title, author, status, imageUrl, createDate));
}
return searchResult;
}
} finally {
checkConnection();
}
return MyConstants.FOUND_NO_LIST;
}
这是我按标题代码搜索的图书:
public ArrayList<BookDTO> getBookByLikeTitle(String searchValue) throws SQLException, NamingException {
try {
connection = MyDB.createConnection();
if (connection != null) {
String sql = "Select Id, Title, Author, ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status "
+ "From " + MyConstants.TBL_BOOK
+ "Where Title like ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "%" + searchValue + "%");
result = statement.executeQuery();
ArrayList<BookDTO> searchResult = null;
while (result.next()) {
String title = result.getString("Title");
String author = result.getString("Author");
String imageUrl = result.getString("ImageUrl");
String status = result.getString("Status");
int id = result.getInt("Id");
int price = result.getInt("Price");
int quantity = result.getInt("Quantity");
int categoryId = result.getInt("CategoryId");
Date createDate = result.getDate("CreatedDate");
if (searchResult == null) {
searchResult = new ArrayList<>();
}
searchResult.add(new BookDTO(id, categoryId, quantity, price, title, author, status, imageUrl, createDate));
}
return searchResult;
}
} finally {
checkConnection();
}
return MyConstants.FOUND_NO_LIST;
}
这是我的常量:
public final static String TBL_BOOK = "Book ";
public final static String STATUS_STRING_ACTIVE = "Active";
public final static int MIN_BOOK_QUANTITY = 1;
我展示的是标题一,因为该代码非常好。那么可以看到,两种方法中的结果集部分是完全一样的,所以结果部分没有问题。所以,我去检查了数据库。 这是我的 table 创建:
Create table [Book] (
[Id] int NOT NULL IDENTITY(1,1) primary key,
[Title] varchar(200),
[Author] varchar(50),
[ImageUrl] varchar(MAX),
[Quantity] int,
[Price] int,
[CategoryId] int,
[CreatedDate] datetime,
[Status] varchar(10))
而且我的SQL查询没有错check_get_all_query。因此,我只能认为错误的部分是 Quantity 条件部分。所以,我尝试将值直接放在字符串中而不是使用 setInt 语句:
String sql = "Select Id, Title, Author,ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status"
+ "From " + MyConstants.TBL_BOOK
+ "Where Quantity >= " + MyConstants.MIN_BOOK_QUANTITY
+ "And Status = " + MyConstants.STATUS_STRING_ACTIVE;
statement = connection.prepareStatement(sql);
结果是一样的。所以...我卡住了。日志一直显示我有这个错误:
BookMarketController SQL: Invalid column name 'Quantity'.
谁能告诉我为什么?
您在 Status"
之后错过了一个 space,因此有一个 StatusFrom 列。
在后续行的开头使用绑定变量和 space:
String selectAll =
"Select Id, Title, Author,ImageUrl, Quantity, Price, CategoryId, CreatedDate, Status";
String sql = selectAll
+ " Where Quantity >= ? And Status = ?";