1

I'm trying to retrieve idPatient from the database but i keep getting errors, idPatient is auto increment. But i don't know if that's what causing the problem.

This is the code i'm trying

public List<Patient> getPatientsFromDatabase() {
    String sql = "SELECT naam, voornaam, gebit, idPatient from patienten";
    try (Connection con = sql2o.open()) {
        return con.createQuery(sql)
                .throwOnMappingFailure(false)
                .executeAndFetch(Patient.class);
    }
}

the DB:

enter image description here

The error:

enter image description here

Edit - Patient:

public Patient(String voornaam, String naam, String gebit, int id) {
    this.voornaam = voornaam;
    this.naam = naam;
    this.id = id;
    if (null != gebit)switch (gebit) {
        case "melkgebit":
            g = new Melkgebit();
            this.gebit = "melkgebit";
            break;
        case "volwassengebit":
            g = new Volwassengebit();
            this.gebit = "volwassengebit";
            break;
        case "wisselgebit":
            this.gebit = "wisselgebit";
            break;
        default:
            break;
    }
}
4

2 回答 2

2

好像Patient没有这个属性idPatient。核实

于 2016-05-26T11:53:16.100 回答
1

列和属性之间的映射是按名称自动完成的。当然这并不总是可能的

在您的情况下,您已将 id (java 属性)定义为 idPatient (数据库表中的相应列)。最简单的解决方案是在 sql 查询中使用别名。

尝试更改您的sql:

SELECT naam, voornaam, gebit, idPatient id from patienten;

欲了解更多信息,请访问http://www.sql2o.org/docs/column-mappings/

于 2016-05-26T12:03:08.203 回答