Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setObject() method of PreparedStatement

Tags:

Can I use the setObject() method of PreparedStatement for all datatypes (like String, int or double)?

What are the potential problems if I use that?

protected void fillStatement(PreparedStatement stmt, Object[] params)         throws SQLException {          if (params == null) {             return;         }          for (int i = 0; i < params.length; i++) {             if (params[i] != null) {                 stmt.setObject(i + 1, params[i]);             } else {                 stmt.setNull(i + 1, Types.OTHER);             }         }     } 
like image 766
Java P Avatar asked Aug 03 '12 10:08

Java P


People also ask

Which are the parameters of setString () method?

setString. Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database.

Which method is used to create prepare statement?

Creating a PreparedStatement You can create an object of the PreparedStatement (interface) using the prepareStatement() method of the Connection interface. This method accepts a query (parameterized) and returns a PreparedStatement object.

What is addBatch PreparedStatement?

The addBatch() method of Statement, PreparedStatement, and CallableStatement is used to add individual statements to the batch. The executeBatch() is used to start the execution of all the statements grouped together.

What method on a PreparedStatement can you use to execute a select query?

As with Statement objects, to execute a PreparedStatement object, call an execute statement: executeQuery if the query returns only one ResultSet (such as a SELECT SQL statement), executeUpdate if the query does not return a ResultSet (such as an UPDATE SQL statement), or execute if the query might return more than one ...


1 Answers

I use setObject() exclusively with MySQL and I've never had a problem with it. I cannot speak for other databases or other vendors.

like image 117
user207421 Avatar answered Oct 12 '22 16:10

user207421