java - jdbc 使用自动增量字段将数据插入数据库
问题描述
我对自动增量字段有疑问。
我创建了这张表,
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中初始化id。
您可以删除 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
编辑:如果你必须指定 id 你可以使用 command LAST_INSERT_ID()
。检查此链接以获取参考LAST_INSERT_ID() MySQL
它将返回将要使用的下一个 id 的编号。
推荐阅读
- javascript - 使用 React 的 Containment Composition,`props.children` 是否总是 Array 类型?
- ms-word - TS2339:“RequestContext”MS 字插件类型上不存在属性“应用程序”
- javascript - 设置猫鼬结果的属性值在Nodejs中不起作用
- python - TensorFlow:将来自一个模型的预测整合到新模型的训练中的最简单方法是什么?
- javascript - 为表格行选择颜色值时,Ajax 请求不起作用
- awk - 我有三个文本文件,我想将它们合并(打印)到一个文件中。使用 awk 程序
- c - 在 Linux 内核中删除列表的头部
- java - 抖动编译错误 gradle java build
- matlab - 如何使用 SIMULINK 控制自定义硬件
- python - 动态确定已在 TensorFlow 中初始化变量的张量