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

时间:2014-07-02 10:19:53

标签: java sql sqlite

我已经获得了使用sqlite演示CRUD操作的作业。当我尝试从数据库中检索项目时,我的ResultSet似乎没有“id”列。我很难过。

这是我的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;
    }

...

}

我为我的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 = ?";

...

}

我得到一个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)    

编辑:

联系模型类

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
}

4 个答案:

答案 0 :(得分:5)

您的选择不包括列id,因此它不在结果集中。将其添加到选择中:

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

答案 1 :(得分:1)

您的SQL查询

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

不要选择id列。您只能从查询中选择的结果集中检索这些列。

为了获取id列值,您需要将查询修改为

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

答案 2 :(得分:1)

查询您使用的数据库时:

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

但来自resultset您期望的身份contact.setId(resultSet.getInt("id"));

因此错误会将查询更改为:

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

答案 3 :(得分:0)

只需检查您的数据库' CONTACT' table包含名为' id'的列。 仅当列名称不匹配时才会出现此错误。

您的查询是否正确