JSP数据库setTime,从表单中获取时间

时间:2014-02-17 03:54:53

标签: database jsp

// Create the prepared statement and use it to
// INSERT the student attributes INTO the Student table.
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO Review VALUES (?, ?, ?, ?)");

pstmt.setInt(1, Integer.parseInt(request.getParameter("section_id")));
pstmt.setString(2, request.getParameter("quarter"));                   
pstmt.setInt(3, Integer.parseInt(request.getParameter("year")));
pstmt.setTime(4, ????)

任何人都可以告诉我如何读取时间类型的值?让我们说时间就像" 16:34"?如何定义模式并阅读它?

1 个答案:

答案 0 :(得分:0)

你需要这样做。

  public static java.sql.Time getCurrentJavaSqlTime(String time) {
    Date date = new SimpleDateFormat("HH:mm", Locale.ENGLISH).parse(time);  //Convert to Date
    return new java.sql.Time(date.getTime());  //Now to time
  }

  java.sql.Time time = getCurrentJavaSqlTime("16:34");
  System.out.println("time=" + time);
  pstmt.setTime(4,time);

使用格式模式处理字符串。

H   Hour in day (0-23)   Number             0
k   Hour in day (1-24)   Number             24
K   Hour in am/pm (0-11) Number             0
h   Hour in am/pm (1-12) Number             12
m   Minute in hour       Number             30

要从用户那里获取

java.sql.Time time = getCurrentJavaSqlTime((String)request.getParameter("time"));
 pstmt.setTime(4,time);