简体   繁体   中英

java.sql.SQLException: no such column

I've been given an assignment to demonstrate CRUD operations using sqlite. When I try to retrieve an item from the database my ResultSet doesn't seem the have the "id" column. I am quite stumped.

This is my ContactRepository class.

public class ContactRepositoryJDBC implements ContactRepository {

    private static Connector connector = new Connector();

    ...

    public Contact getById(int id) throws SQLException {
        Contact contact = null;

        try (Connection conn = connector.getConnection();
            PreparedStatement statement = conn
                    .prepareStatement(SQL.GET_CONTACT_BY_ID)) {
            statement.setInt(1, id);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    contact = new Contact();
                    contact.setId(resultSet.getInt("id"));
                    contact.setFirstName(resultSet.getString("first_name"));
                    contact.setSurname(resultSet.getString("surname"));
                    contact.setHomeNumber(resultSet.getString("home_number"));
                    contact.setCellNumber(resultSet.getString("cell_number"));
                    contact.setEmail(resultSet.getString("email"));
                }
            }
        }

        if (contact == null) {
            System.out.print("no contact with id " + id + " found : ");
        }
        return contact;
    }

...

}

I use a seperate class for my sql statements

public class SQL {

...

public static final String GET_CONTACT_BY_ID = "SELECT first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?";

...

}

I get an SQLException "no such column id"

Exception in thread "main" java.sql.SQLException: no such column: 'id'
at org.sqlite.RS.findColumn(RS.java:121)
at org.sqlite.RS.getInt(RS.java:293)
at net.phonebook.repository.ContactRepositoryJDBC.getById(ContactRepositoryJDBC.java:59)
at net.phonebook.model.app.App.main(App.java:31)    

EDIT:

Contact model class

public class Contact {

    private int id;
    private String firstName;
    private String surname;
    private String homeNumber;
    private String cellNumber;
    private String email;

    public Contact(String firstName, String surname, String homeNumber,
            String cellNumber, String email) {
        this.firstName = firstName;
        this.surname = surname;
        this.homeNumber = homeNumber;
        this.cellNumber = cellNumber;
        this.email = email;
    }

    public Contact() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    ... getters and setter for everything else
}

Your selection does not include the column id and hence it is not in the result set. Add it to the selection:

public static final String GET_CONTACT_BY_ID = "SELECT id, first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?";

Your SQL Query

public static final String GET_CONTACT_BY_ID = "SELECT first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?";

do not selects the id column. You can only retrieve those columns from result set which are selected in your query.

In Order to get id column value you need to modify your query to

public static final String GET_CONTACT_BY_ID = "SELECT id,first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?";

While querying the database you used:

SELECT first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?

but from the resultset you are expecting id contact.setId(resultSet.getInt("id"));

hence the error change the query to:

SELECT id, first_name, surname, home_number, cell_number, email FROM Contact WHERE id = ?

Just check your database that 'CONTACT' table contains the column named as 'id'. This error is only arrived when there is column name mismatch.

Your Query is Correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM