Good afternoon,
I'm trying to get a String from a JTextField. Then convert it to Date for afterwards sending that into a MYSQL statement.
String nom = jTextField_Name.getText().toUpperCase();
int temp = Integer.parseInt(jTextField_Temp.getText());
String d = jTextField_Date.getText();
System.out.println("Inputs taken");
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date parsed = (Date) format.parse(d);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
System.out.println("Converted to proper Date for MYSQL");
int retorn = db.insertTemperatura(nom, sql, temp);
if (retorn == 1){
jTextArea_Result.setText("S'ha inserit "+nom+" amb la seva temperatura correctament!");
} else{
jTextArea_Result.setText("-----Hi ha hagut un ERROR a l'hora d'inserir "+nom+" amb la seva temperatura.\nIntenta-ho de nou sisplau.");
}
} catch (ParseException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
The thing is, that i'm not doing the conversion right, i've tried diferent ways and I always came up with that error:
run:
Llegides entrades
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
I've used other types of format but I don't know what I'm doing wrong.
Thanks in advance!
The method I call for that insertion into the DDBB is:
public int insertTemperatura(String nom, Date data, int temperatura){
try {
Statement s = c.createStatement();
String query = "INSERT INTO TaulaTemp VALUES ('"+nom+"',"+temperatura+","+data+")";
int numRows = s.executeUpdate(query);
if (numRows > 0){
return 1;
}
return 0;
} catch (SQLException ex) {
Logger.getLogger(DDBB.class.getName()).log(Level.SEVERE, null, ex);
return -1;
}
}