When the following sql is used to query a database, it throws "java.sql.SQLException: ORA-01722: invalid number" exception.
String name = "Apple";
String sql = new StringBuilder("SELECT * FROM fruit ");
sql.append("WHERE name = ").append(name).toString();
To fix the exception, do one of the followings.
sql.append("WHERE name = '").append(name).append("'").toString();
String sql = new StringBuilder("SELECT * FROM fruit ");
sql.append("WHERE name = ?").toString();
Use the PreparedStatement to set the value.
java.sql.PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
String name = "Apple";
String sql = new StringBuilder("SELECT * FROM fruit ");
sql.append("WHERE name = ").append(name).toString();
To fix the exception, do one of the followings.
1. Indicate that name is a string in the sql by putting single quotes around it
String sql = new StringBuilder("SELECT * FROM fruit ");sql.append("WHERE name = '").append(name).append("'").toString();
2. Use java.sql.Statement to set the value
Get your database java.sql.Connection object. Then, change the sql to the following.String sql = new StringBuilder("SELECT * FROM fruit ");
sql.append("WHERE name = ?").toString();
Use the PreparedStatement to set the value.
java.sql.PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.