Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java sql insert

Tags:

sql

I got a table with 4 fields:

id, int(11), auto increament email, 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..

like image 594
Imri Persiado Avatar asked Dec 20 '12 20:12

Imri Persiado


2 Answers

First of all, I hope you're using PreparedStatements.

Assuming you have a Connection object named conn and two strings email and password...

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();
like image 169
Powerlord Avatar answered Sep 22 '22 20:09

Powerlord


In SQL you can specify which columns you want to set in the INSERT statement:

INSERT INTO table_name(email, pass, date_created) VALUES(?, ?, ?)
like image 33
Aviram Segal Avatar answered Sep 20 '22 20:09

Aviram Segal