java 如何将日期和时间从 JDateChooser 格式化为 mysql datetime 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26589976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to format date and time from JDateChooser to mysql datetime column
提问by Swapnil Sharma
I am trying yo store current date and time from JDateChooser to mysql database, but it inserts only todays date and current time is 00:00:00
我正在尝试将当前日期和时间从 JDateChooser 存储到 mysql 数据库,但它仅插入今天的日期,当前时间为 00:00:00
java.util.Date dt1,dt2;
String n=name.getText();
String i=id.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dt1=jDateChooser1.getDate();
dt2=jDateChooser2.getDate();
String strdtver1=(String) sdf.format(jDateChooser1.getDate());
String strdtver2=(String) sdf.format(jDateChooser2.getDate());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testing","root","");
Statement stmt=con.createStatement();
String sql="Insert into ss values('"+(n)+"','"+(strdtver1)+"','"+(strdtver2)+"','"+(i)+"');";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "RECORD ENTERED SUCCESSFULLY");
} catch(Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
}
It is a program to store check in & check out date and time. The datatype in mysql is datetime for Check_in and Check_out. The data entered in my sql at column Check_in is 2014-10-27 00:00:00. The current date is correct but i want to insert current time instead of 00:00:00.Please help me.
这是一个存储签入和签出日期和时间的程序。mysql 中的数据类型是 Check_in 和 Check_out 的日期时间。在我的 sql 列 Check_in 中输入的数据是 2014-10-27 00:00:00。当前日期是正确的,但我想插入当前时间而不是 00:00:00。请帮助我。
回答by guido
Your are formatting the dates you get from widgets using SimpleDateFormat
, but you only use a date pattern as a format string; for date and time string format supported by mysql, use:
您正在使用 格式化从小部件获得的日期SimpleDateFormat
,但您只使用日期模式作为格式字符串;对于 mysql 支持的日期和时间字符串格式,请使用:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
回答by VGR
You should not convert your Date objects to Strings. Instead, pass them to a PreparedStatement:
您不应将日期对象转换为字符串。相反,将它们传递给 PreparedStatement:
try (PreparedStatement statement =
con.prepareStatement("insert into ss values ?, ?, ?, ?")) {
statement.setString(1, n);
statement.setDate(2, dt1);
statement.setDate(3, dt2);
statement.setString(4, i);
statement.executeUpdate();
}
Also, using a PreparedStatement is highly recommended when storing user data, as it prevents a serious malicious attack vector known as SQL Injection. (Imagine if the user entered his name as ', null, null, null; delete from ss; insert '
.)
此外,强烈建议在存储用户数据时使用 PreparedStatement,因为它可以防止称为SQL 注入的严重恶意攻击媒介。(想象一下,如果用户将他的名字输入为', null, null, null; delete from ss; insert '
。)
回答by Deepak Singh
public void calendarExpale(){
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
final JTextField textField = new JTextField(15);
JDateChooser chooser = new JDateChooser();
Date d1=(Date)chooser.getDate();
chooser.setSize(15, 10);
chooser.addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
JDateChooser chooser = (JDateChooser)evt.getSource();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
textField.setText(formatter.format(chooser.getDate()));
}
});
content.add(textField);
content.add(chooser);
}
回答by Humphrey
This is the easiest way I have ever used. You do not need to do any extra thing .
这是我用过的最简单的方法。你不需要做任何额外的事情。
statement.setString(2,((JTextField) jDateChooser1.getDateEditor().getUiComponent()).getText());