Can CallableStatement be used in place of PreparedStatement?

A Nice Guy

Need some help with a JDBC query. I would have tried it out myself, but right now I do not have access to a database.

My question is: Since CallableStatement extends PreparedStatement, does this mean we can use CallableStatement to execute any query prepared for prepared statement?

More specifically, any downsides to using Callable like this:

CallableStatement stmt = null;
String sql = "UPDATE Employees set age=30 WHERE id=";
      stmt = conn.prepareCall(sql);
      int empID = 102;
      stmt.setInt(1, empID); );
      stmt.execute();
stmt.close();
conn.close();
Maxim

Yes, you can. Difference of prepareCall and prepareStatement methods described in documentation. As you can see they a just optimized for different tasks.

package com.company;

import java.sql.*;

public class Main {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mysql", "user", "password");

        CallableStatement callableStatement = connection.prepareCall("SELECT * FROM db WHERE user = ?");
        callableStatement.setString(1, "deployer");
        ResultSet resultSet = callableStatement.executeQuery();

        while(resultSet.next()) {
            System.out.println(resultSet.getString("Db"));
        }

        connection.close();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can PreparedStatement's 'setObject' method be used for any datatype?

From Dev

Can a different path be used in place of /home?

From Dev

Can a bash array be used in place of eval set -- "$params"?

From Dev

Can "sendkeys" be used in place of "select" for dropdown in selenium webdriver?

From Dev

Can Stripe be used in place of Google IAB for multi-platform apps?

From Dev

Can ASP.Net MVC be used as an API in place of WebAPI?

From Dev

Can a lambda be used to change a List's values in-place ( without creating a new list)?

From Dev

Can angular's ngTouch library be used to detect a long click (touch/hold/release in place) event?

From Dev

How can LINQ return 0 in place of NULL when being used in Sum?

From Dev

can eclipse CDT be used in place of vc++ inorder to call C++ library in node.js by using SWIG?

From Java

Why the JVM cannot be used in place of WebAssembly?

From Dev

Opencart module modification for the place it is used in the catalog

From Dev

Where to place the drawing code in a JPanel used as canvas

From Dev

Where to place miscellaneous widely used functions?

From Dev

Place an image in tableColumn - updateItem is never used

From Dev

Keep objects in place when scroll view is used

From Dev

zip outputs in the wrong place when used in a loop

From Dev

Can I re-use a PreparedStatement if it hits a transient exception?

From Dev

Cassandra PreparedStatement usage with ExecuteAsync can help improve bulk insertion?

From Dev

Eclipse - Can not reach market place

From Dev

Closing resultSet and callableStatement

From Dev

Performance Degradation on CallableStatement

From Dev

A CallableStatement was executed with nothing returned

From Dev

Execute a CallableStatement blocks the application

From Dev

A CallableStatement was executed with nothing returned

From Dev

QSignalSpy can not be used with threads

From Dev

Can memcached be used for locking?

From Java

Can comments be used in JSON?

From Dev

Can characters be used as indices?

Related Related

  1. 1

    Can PreparedStatement's 'setObject' method be used for any datatype?

  2. 2

    Can a different path be used in place of /home?

  3. 3

    Can a bash array be used in place of eval set -- "$params"?

  4. 4

    Can "sendkeys" be used in place of "select" for dropdown in selenium webdriver?

  5. 5

    Can Stripe be used in place of Google IAB for multi-platform apps?

  6. 6

    Can ASP.Net MVC be used as an API in place of WebAPI?

  7. 7

    Can a lambda be used to change a List's values in-place ( without creating a new list)?

  8. 8

    Can angular's ngTouch library be used to detect a long click (touch/hold/release in place) event?

  9. 9

    How can LINQ return 0 in place of NULL when being used in Sum?

  10. 10

    can eclipse CDT be used in place of vc++ inorder to call C++ library in node.js by using SWIG?

  11. 11

    Why the JVM cannot be used in place of WebAssembly?

  12. 12

    Opencart module modification for the place it is used in the catalog

  13. 13

    Where to place the drawing code in a JPanel used as canvas

  14. 14

    Where to place miscellaneous widely used functions?

  15. 15

    Place an image in tableColumn - updateItem is never used

  16. 16

    Keep objects in place when scroll view is used

  17. 17

    zip outputs in the wrong place when used in a loop

  18. 18

    Can I re-use a PreparedStatement if it hits a transient exception?

  19. 19

    Cassandra PreparedStatement usage with ExecuteAsync can help improve bulk insertion?

  20. 20

    Eclipse - Can not reach market place

  21. 21

    Closing resultSet and callableStatement

  22. 22

    Performance Degradation on CallableStatement

  23. 23

    A CallableStatement was executed with nothing returned

  24. 24

    Execute a CallableStatement blocks the application

  25. 25

    A CallableStatement was executed with nothing returned

  26. 26

    QSignalSpy can not be used with threads

  27. 27

    Can memcached be used for locking?

  28. 28

    Can comments be used in JSON?

  29. 29

    Can characters be used as indices?

HotTag

Archive