1

This is more of an efficiency question: When performing JDBC in an Android application, should I use preparecall or createstatement with a stored procedure? Here is an example of a method in my code which calls a stored procedure and works:

public static ResultSet getUserNames (String userid) {
    ResultSet rs = null;
    try {
        cstmt = con.prepareCall("{call dbo.StoredProcedure(?)}");
        cstmt.setString("@ID", userid);
        cstmt.setQueryTimeout(10);
        rs = cstmt.executeQuery();
        return rs;

    } catch (Exception e) {
        Log.e("DB Error:", e.getMessage());
        return null;
    }
}
Jay
  • 614
  • 5
  • 22
  • 2
    In terms of efficiency, [Android should not use JDBC connections](http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android#15853566) anyway. – OneCricketeer Feb 14 '17 at 22:09
  • It depends, performance wise it probably doesn't matter. – Mark Rotteveel Feb 15 '17 at 10:28
  • 1
    @MarkRotteveel - [This MSDN article](https://msdn.microsoft.com/en-us/library/ms403294.aspx) refers to ODBC, but if the JDBC driver works the same way (which seems likely) and a `CallableStatement` uses RPC then it should be more efficient than sending an `EXEC ...` using a `Statement` or `PreparedStatement` object. Of course, whether it is *significantly* faster is another matter. – Gord Thompson Feb 15 '17 at 13:43
  • @GordThompson If we really want to know, we could check out the [mssql-jdbc](https://github.com/Microsoft/mssql-jdbc) sources ;) – Mark Rotteveel Feb 15 '17 at 13:59
  • @MarkRotteveel - As it turns out, mssql-jdbc currently does not "directly" call a Stored Procedure when we use a CallableStatement; it maps to an `sp_executesql N'EXEC ...'`call. However, [that may change](https://github.com/Microsoft/mssql-jdbc/pull/263). – Gord Thompson Apr 28 '17 at 15:33

0 Answers0