Jarvis Hoo
Jarvis Hoo

Reputation: 5

can't get parameter with a value of integer in SQL query

I'm trying to get the integer(age information) in my SQL query using this method

// ...
Integer age = 42;

String sql = "insert into user_info value('" + username + "','" + passcode + "','" + gender + "',age,'" + email + "')";

try {
    PreparedStatement preparedStatement = con.prepareStatement(sql);
} catch (SQLException e) {
    e.printStackTrace();
}
try {
    i = stmt.executeUpdate(sql);
} catch (SQLException e) {
    e.printStackTrace();
}

finally i got the result of successfully inserting all the information, username, passcode, gender and email, but for the AGE info, it just shows NULL in my mysql database table, and i have tried so hard to fix this but still got confused, please help me out, thx:)

Upvotes: 0

Views: 252

Answers (2)

Mr Rubix
Mr Rubix

Reputation: 1519

You got an error in you sql query

String sql = "insert into user_info value('" + username + "','" + passcode + "','" + gender + "',age,'" + email + "')";

This will not insert the age value, so age will be set to his default value (NULL)

Try with this query

String sql = "insert into user_info value('" + username + "','" + passcode + "','" + gender + "','" + age + "','" + email + "')";

Upvotes: 0

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Correct your code first to and then try.

gender + "'," + age + ",'" + email

Secondly you are not using PreparedStatement in right manner. In PreparedStatement you set the dynamic values and do concatenate the SQL like this. Check this https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

Upvotes: 2

Related Questions