• UCANACCESS With JDBC Prepared Statement

    UCANACCESS with JDBC Prepared Statement


    In UCANACCESS with JDBC prepared statement, the PreparedStatement address the following requirements:

    1. Creating parametrised statements such that data for parameters can be substituted dynamically.
    2. Creating statements involving data values that cannot always be represented as character strings.
    3. Pre-compiling SQL statements to avoid repeated compiling of the same SQL statements.

    In most cases, you may not have the complete information to construct a WHERE clause in SQL. For instance, to write a SQL SELECT statement to select the data of a user, while writing the JDBC code, you need to know the primary key value to construct the SQL. In most cases, such information is available only at runtime. Prepared statements address this problem by providing for parameters in SQL.

    Instead of using values, you may use “?” qualifiers in SQL. So instead of creating a statement with the SQL string:

    SELECT <select_fields> FROM <table_name> WHERE USER_ID = <value>

    You can use a prepared statement with the SQL string:

    SELECT <select_fields> FROM <table_name> WHERE USER_ID = ?

    You can substitute values using methods on the java.sql.PreparedStatement interface.

    The same applies to SQL involving complex data types such as long text data, binary data, or even timestamp data. Such data types cannot be expressed as such plain string. For instance, how would we create a SQL statement to update image data? Since prepared statements are parameterized, instead of expressing such data type sin the SQL statement directly, you can set the data using various methods on the java.sql.PreparedStatement interface.

    In addition, the same SQL statement can be executed many times with different parameters and the database can compile such statements just once, this improving performance. A PreparedStatement object can hold precompiled SQL statements. The following methods on the java.sql.Connection interface let us create PreparedStatement objects:

    PreparedStatement prepareStatement(String sql) throws SQLExceptionPreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency) throws SQLException

    JDBC PreparedStatement – Example

    Here I have created simple Java DataBase Connectivity (JDBC) PreparedStatement example that show you the use of PreparedStatement class and prepareStatement() method with several DataBase operations like, Insert, Save.

    UCANACCESS with JDBC PreparedStatment Exmple

    Source Code Download:ucanaccess_PreparedStmtExample

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *