誰もSQLのデータベースのdatetime列にnull値を設定する方法を知っていますか?私はnullとして文字列を設定しようとしました:String date = null;
しかし、私はこのエラーが表示されます:どのようにNULLの値をSQLのjavaにSQLの列に挿入する
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting datetime from character string
。
また、単に文字列を宣言するだけで、文字列を値に設定しようとしました(文字列の日付;)。しかし、これはちょうど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()を指定します。ユーザーが日付を選択しても問題ありませんが、日付を選択しなかった場合はエラーが発生するため、null値を設定できる方法を見つけようとしています。
日付列にNULLでない制約があるかどうかチェックしましたか? –
はい、null値を許可するように列を設定します – meebee