从datetime-local的HTML输入插入到PSQL数据库中

huangapple go评论127阅读模式
英文:

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' -

&lt;input  style=&quot;border:1px solid grey; width: 100%&quot;  type=&quot;datetime-local&quot; id=&quot;maintsd&quot; name=&quot;maintsd&quot;&gt;

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(&quot;INSERT INTO mytable (columnfoo) VALUES (?)&quot;);
LocalDateTime ldt = LocalDateTime.parse(&quot;2020-09-05T23:59&quot;, 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);

huangapple
  • 本文由 发表于 2020年9月4日 06:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63732294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定