在Java中使用STR_TO_DATE通过PreparedStatement插入日期 [英] Using STR_TO_DATE in Java to INSERT Date, with PreparedStatement

查看:338
本文介绍了在Java中使用STR_TO_DATE通过PreparedStatement插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's suppose we have a JFrame called FrmRegistration. Its function is inserting data into a table called records.

MySQL的命令 desc记录将导致以下结果:

MySQL's command desc records would result the following:

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

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

Within FrmRegistration there's a JFormattedTextField for birthday input we'll call ftfBirthday. In Netbeans, we put names into components by right-clicking it and going to Properties -> Code tab -> Variable name. Or right-click -> Customize code -> Rename... button.

右键单击该字段,然后转到属性",然后在FormatterFactory中,单击"..."按钮.使用以下代码创建一个自定义字段:####/##/##

Right-click the field and go to Properties, then in FormatterFactory, click the "..." button. Create a customized field with: ####/##/##

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

The reason for a JFormattedTextField is that the user wouldn't lose time by typing the slashes. They appear automatically.

FrmRegistration 中名为插入"的按钮的源代码中应该怎么做?

What should be done in the source-code of a button in FrmRegistration called Insert?

推荐答案

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

Before going to the source-code, right-click the date field and go to Properties. Copy the content of text. It should be (a = one space):

aaaa/aa/aa

aaaa/aa/aa

它将在"}"中使用,否则将在("//".equals(生日)){"行中使用.

(请参阅代码以获取适当的参数)

(See the code for proper parameter)

我添加了一些额外的内容,例如检查字段是否为空.

I added some things extra, like checking if fields are empty.

    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 ");

        }

就是这样.希望它能对某人有所帮助! :-)

That's it. Hope it helps someone! :-)

这篇关于在Java中使用STR_TO_DATE通过PreparedStatement插入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆