0

我希望我的代码根据格式验证日期,日月也应该不超过日月值......即使我输入任何格式mm/dd/yyyymm/dd/yyyyyyyy/mm/dd它应该验证所有,因为 ms 访问支持所有格式。

以及如何更新它,因为我的代码是:

        Connection connection;
    String text = txtUpdate.getText();
    int Update = Integer.parseInt(text);
    String Name = txtName.getText();
    String Email = txtEmail.getText();
    String Mobile = txtMobile.getText();
    String Address = txtAddress.getText();
    String Dob = txtDob.getText();

            if (check_Name() && check_Email() && check_Mobile() && check_Address() && check_Dob()) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection("jdbc:odbc:NewPData");
            String query = "update Table1 set Name='" + Name + "' , Email='" + Email + "' , Mobile=" + Mobile + ", Address= '" + Address + "', DOB=" +Dob + ", where ID=" + Update;
            PreparedStatement ps_Statement = connection.prepareStatement(query);
            ps_Statement.executeUpdate();
            JOptionPane.showMessageDialog(panelID, "Record Updated Successfully");
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
4

3 回答 3

1

使用java.text.SimpleDateFormat类根据指定的日期格式解析字符串(文本)。

于 2012-12-05T05:44:41.870 回答
0

As for my previous answer, you should use SimpleDateFormat to parse the String to Date.

EDIT:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
   yourDate = sdf.parse( Dob );
} catch ( Exception ex ) {
   // Your Catch Block
}</code>

You may use MMM if your month input happens to be in word. i.e 12-Nov-2012 etc.

EDIT:

To insert the value prior to this format "yyyy-MM-dd HH:mm:ss", add this code under your parse.

Dob = sdf.format(yourDate);

String query = "update Table1 set Name='" + Name + "' , Email='" + Email + "' , Mobile=" + Mobile + ", Address= '" + Address + "', DOB='" +Dob + "' where ID=" + Update;

Date requires single quotation marks. Plus, eliminate the comma after DOB ='" + Dob + "'

于 2012-12-05T06:17:16.707 回答
0

做这个

public java.sql.Date checkdate(String Dob)
{
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    try {
       java.util.Date yourDate = sdf.parse( Dob );
       java.sql.Date yoursqlDate= new java.sql.Date(yourDate.getTime());
       return yoursqlDate;
    } catch ( Exception ex ) {
       // Your Catch Block
    }
    return null;
}
于 2012-12-05T07:11:17.367 回答