简体   繁体   中英

JDBC/SQL - ID is always 0

so i'm trying to insert an hardcoded member to my Members table. I'm using Spring Boot & JDBC on Eclipse.

This is my schema.sql:

CREATE TABLE Members 
(
    ID int not null,
    LastName varchar(255) not null,
    FirstName varchar(255) not null,
    PhoneNumber integer not null,
    created timestamp not null,
    primary key(ID)
);

INSERT INTO MEMBERS (ID, LASTNAME, FIRSTNAME,PHONENUMBER, CREATED)
VALUES(1001, 'Max', 'Mad', 0547547547, sysdate());

i got a findAll method in a DAO class:

public List<Member> findAll(){
        return jtemp.query("select * from members", new BeanPropertyRowMapper<Member>(Member.class));
}

when i run it, it returns:

[Member [id=0, firstName=Mad, lastName=Max, phoneNumber=547547547, created=2017-10-31 18:57:21.606]]

As you can see the ID wasn't inserted for some reason.

my Member class is like this:

public class Member {

    private long id;
    private String firstName;
    private String lastName;
    private long phoneNumber;
    private Date created;
    public Member(long id, String firstName, String lastName, long phoneNumber, Date created) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.created = created;
    }

    public long getUserId() {
        return id;
    }
    public void setUserId(long userId) {
        this.id = userId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public long getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(long phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public String toString() {
        return "Member [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber="
                + phoneNumber + ", created=" + created + "]";
    }


}

How can i fix it? I tried to inserted different numbers, but i always get a zero in the log.

Thanks

Column names of the query must match the setters in the target object.

Your query has a column named id but there is no setId method. You should either rename setUserId to setId or in the query give the alias user_id to id column.

From BeanPropertyRowMapper documentation:

Column values are mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties. The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.

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