英文:
Insert into PSQL DB from datetime-local html input
问题
我无法正确获取日期时间的适当格式,以用于插入语句(更多是准备好的语句)。
从HTML输入(作为setter)传入变量为'String' -
<input style="border:1px solid grey; width: 100%" type="datetime-local" id="maintsd" name="maintsd">
Setter:
public void setMaintsd(String maintsd) {
this.maintsd = maintsd;
}
准备好的语句:
ps.setTimestamp(2, obj_modeSwitch_Bean.getMaintsdI());
Setter将值设置为2020-09-05T23:59
。
现在,预准备的语句不会使用setTimeStamp
(我提取的函数)或setDate
(提取日期时间的函数)进行设置。
预准备的语句或参数是否有变化?
英文:
I'm unable to get the proper format for the datatime into the insert statement (more of prepared statement).
Input from HTML (as setter) into variable as 'String' -
<input style="border:1px solid grey; width: 100%" type="datetime-local" id="maintsd" name="maintsd">
Setter:
public void setMaintsd(String maintsd) {
this.maintsd = maintsd;
}
Prepared Statement:
ps.setTimestamp(2, obj_modeSwitch_Bean.getMaintsdI());
Setter puts values as 2020-09-05T23:59
now the prepared statement doesn't set with setTimeStamp(my function to fetch) OR setDate(func to fetch datetime)
Is there a change in the prepared statement or with the parameter?
答案1
得分: 1
PreparedStatement#setObject
以下是一个示例:
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
LocalDateTime ldt = LocalDateTime.parse("2020-09-05T23:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
st.setObject(1, ldt);
st.executeUpdate();
st.close();
在您的情况下,应该是:
LocalDateTime ldt = LocalDateTime.parse(obj_modeSwitch_Bean.getMaintsdI(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
ps.setObjec(2, ldt);
英文:
PreparedStatement#setObject
Given below is an example:
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
LocalDateTime ldt = LocalDateTime.parse("2020-09-05T23:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
st.setObject(1, ldt);
st.executeUpdate();
st.close();
In your case, it should be
LocalDateTime ldt = LocalDateTime.parse(obj_modeSwitch_Bean.getMaintsdI(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
ps.setObjec(2, ldt);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论