java 如何在java中添加自动增量列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31316247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an auto increment column in java?
提问by Ankush Tyagi
I want to add the database from my jform and there's a column which will be auto incremented, like when i click done, the data will be inserted and a column receipt_no
will have a value 1. Next time I click done then this value should be 2 and so on.
我想从我的 jform 添加数据库,并且有一列会自动增加,就像当我点击完成时,数据将被插入并且一列receipt_no
的值是 1。下次我点击完成时,这个值应该是 2等等。
So the problem is, i have created a table with receipt_no as the primary key and auto increment, so what should be my query in java, to add the data correctly in the table.
所以问题是,我创建了一个以receipt_no作为主键和自动增量的表,那么我在java中的查询应该是什么,以便在表中正确添加数据。
String sql = "insert into table_name values('"++"',...)";
Can you help me in this query?
你能帮我完成这个查询吗?
回答by Vicky Thakor
Step 1:Creating table in MySQL
第 1 步:在 MySQL 中创建表
CREATE TABLE `user_master` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Firstname` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2:Insert record
第二步:插入记录
INSERT INTO user_master (`Firstname`) values('Vicky');
Step 3:Fetch record
第 3 步:获取记录
SELECT * FROM user_master;
回答by Mistalis
I can't comment so there is an answer to the comment you posted in your question:
我无法发表评论,因此您在问题中发表的评论有答案:
If your table is
如果你的桌子是
CREATE TABLE users(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
);
You can simply auto_increment the primary by not giving it on your SQL request:
您可以通过不在 SQL 请求中提供它来简单地 auto_increment 主:
INSERT INTO users(firstname, lastname) VALUES('Steve', 'Jobs');
Java don't have to generate auto increment, it is SQL job :)
Java 不必生成自动增量,它是 SQL 作业:)