简体   繁体   English

java.sql.SQLException:没有这样的列

[英]java.sql.SQLException: no such column

I've been given an assignment to demonstrate CRUD operations using sqlite. 我被分配了一个使用sqlite演示CRUD操作的任务。 When I try to retrieve an item from the database my ResultSet doesn't seem the have the "id" column. 当我尝试从数据库中检索项目时,我的ResultSet似乎没有“ id”列。 I am quite stumped. 我很沮丧。

This is my ContactRepository class. 这是我的ContactRepository类。

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 我将单独的类用于我的sql语句

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" 我得到一个SQLException“没有这样的列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. 您的选择不包括列id ,因此不在结果集中。 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 您的SQL查询

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. 不要选择id列。 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 为了获取id列值,您需要将查询修改为

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")); 但是从resultset您期望的是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'. 只需检查数据库,确保“ CONTACT”表包含名为“ id”的列。 This error is only arrived when there is column name mismatch. 仅当列名不匹配时,才会出现此错误。

Your Query is Correct 您的查询是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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