Database Error
The SQL syntax error or exception 'ORA-01722: invalid number' occurred while executing the PreparedStatement
What This Error Means
This error is thrown when the database encounters an invalid number while executing a SQL query. This can happen when the data type of the column in the database does not match the data type of the value being inserted or updated.
Why It Happens
This error occurs when the data type of the column in the database does not match the data type of the value being inserted or updated. For example, if a column is defined as NUMBER data type and a string value is being inserted into it, the database will throw this error.
How to Fix It
- 1To fix this error, you need to ensure that the data type of the column in the database matches the data type of the value being inserted or updated. You can do this by casting the value to the correct data type before executing the SQL query. Here's an example:
- 2// Before (broken code)
- 3PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (salary) VALUES (?)");
- 4pstmt.setDouble(1, "50000");
- 5// After (fixed code)
- 6PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (salary) VALUES (?)");
- 7Double salary = Double.parseDouble("50000");
- 8pstmt.setDouble(1, salary);
Example Code Solution
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DatabaseErrorExample {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "username", "password");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (salary) VALUES (?)");
pstmt.setDouble(1, "50000");
pstmt.executeUpdate();
}
}import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DatabaseErrorExample {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "username", "password");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (salary) VALUES (?)");
Double salary = Double.parseDouble("50000");
pstmt.setDouble(1, salary);
pstmt.executeUpdate();
}
}Fix for The SQL syntax error or exception 'ORA-01722: invalid number' occurred while executing the PreparedStatement
Browse Related Clusters
Related JAVA Errors
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error