2

Hi i am trying to execute query

Statement stmt = conn.createStatement();
stmt.executeUpdate(location_query);

location_query is

insert into table(col, col2) value("2", "");

Both col and col2 are double(12,2) type, i get error Data truncated for column 'col2' at row 1, but if I print my query and copy, and paste it to PMA (PhpMyAdmin) its correct.

I guesst that its becouse value of col2 is empty, but is there chance to solve this in other way not to checking all variables which im trying to put to my query ??

1
  • no its not default null, but I can change it Commented Jun 1, 2011 at 9:56

1 Answer 1

5

Well, an empty String is not a valid number, therefore different database systems might handle that differently. When you just want that column NULL or filled with the default, you should leave it out like this:

insert into table(col) value(2);

OR set it NULL explicitly:

insert into table(col, col2) value(2, NULL);

Besides, numbers are written without quotes in SQL. Furthermore, you should cosider using PreparedStatements only. PreparedStatements give you type saftly and protect from SQL injection and might be faster when reused.

Take a look at google "statement vs. preparedstatement"

EDIT

Your example would look something like this, when using prepared statements:

String yourFirstString = "2.34";
String yourSecondString = "";

double yourFirstDouble = 0;
double yourSecondDouble = 0;

if(yourFirstString  != null && yourFirstString.trim().length() > 0){
  try {
     yourFirstDouble = Double.parseDouble(yourFirstString); 
  }catch(NumberFormatException e){
    // handle Exception here
  }
}

// same parsing for the second double

PreparedStatement stmt = conn.prepareStatement("insert into table (col, col2) value(?, ?)");
stmt.setDouble(1, yourFirstDouble);
stmt.setDouble(2, yourSecondDouble);
stmt.executeUpdate();
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but will preparedStatements protect me against empty values ?? and secound thing is that i never know which value is empty thats why I have to put all of them
Since ´setDouble(int, double)´ is type save you can only use valid doubles as argument. That means, you can not apply an empty String to that Statement. You should test if the String is empty and and if so, set 0 or NULL. Otherwise, you should try to parse the String into a double and use it. I'll edit my answer and add an example.
I added some parsing example. However, all that logic should not be in the same place. The parsing of the values, user entries I would guess, should be validated when entered somewhere near the GUI logic. The database layer however, should only get doubles and not do string parsing, validation and the like.

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.