-3

MS Access に日付と時刻を追加したいのですが、日付と時刻の変数が文字列です。すなわち

String dt="12/2/2014 9:00 PM"; //this is selected from a calender component and a ComboBox

MS Access フィールドのタイプは (日付/時刻) です。フィールドに挿入できるように、文字列を日付/時刻型に変換する方法は? いくつかのコード行で説明していただけますか? 私はJavaの専門家ではないので。私が欲しいもののようなもの:

step1: 文字列を日時フィールドに変換する

step2: statement.executeUpdate(Insert into table (Date-Time) Values(??????)

4

1 に答える 1

2

ステップ:1 文字列を日時フィールドに変換する:

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
String stringDate = "12/2/2014 9:00 PM";
java.util.Date date = df.parse(stringDate);

これにより、文字列から日付が得られます。SimpleDateFormatの詳細については、API ドキュメントを参照してください

ステップ2:

String query = "Insert into table MyTable(dateColumn) Values(?)";
PreparedStatement ps  = connection.prepareStatement(query);
ps.setTimestamp(1,new java.sql.Timestamp(date.getTime()));
ps.executeUpdate();

準備済みステートメントの使用に関する詳細は次のとおりです。

于 2013-01-17T09:27:42.917 に答える