我有一个数据库名称"病房数据库"其中一个表名患者有一个栏目"入院日期"其数据类型为" 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);
}
这些是列名称和相对的jtextfields。在此,日期变量显示错误
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);
}
}
答案 0 :(得分:2)
假设您有第i列作为数据库中的日期。
java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
date = new java.util.Date(timestamp.getTime()));