我有一个数据库名称为"ward database ",其中一个以患者命名的表有一个列"入院日期",其数据类型为"date"。现在的问题是,当数据在jframe表单中填充时,它在数据库中得到更新,所有其他数据类型都是字符串,但当它为日期列获取文本时,它显示错误。那又怎么样?"数据类型"我应该使用它来获得在jfield中输入的日期并保存在数据库中。代码如下:
private void UpdateActionPerformed(java.awt.event.ActionEvent evt) {
newentry = new Patients();
Connection conn= null;
PreparedStatement st =null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (ClassNotFoundException | InstantiationException |IllegalAccessException ex)
{
Logger.getLogger(New_Patient_Entry.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/warddatabase","admin","admin");
}
catch(SQLException ex){
Logger.getLogger(New_Patient_Entry.class.getName()).log(Level.SEVERE, null, ex);
}
这些是列名和它们的相对jtextfield。在这里,date变量显示错误
newentry.patientID = pat_id.getText();
newentry.name = pat_name.getText();
newentry.age= pat_age.getText();
newentry.unit=pat_unit.getText();
newentry.sex=pat_sex.getText();
newentry.diagonis=diagonisis.getText();
newentry.dateOfAddmission=DOA.getDate();
newentry.treatmentPlan=treat_plan.getText();
String sql1 = "Insert into patients (Patient_ID,Name,Age,Unit,Sex,Diagonsis,DateOfAddmission,Treatment_Plan) values ('" +newentry.patientID+ "', '"+newentry.name+"', '"+newentry.age+"','"+newentry.unit+"','"+newentry.sex+"','"+newentry.diagonis+"','"+newentry.dateOfAddmission+"','"+newentry.treatmentPlan+"')";
try {
st = conn.prepareStatement(sql1);
}
catch (SQLException ex) { Logger.getLogger(New_Patient_Entry.class.getName()).log(Level.SEVERE, null, ex);
}
try {
st.executeUpdate(sql1);
} catch (SQLException ex) {
Logger.getLogger(New_Patient_Entry.class.getName()).log(Level.SEVERE, null, ex);
}
}
假设数据库中有第i列作为日期。
java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
date = new java.util.Date(timestamp.getTime()));