SetObject() method, error Parameter index out of range and com. The MySQL. JDBC. Exceptions. Jdbc4. MySQLSyntaxErrorException error
Learn how to use the setObject() method in PreparedStatement for placeholders in SQL procedures. , appear when to replace the Parameter index out of range error:
1, the number of placeholders, and transfer the Object [] array element number
2, different placeholders used in Chinese?Rather than?
If the above situation is modified, the com. Mysql. JDBC. Exceptions. Jdbc4. MySQLSyntaxErrorException error
see first in the use of setObject () method before and after the difference between a PreparedStatement object:
Object[] objects = new Object[]{"name","age","xixi","23"};
sql = "insert into student (?,?) values(?,?);";
//Get the execution object (connection as Connection object)
preparedStatement = connection.preparedStatement(sql);
System.out.println(preparedStatement.toString());
// determine if the parameter exists, and replace it
if (obs!=null&&obs.length>0){
for (int i = 0; i < obs.length; i++) {
preparedStatement.setObject(i+1,obs[i]);
}
}
System.out.println(preparedStatement.toString());
Perform before:
com. Mysql. JDBC. JDBC42PreparedStatement @ 443 b7951:
insert into student (* * NOT SPECIFIED, the NOT SPECIFIED) values (NOT SPECIFIED, the NOT SPECIFIED * *);
com after execution. Mysql. JDBC. JDBC42PreparedStatement @ 443 b7951:
insert into student (” name “, “age”) values (‘ hee hee ‘, ’23’);
All ** Not SPECIFIED ** placeholders are replaced by elements in the Objects[] array with single quotes.
in the mysql database, the parameters need to add single quotation marks, it is enclosed within a string value. Column names, table names, and so on use backquotes to distinguish them from special keywords (the symbol to the left of the 1 digit).
So if you want to replace non-attribute values, you should choose other methods such as concatenation SQL statements to prevent errors.
Read More:
- MySQL Build table error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
- [Solved] C# connecting to MySQL database reports an error
- [Solved] SQLSTATE[HY000] [2002] Connection refused to report an error when PHP connects to mysql in the docker container
- MYSQL Insert Data Error: check the manual that corresponds to your MySQL server version for the right syntax
- Mysql 8.0.13 Enabling remote access (ERROR 1064 (42000): You have an error in your SQL syntax; check the manual th)
- [Solved Perfectly] MySQL ERROR 1064 (42000): You have an error in your SQL syntax;
- MySQL Change password failure prompt: ERROR 1064(42000):You have an error in your SQL syntax: check the corresponds to your M
- MYSQL Use cmd to change root password error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha
- How to Solve “Out of range value for column” Error
- [Solved] pymysql.err.ProgrammingError: (1064, ‘You have an error in your SQL syntax;
- [Solved] Sqlyog always reports an error when creating a new connection.
- Doris reports an error: error 1064 (HY000) [How to Solve]
- Bulk Update Error: #Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the m
- [Solved] java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corres
- After installing mysql5.7 on centos7, an error 1045 (28000) is reported: access denied for user ‘root’ @’localhost ‘(using PAS)
- [Solved] QuestDB Exception–ERROR: Cannot insert rows out of order.
- MYSQL Index Key Length 1071 – Specified key was too long; max key length is 3072 bytes
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- Error 1064 (42000): you have an error in your SQL syntax
- [Solved] there are special symbols in the initial password for installing MySQL in Hadoop, and an error is reported