使用queryForObject

时间:2017-02-20 13:31:07

标签: java mysql jdbc spring-jdbc jdbctemplate

以下代码我应该返回的客户等于codCli的字符串。

我有一个mysql的数据库。当我尝试查询所有它工作正常但具体选择不起作用。实际上,我想将查询结果放在像Customer这样的bean /容器中,或者放在一个列表中进行操作。

private JdbcOperations jdbc;
.....
public Customer getUser(String codCli) {
    try {
        Customer customer = jdbc.queryForObject(
            "SELECT id, codice_cliente, Indrizzo FROM customers WHERE codice_cliente = ?",
            new Object[] { codCli },
            new RowMapper<Customer>() {
                public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {

                    Customer cus = new Customer();
                    cus.setId(rs.getLong("id"));
                    cus.setCodiceCliente(rs.getString("codice_cliente"));
                    // customer.setAge(rs.getInt("age"));
                    cus.setCodiceAzienda(rs.getString("CodiceAzienda"));

                    return cus;
                }
            }
        );
        return customer;

    } catch (EmptyResultDataAccessException e) {
        return null;
    }
}

这是我的客户类:

package com.example;

public class Customer {

    private Long id;
    private String firstName, lastName, CodiceCliente, Indrizzo, CodiceAzienda;

    public Customer() {
        // TODO Auto-generated constructor stub
    }

    public Customer(Long id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Customer(String CodiceCliente, String codiceAz, String Indrizzo) {
        this.id = id;
        this.CodiceAzienda = codiceAz;
        this.lastName = lastName;
        this.CodiceCliente = CodiceCliente;
        this.Indrizzo = Indrizzo;
    }

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

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setCodiceCliente(String lastName) {
        this.CodiceCliente = lastName;
    }

    public void setCodiceAzienda(String lastName) {
        this.CodiceAzienda = lastName;
    }

    public void setIndrizzo(String firstName) {
        this.Indrizzo = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Long getId() {
        return this.id;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public String getCodiceCliente() {
        return this.CodiceCliente;
    }

    public String getCodiceAzienda() {
        return this.CodiceAzienda;
    }

    public String Indrizzo() {
        return this.Indrizzo;
    }
}

以下是错误消息:

java.lang.NullPointerException: null
at com.example.CustomerService.getUser(CustomerService.java:87) ~[classes/:na]

0 个答案:

没有答案