Я работаю с локальным сервером Derby, и у меня возникают проблемы с сообщением java. sql .SQLSyntaxErrorException: 'ID' не является столбцом в таблице или VTI 'AUTHORBOOKDB.AUTHOR'. У меня настроена база данных, и ID отображается в схеме AUTHORBOOK.AUTHOR.
Вот мои две сущности:
AuthorDAO.java
package entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import core.*;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class AuthorDAO implements DAO<Author>
{
public AuthorDAO() {
}
List<Author> authors;
/**
* Get a single author entity as a author object
* @param id
* @return
*/
@Override
public Optional<Author> get(int id) {
DB db = DB.getInstance();
ResultSet rs = null;
try {
String sql = "SELECT * FROM Author WHERE ID = ?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
Author author = null;
while (rs.next()) {
author = new Author(rs.getInt("ID"), rs.getString("FIRSTNAME"), rs.getString("LASTNAME"));
}
return Optional.ofNullable(author);
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
/**
* Get all author entities as a List
* @return
*/
@Override
public List<Author> getAll() {
DB db = DB.getInstance();
ResultSet rs = null;
authors = new ArrayList<>();
try {
String sql = "SELECT * FROM Author";
rs = db.executeQuery(sql);
Author author = null;
while (rs.next()) {
author = new Author(rs.getInt("ID"), rs.getString("FIRSTNAME"), rs.getString("LASTNAME"));
authors.add(author);
}
return authors;
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
/**
* Insert a author object into author table
* @param author
*/
@Override
public void insert(Author author)
{
DB db = DB.getInstance();
try {
String sql = "INSERT INTO Author(ID, FIRSTNAME, LASTNAME) VALUES (?, ?, ?)";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, author.getID());
stmt.setString(2, author.getFirstName());
stmt.setString(3, author.getLastName());
int rowInserted = stmt.executeUpdate();
if (rowInserted > 0) {
System.out.println("A new author was inserted successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Update a author entity in database if it exists using a author object
* @param author
*/
@Override
public void update(Author author) {
DB db = DB.getInstance();
try {
String sql = "UPDATE Author SET FIRSTNAME=?, LASTNAME=? WHERE ID=?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setString(1, author.getFirstName());
stmt.setString(2, author.getLastName());
stmt.setInt(4, author.getID());
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("An existing author was updated successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Delete a author from author table if the entity exists
* @param author
*/
@Override
public void delete(Author author) {
DB db = DB.getInstance();
try {
String sql = "DELETE FROM Author WHERE ID = ?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, author.getID());
int rowsDeleted = stmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("A author was deleted successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Get all column names in a list array
* @return
*/
@Override
public List<String> getColumnNames() {
DB db = DB.getInstance();
ResultSet rs = null;
List<String> headers = new ArrayList<>();
try {
String sql = "SELECT * FROM Author WHERE ID = -1";//We just need this sql query to get the column headers
rs = db.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
//Get number of columns in the result set
int numberCols = rsmd.getColumnCount();
for (int i = 1; i <= numberCols; i++) {
headers.add(rsmd.getColumnLabel(i));//Add column headers to the list
}
return headers;
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
}
BookDAO.java
package entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import core.*;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class BookDAO implements DAO<Book>
{
public BookDAO() {
}
List<Book> books;
/**
* Get a single book entity as a book object
* @param id
* @return
*/
@Override
public Optional<Book> get(int id) {
DB db = DB.getInstance();
ResultSet rs = null;
try {
String sql = "SELECT * FROM Book WHERE ID = ?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
Book book = null;
while (rs.next()) {
book = new Book(rs.getInt("ISBN"), rs.getString("TITLE"), rs.getInt("AUTHORID"));
}
return Optional.ofNullable(book);
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
/**
* Get all book entities as a List
* @return
*/
@Override
public List<Book> getAll() {
DB db = DB.getInstance();
ResultSet rs = null;
books = new ArrayList<>();
try {
String sql = "SELECT * FROM Book";
rs = db.executeQuery(sql);
Book book = null;
while (rs.next()) {
book = new Book(rs.getInt("ISBN"), rs.getString("FIRSTNAME"), rs.getInt("AUTHORID"));
books.add(book);
}
return books;
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
/**
* Insert a book object into book table
* @param book
*/
@Override
public void insert(Book book)
{
DB db = DB.getInstance();
try {
String sql = "INSERT INTO Book(ISBN, title, authorid) VALUES (?, ?, ?)";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, book.getIsbn());
stmt.setString(2, book.getTitle());
stmt.setInt(3, book.getauthorID());
int rowInserted = stmt.executeUpdate();
if (rowInserted > 0) {
System.out.println("A new book was inserted successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Update a book entity in database if it exists using a book object
* @param book
*/
@Override
public void update(Book book) {
DB db = DB.getInstance();
try {
String sql = "UPDATE Book SET ISBN=?, TITLE=?, WHERE ID=?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, book.getIsbn());
stmt.setString(2, book.getTitle());
stmt.setInt(4, book.getauthorID());
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("An existing book was updated successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Delete a book from book table if the entity exists
* @param book
*/
@Override
public void delete(Book book) {
DB db = DB.getInstance();
try {
String sql = "DELETE FROM Book WHERE ID = ?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setInt(1, book.getIsbn());
int rowsDeleted = stmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("A book was deleted successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
}
}
/**
* Get all column names in a list array
* @return
*/
@Override
public List<String> getColumnNames() {
DB db = DB.getInstance();
ResultSet rs = null;
List<String> headers = new ArrayList<>();
try {
String sql = "SELECT * FROM Book WHERE ID = -1";//We just need this sql query to get the column headers
rs = db.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
//Get number of columns in the result set
int numberCols = rsmd.getColumnCount();
for (int i = 1; i <= numberCols; i++) {
headers.add(rsmd.getColumnLabel(i));//Add column headers to the list
}
return headers;
} catch (SQLException ex) {
System.err.println(ex.toString());
return null;
}
}
}
Также вот трассировка стека:
INFO: Flyway 3.2.1 by Boxfuse
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.dbsupport.DbSupportFactory createDbSupport
INFO: Database: jdbc:derby:authorbookdb (Apache Derby 10.10)
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.metadatatable.MetaDataTableImpl removeFailedMigrations
INFO: Repair of failed migration in metadata table "AUTHORBOOKDB"."schema_version" not necessary. No failed migration detected.
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.command.DbRepair repair
INFO: Metadata table "AUTHORBOOKDB"."schema_version" successfully repaired (execution time 00:00.230s).
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.command.DbValidate validate
INFO: Validated 2 migrations (execution time 00:00.007s)
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.command.DbMigrate migrate
INFO: Current version of schema "AUTHORBOOKDB": 1
Apr 06, 2020 8:13:05 PM org.flywaydb.core.internal.command.DbMigrate logSummary
INFO: Schema "AUTHORBOOKDB" is up to date. No migration necessary.
java.sql.SQLSyntaxErrorException: 'ID' is not a column in table or VTI 'AUTHORBOOKDB.AUTHOR'.
Спасибо за вашу помощь!