两个不同ID上的GetId()

时间:2019-07-01 14:39:24

标签: java dao

我的问题是我无法在数据库中插入两个不同的ID。 uhrid始终与kundeid相同。

我知道问题出在我的dao类中。

public Bestellung insert(Bestellung entity) throws DbException {
            if (!this.hasConnection()) 
                throw new DbException("No connection to Database");

            try {
                // create sql command with placeholder
                String sql = "INSERT INTO " + this.tableName + " VALUES (NULL,?,?)";
                PreparedStatement preparedStatement = this.conn.getConnection().prepareStatement(sql);

                preparedStatement.setInt(1, entity.getId());
                preparedStatement.setInt(2, entity.getId());

                int affectedRows = preparedStatement.executeUpdate();

                if (affectedRows != 1) 
                    throw new SQLException("Insert failed");
                entity.setId(this.getLastAutoincrementId(preparedStatement));           
            } catch (SQLException e) {
                throw new DbException("Could not insert the entity=" + entity.toString(), e);
            }

            // return entity
            return entity;
        }
@POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createBestellung(
            @FormParam("uhrid") int uhrid,
            @FormParam("kundeid") int kundeid
            ) {
        System.out.println("BestellungService - createBestellung");

            Bestellung entity = new Bestellung();
            entity.setId(uhrid);
            entity.setId(kundeid);

            ResponseBuilder responseBuilder = null;
            DbConnection conn = null;
        try {

                conn = DbConnection.getInstance();
            BestellungDao dao = new BestellungDao(conn);

            entity = dao.insert(entity);

            System.out.println("Inserted entity: " + entity.toString());
            responseBuilder = Response.status(Status.OK).entity(entity);

        } catch (DbException | SQLException e) {

            e.printStackTrace();
            ServiceException se = new ServiceException("Fehler aufgetreten", e);
            responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(se.toJSON());
        } finally {
            conn.close();
        }

        return responseBuilder.build();
}}
public class Oberklasse {

    protected int id;

    public Oberklasse() {
        this.id = 0;
    }

    public int getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "Oberklasse [id=" + id + "]";
    }
public class Kunde extends Oberklasse{

    private String anrede;
    private String vorname;
    private String nachname;
    private String adresse;

    ...

}

Uhr类看起来几乎相同。

数据库中的输出:

id | uhrid | kundeid 
---+-------+---------    
 1 |   3   |    3

例如预期的输出:

id | uhrid | kundeid 
---+-------+---------    
 1 |   2   |   3

uhrid始终与kundeid相同。

我希望有人可以帮助我解决我的问题。

谢谢。

0 个答案:

没有答案