我希望我的代码根据格式,日期月份来确认日期,因为它不会超过日月值......即使我输入任何格式 mm / dd / yyyy 或 mm / dd / yyyy 或 yyyy / 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();
}
}
答案 0 :(得分:1)
使用java.text.SimpleDateFormat
类按照指定的日期格式解析字符串(文本)。
答案 1 :(得分:0)
至于我之前的回答,你应该使用SimpleDateFormat来解析String to Date。
编辑:
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;
try {
yourDate = sdf.parse( Dob );
} catch ( Exception ex ) {
// Your Catch Block
}</code>
答案 2 :(得分:0)
DO THIS
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;
}