有谁知道如何设置空值到数据库中的日期时间列sql?我试图设置字符串为空:String date = null;
但我会得到这个错误:如何将空值插入到SQL的日期时间列与java
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting datetime from character string
。
此外,我试图通过简单地声明它(String date;)来设置我的字符串没有值。然而,这只会将日期设置为1900年1月1日。
我使用了3个下拉框供程序用户选择月份,日期和年份。
DateNew newdateprocessed;
int b, c;
String d;
//First I took the values from each drop down box.
b = Date_Processed_Month_Mod.getSelectedIndex();
c = Date_Processed_Day_Mod.getSelectedIndex();
d = Date_Processed_Year_Mod.getSelectedItem().toString();
newdateprocessed = new DateNew(b, c, d);
//Then I used these values in a separate function to put them all together as just one string
public class DateNew extends Object {
String newdate;
String monthword, newdateword;
int check=0;
public DateNew (int month, int day, String year) {
//newdate = ""+ month + "/" + day + "/" + year;
if (month == 0 | day == 0 | year.equals("Year")) {
newdate=null;
check = 1;
}
else {
switch (month) {
case 1:
monthword="January";
break;
case 2:
monthword="February";
break;
case 3:
monthword="March";
break;
case 4:
monthword="April";
break;
case 5:
monthword="May";
break;
case 6:
monthword="June";
break;
case 7:
monthword="July";
break;
case 8:
monthword="August";
break;
case 9:
monthword="September";
break;
case 10:
monthword="October";
break;
case 11:
monthword="November";
break;
case 12:
monthword="December";
break;
}
newdateword= monthword + " " + day + ", " + year;
newdate = ""+ month + "/" + day + "/" + year;
}
check=month;
}
public String newdate() {
return newdate;
}
public int newdatecheck(){
return check;
}
public String newdateword(){
return newdateword;
}
最后,我会给newdateprocessed.newdate()到我的数据库中的列。如果用户选择一个日期,它会工作正常,但如果他们没有选择一个日期,我会得到一个错误,这就是为什么我试图找到一种方法能够将一个空值。
您是否检查过日期列是否存在非空约束? –
是的,我设置列允许空值 – meebee