【问题标题】:Using STR_TO_DATE in Java to INSERT Date, with PreparedStatement在 Java 中使用 STR_TO_DATE 插入日期,带有 PreparedStatement
【发布时间】:2012-11-03 15:47:06
【问题描述】:

假设我们有一个名为FrmRegistration 的JFrame。它的功能是将数据插入一个名为records的表中。

MySQL 的命令 desc records 会产生以下结果:

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id        | varchar(7)   | NO   | PRI |         |       | 
| name      | varchar(100) | NO   |     | NULL    |       | 
| birthday  | date         | NO   |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

FrmRegistration 中有一个用于生日输入的JFormattedTextField,我们称之为ftfBirthday。在 Netbeans 中,我们通过右键单击组件并转到属性 -> 代码选项卡 -> 变量名来将名称放入组件中。或右键单击 -> 自定义代码 -> 重命名...按钮。

右键单击该字段并转到“属性”,然后在 FormatterFactory 中单击“...”按钮。使用以下命令创建自定义字段:####/##/##

使用 JFormattedTextField 的原因是用户不会因为输入斜线而浪费时间。它们会自动出现。

FrmRegistration 中一个名为 Insert 的按钮的源代码应该做什么?

【问题讨论】:

    标签: java mysql date prepared-statement string-to-datetime


    【解决方案1】:

    在转到源代码之前,右键单击日期字段并转到属性。复制文本的内容。应该是(a = 一个空格):

    aaaa/aa/aa

    它将在“} else if (" // ".equals(birthday)) {" 行中使用。

    (正确参数见代码)​​

    我添加了一些额外的东西,比如检查字段是否为空。

        try {
                    Class.forName("com.mysql.jdbc.Driver");
    
                    try (Connection con = DriverManager.getConnection(
    
                                    "jdbc:mysql://localhost/database_name_here",
                                    "username_here", "password_here")) {
    
                        String if = txtId.getText();
                        String name = txtName.getText();
                        String birthday = ftfBirthday.getText();
    
                        PreparedStatement stmt = con.prepareStatement(
    
                                "INSERT INTO records "
                                + "(id, name, birthday)"
                                + "VALUES(?,?,STR_TO_DATE(?,'%Y/%m/%d'))");
    
                        if (id.isEmpty()) {
                            JOptionPane.showMessageDialog(null,
                                    "The ID field must be completed!");
    
                        } else if (name.isEmpty()) {
                            JOptionPane.showMessageDialog(null,
                                    "The Name field must be completed!");
    
                        } else if ("    /  /  ".equals(birthday)) {
                            JOptionPane.showMessageDialog(null,
                                    "The Birthday field must be completed!");
    
                        } else {
    
                            stmt.setString(1, id);
                            stmt.setString(2, name);
                            stmt.setString(3, birthday);
    
                            stmt.executeUpdate();
    
                            JOptionPane.showMessageDialog(this, " Data was saved successfully! ");
    
                        }
    
                    }
    
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, "SQL command error "
                            + e.getMessage());
    
                } catch (ClassNotFoundException e) {
                    JOptionPane.showMessageDialog(this,
                            " Database driver not found ");
    
            }
    

    就是这样。希望它可以帮助某人! :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2014-09-05
      • 2017-02-11
      • 2021-11-03
      • 2012-08-11
      相关资源
      最近更新 更多