0

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;
    }
}
7
  • 1
    You can pass directly a java.util.Date to mySQL... is not necesary Parse it to java.sql.Date Commented Mar 3, 2017 at 16:24
  • 1
    Possible duplicate of How to convert java.util.Date to java.sql.Date? Commented Mar 3, 2017 at 16:24
  • 2
    You can't simply cast a java.util.Date to a java.sql.Date... Commented Mar 3, 2017 at 16:25
  • 1
    You have not shown the important code, i.e. the definition of insertTemperatura. Please edit you post and include that code. Commented Mar 3, 2017 at 16:39
  • 2
    Do not build your SQL like that, use a PreparedStatement with placeholders instead. Your code is wide open to SQL-Injection attacks. Commented Mar 3, 2017 at 16:39

1 Answer 1

1

You can pass directly a java.util.Date to mySQL... is not necessary Cast it to java.sql.Date

Just delete this line:

java.sql.Date sql = new java.sql.Date(parsed.getTime());

and pass in this line the variable casted

int retorn = db.insertTemperatura(nom, parsed, temp);

UPDATE:

You should use a preparedStatement clause instead of put all your query in a String, like this:

String query = "INSERT INTO TaulaTemp VALUES (?,?,?)";

PreparedStatement preparedStatement = dbConnection.prepareStatement(query);
preparedStatement.setString(1, nom);
preparedStatement.setDate(2, data);
preparedStatement.setInt(3, temperatura);
// execute insert SQL stetement
preparedStatement .executeUpdate();

Remember to use PreparedStatement add the necesary import statement:

import java.sql.PreparedStatement;
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Dazak, thanks for the answer. I've just tried and it keeps me saying the same. Can it be an error from the input type I get from my method insertTemperatura? I've just updated the question with it's code, maybe it's there where I should change it :S
As @JimGarrison said... use a PreparedStatement... I update my answer so you can see it
Now I've changed the code with the prepared statement but the error seems to be in the same part. As I've put system.out.prints, it seems that it's not passing the line: Date parsed = (Date) format.parse(d);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.