【问题标题】:I'm sending a month type string from JSP to servlet, then to dao from servlet. How can I get the Year and month from this string?我将月份类型的字符串从 JSP 发送到 servlet,然后从 servlet 发送到 dao。如何从此字符串中获取年份和月份?
【发布时间】:2019-07-03 23:19:00
【问题描述】:

我应该如何在 mysql 中将字符串转换为 Year-MMMM 格式,还是必须在控制器部分进行转换? 假设:2019-02 被保存为 2019-02,但我想要另一列将 2019-02 转换为 2019-February。

JSP 部分

<form action="YearMonthController" method="post">

    <input type="hidden" name="command" value="CREATE_YearMonth" />

    Month with year: <input type="month" name="year_month">

    <input type="submit">

</form>

这是控制器部分

private void create_a_yearMonth_controller(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    String _yearMonth = request.getParameter("year_month");

    YM ym = new YM(_yearMonth);

    _ymDAO.create_a_yearMonth_dao(ym);

}

这是 DAO 部分

public void create_a_yearMonth_dao(YM ym) throws Exception {

    Connection connection = null;
    PreparedStatement prepared_statement = null;

    try {
        connection = data_source.getConnection();

        String sql = "INSERT INTO `years_months`\r\n" + "(yearMonth) \r\n" + "VALUES \r\n" + "(?);";

        prepared_statement = connection.prepareStatement(sql);

        prepared_statement.setString(1, ym.get_yearMonth());

        prepared_statement.execute();
    } finally {
        close(connection, prepared_statement, null);
    }

}

这是桌子

CREATE TABLE `years_months` (
yearMonth VARCHAR(50) NOT NULL,
PRIMARY KEY (yearMonth)
);

【问题讨论】:

  • 我建议您将年月存储为日期,也许将天始终设置为 1,然后您可以使用标准格式函数在 sql 或 java 中格式化日期。此外,在要与日期进行比较的查询中使用该列会容易得多。
  • 如果我使用日期,我会收到以下错误。 HTTP 状态 500 - com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的日期值:第 1 行的列“yearMonth”的“2019-02”
  • 当然你需要给一天,就像我在之前的评论中提到的那样,只需将它设置为 1
  • 您能详细说明一下吗?会很有帮助。
  • 不确定您的意思,只需在存储之前将'-01'添加到字符串中。 Mysql应该能够自动将格式为'yyyy-mm-dd'的字符串转换为日期

标签: mysql jdbc java-time date


【解决方案1】:

由于 MySQL 没有年月数据类型(至少我认为没有),我会始终将年月单独存储为 2019-02(因此,varchar(50) 列对于此目的来说相当宽,@ 987654323@ 可以)。这是 ISO 8601 格式并得到广泛认可。

在 Java 中,我会使用 YearMonth 来表示年份和月份(无需发明自己的类)。并根据演示的需要对其进行格式化。 YearMonth.toString 生成提到的 ISO 8601 格式,YearMonth.parse 将其解析回来,所以一切都很好地结合在一起。

public void createAYearMonthDao(YearMonth ym) throws SQLException {
    String sql = "INSERT INTO `years_months` (yearMonth) VALUES (?);";

    try (Connection connection = data_source.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setString(1, ym.toString());
        preparedStatement.execute();
    }
}

在需要时生成类似2019-February 的字符串:

    DateTimeFormatter ymFormatter = DateTimeFormatter.ofPattern("uuuu-MMMM", Locale.ENGLISH);

    YearMonth ym = YearMonth.of(2019, Month.FEBRUARY);
    String formattedYm = ym.format(ymFormatter);
    System.out.println(formattedYm);

输出:

2019 年 2 月

YearMonth有分别获取年月的方法,例如:

    System.out.println("Year " + ym.getYear() + " month " + ym.getMonth());

2019 年 2 月

cmets 中关于在 MySQL 中使用 date 和在 Java 中使用 LocalDate 的替代建议也不错。优点:您在保存和检索时保存格式和解析。缺点:您的数据库中有一个多余的月份。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2020-12-19
    • 2012-02-17
    • 2021-01-30
    • 2012-08-28
    • 2021-01-25
    相关资源
    最近更新 更多