java sql 插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13980144/
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
java sql insert
提问by Imri Persiado
I got a table with 4 fields:
我有一个包含 4 个字段的表:
id, int(11), auto increament email, varchar(32) pass, varchar(32) date_created, date
id、int(11)、自动增加电子邮件、varchar(32) pass、varchar(32) date_created、date
My question is how my query should look like? I mean I don't need to insert the first value to id because it's auto increment but I have to insert all of the values..
我的问题是我的查询应该是什么样子?我的意思是我不需要将第一个值插入到 id 中,因为它是自动递增的,但我必须插入所有值..
采纳答案by Aviram Segal
In SQL you can specify which columns you want to set in the INSERT
statement:
在 SQL 中,您可以指定要在INSERT
语句中设置的列:
INSERT INTO table_name(email, pass, date_created) VALUES(?, ?, ?)
回答by Powerlord
First of all, I hope you're using PreparedStatements.
首先,我希望你使用 PreparedStatements。
Assuming you have a Connection object named conn and two strings email and password...
假设您有一个名为 conn 的 Connection 对象和两个字符串电子邮件和密码...
PreparedStatement stmt = conn.prepareStatement("INSERT INTO table_name(email, pass, date_created) VALUES (?, ?, ?)");
stmt.setString(1, email);
stmt.setString(2, password);
stmt.setDate(3, new Date());
stmt.executeUpdate();
回答by nixgadget
You can insert in the format
您可以插入格式
INSERT INTO YourTable (Your Columns) VALUES (Your Values)
So for e.g.
所以对于例如
INSERT INTO Test_Table (email, pass, data_created) VALUES ('[email protected]', 'pass', to_date(string, format))
回答by Kaf
Using parameters-tsql
; (better to pass values in parameters rather than as strings)
使用参数- tsql
; (最好在参数中传递值而不是作为字符串传递)
Insert into [YourTableName] (email, pass, date_created)
values (@email, @pass, @date_created)