1

我想将从文本框中获取的时间插入 mysql 数据库 TIME 列。我想我需要在查询中使用“STR_TO_DATE”将字符串转换为时间,就像在mysql中将字符串转换为日期一样。我寻找答案,但没有得到我需要的答案。

编辑:来自评论的 SQL:

"insert into schedules (
    courseid, 
    batch, 
    subjectid, 
    teacherid, 
    stime, 
    etime, 
    date, 
    location, 
    building, 
    department, 
    hall, 
    status
) values ('" + 
    getCourse() + "','" + 
    getBatch() + "', '" + 
    getSubject() + "','" + 
    getTeacher() + "', '" + 
    getStime()+ "','" + 
    getEtime()+ 
    "',STR_TO_DATE('" + getDate() + "','%d-%m-%Y'),'" + 
    getLocation() + "', '" + 
    getBuilding() + "', '" + 
    getDepartment()+ "', '" + 
    getHall() + 
    "','ACTIVE')"
4

1 回答 1

0

如评论中所述,Mysql 接受像 '05:12:59' 这样的简单字符串到 TIME 类型列中,但让我们尝试对其有另一个答案。检查从文本框中获取的日期格式并编辑简单日期格式。你可以试试下面。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parsedDate = dateFormat.parse(request.getParameter("textBoxName"));
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());//or you can assign this stuff to stime variable

我假设您正在使用preparedStatement,因为我认为您会插入很多次。如果是这样,您可以像这样设置参数。

preparedStatement.setTimestamp(1, timestamp);//1 is the index of parameter you can choose named parameters too

您还可以选择设置时间并使用其相对吸气剂将其传递给查询。

于 2015-06-28T05:45:48.247 回答