jdbc将数据插入具有自动增量字段的数据库中

问题描述 投票:0回答:1

我对自动递增字段有问题。

我创建了此表,

CREATE TABLE todo ( id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, name VARCHAR(100) NOT NULL, sequence INT(1) NOT NULL, type VARCHAR(20) DEFAULT 'TODO', regdate DATETIME DEFAULT NOW(), PRIMARY KEY (id) );

我创建了这个模型对象

public class TodoDto {
private Long id;
private String name;
private String regDate;
private int sequence;
private String title;
private String type;

public TodoDto(Long id, String name, String regDate, int sequence, String title, String type) {
    super();
    this.id = id;
    this.name = name;
    this.regDate = regDate;
    this.sequence = sequence;
    this.title = title;
    this.type = type;
}

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getRegDate() {
    return regDate;
}
public void setRegDate(String regDate) {
    this.regDate = regDate;
}
public int getSequence() {
    return sequence;
}
public void setSequence(int sequence) {
    this.sequence = sequence;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}

@Override
public String toString() {
    return "TodoDto [id=" + id + ", name=" + name + ", regDate=" + regDate + ", sequence=" + sequence + ", title="
            + title + ", type=" + type + "]";
}

}

我创建此文件是为了将数据插入数据库

public List<TodoDto> getTodos() {
    List<TodoDto> list = new ArrayList<>();

    try {
        Class.forName("com.mysql.jdbc.Driver");
    }catch (Exception e) {
        e.printStackTrace();
    }

    String sql = "SELECT * FROM todo";

    try(Connection conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
            PreparedStatement ps = conn.prepareStatement(sql)){

        try(ResultSet rs = ps.executeQuery()){

            while(rs.next()) {
                Long id = rs.getLong("id");
                String title = rs.getString("title");
                String name = rs.getString("name");
                int sequence = rs.getInt("sequence");
                String regDate = rs.getString("regDate");
                String type = rs.getString("type");

                TodoDto todoDto = new TodoDto(id, title, name, sequence, regDate, type);
                list.add(todoDto);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }

    }catch(Exception e){
        e.printStackTrace();
    }

    return list;
}

我创建了此客户端来执行TodoDao.java

    String title = "title";
    String name = "name";
    Long id = ???
    int sequence = 1;

    String type = "TODO";

    String currentDate = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);

    TodoDto todoDto = new TodoDto(id, name, currentDate, sequence, title, type);

    TodoDao todoDao = new TodoDao();
    int insertCount = todoDao.addTodo(todoDto);

    System.out.println(insertCount);

在此客户端中,我不知道如何初始化Long id

DB上的ID会自行增加。

所以我应该如何初始化它?

我认为这是一个简单的问题。但是我对此没有足够的了解,也没有人要问

请让我知道我该怎么办

java mysql jdbc insert
1个回答
0
投票
创建数据库时,请勿在id字段上强制不为null。删除NOT NULL。不需要它,自动递增将解决它]

id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT

并且当您想使用Java应用程序插入db时,请使用以下任一语法

INSERT INTO Persons (name,regDate,sequence,title,type)
VALUES ('','','','',''); 

// Notice i ignored field id

INSERT INTO Persons VALUES (NULL,'','','','','')

// Notice that i specified NULL in place where id value is expected
© www.soinside.com 2019 - 2024. All rights reserved.