date java 输入时间,在Java中使用STR_TO_DATE通过PreparedStatement插入日期

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

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 | |

+-----------+--------------+------+-----+---------+-------+

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.

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

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

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

解决方案

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

It will be used in the "} else if (" / / ".equals(birthday)) {" line.

(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! :-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值