代码之家  ›  专栏  ›  技术社区  ›  sgerbhctim

如何使用JSP将当前时间和未来时间插入MySQL数据库

  •  1
  • sgerbhctim  · 技术社区  · 7 年前

    我很难在MySQL数据库中插入正确的datetime。数据库设置为:

    aucID,国际(11)人工智能

    最低价格,浮动

    启动,浮动

    开始日期时间

    结束日期时间

    <%@ page import ="java.sql.*" %>
    <% 
    
    
    
       try{
            String itemid = request.getParameter("itemid");   
            String startprice = request.getParameter("startprice");
            String minprice = request.getParameter("minprice");
            String startdate = request.getParameter("current");
            String enddate = request.getParameter("length");
    
            System.out.println(itemid);
    
            System.out.println(startprice);
            System.out.println(minprice);
            System.out.println(startdate);
            System.out.println(enddate);
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://xxx","xxx", "xxx");
            Statement st = con.createStatement();
    
            st.executeUpdate("INSERT INTO auctions (minprice, startprice, startdatetime, enddatetime, itemid) VALUES ('" + minprice + "','" + startprice + "','" + itemid + "','" + ADD CURRENT DATE + "','" + ADD FUTURE DATE + "')");
            con.close();
    
            String URL = "addauction.jsp";
            response.sendRedirect(URL); 
    
        }
        catch (SQLException e){
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
        }
    
    
        %>
    

    我正在建立一个基本的拍卖网站。我需要能够设置创建拍卖的时间,并设置拍卖结束时的未来日期。

    如果你也能教我如何比较时间,那将是非常有帮助的。

    提前谢谢你!

    3 回复  |  直到 7 年前
        1
  •  3
  •   jspcal    7 年前

    如果使用准备好的语句,则可以使用 java.sql.Timestamp 键入。

    String sql = "insert into tbl (col1, col2, col3) values (?, ?, ?)";
    PreparedStatement stmt = dbConnection.prepareStatement(sql);
    stmt.setTimestamp(3, Timestamp.valueOf(new LocalDateTime().now()));
    stmt.executeUpdate();
    

    MySQL也接受 DATETIME 格式为字符串的值 YYYY-MM-DD HH:MM:SS . 您可以使用 DateTimeFormatter :

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
    
    // 2018-08-03 03:50:17
    LocalDateTime.now().format(formatter);
    
    // 2018-09-03 03:50:17
    LocalDateTime.now().plusMonths(1).format(formatter);
    

    现在你要做的就是创造正确的 LocalDateTime 用于表单参数( startdate enddate ),然后绑定 Timestamp 参数或连接格式化的 日期时间 弦。

        2
  •  0
  •   Vitor Darela    7 年前

    首先需要将字符串“startdate”转换为日期对象。然后添加一个未来日期,或者根据需要操作日期。

    //Convert string to date.  
    String string = "January 2, 2010";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh.mm.ss");
    Date date = format.parse(string);
    System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
    
    //Plus future day.
    DateTime dtOrg = new DateTime(date);
    DateTime dtPlusOne = dtOrg.plusDays(1);
    
        3
  •  0
  •   Dorinel Panaite    7 年前

    对我有效的解决方案是将表中的TimeStamp字段配置为默认的Now(),因此每次在DB中添加某些内容而不提供该字段时,都会自动为您填充该字段。