Reputation: 3
I have a database name "ward data base" in which a table names patients has a column "date of admission" whose data type is "date". Now the problem is when the data is filled in a jframe form it gets updated in that database, all the other data types string but when it comes to get text for the date column it shows error. so what "Get.datatype" should i use to get the date entered in the jfield and saved in data base. Here is the code.
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);
}
These are the columns names and there relative jtextfields. In this the date variable shows error
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);
}
}
Upvotes: 0
Views: 557
Reputation: 71
Suppose you have i'th column as date in database.
java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
date = new java.util.Date(timestamp.getTime()));
Upvotes: 2