The problem is that I can't figure out how to insert a null value. The column is date and if it's empty it just adds the current date to the db by default, but I don't know how to leave it empty or null if the user doesn't specify a custom date.
statement.executeUpdate("insert into table values(null, 100, 'text')");
This is the query. I've tried to skip the first value, insert a null, insert empty string, a 0 value. nothing seems to do the trick.
try the below code
String sql = "INSERT INTO temp(val) VALUES (?)";
PreparedStatement st = con.prepareStatement(sql);
if (/* int value is not null */) {
st.setInt(1, value);
} else {
set.setNull(1, Types.INTEGER);
}
count = st.executeUpdate();
Use this form, where you specify what columns you intend to set:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
In your case:
insert into table (col2, col3) values (100, 'text')
(where of course col2 and col3 is the column names of your table)
Edit: The date column (col1) will probably need the constraint "Default null" for this to work as you intended.
in your column_date uncheck NOT NULL then you can insert NULL VALUES
simple code "INSERT INTO table_name
( field_name
) VALUES (NULL)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.