JDBC API Usage: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * JDBC =Query= =Create= =Update= =Delete=")
 
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[JDBC#JDBC_API_Usage|JDBC]]
* [[JDBC#JDBC_API_Usage|JDBC]]
=JDBC Connection Life Cycle=
{{Internal|DataSource#Connection_Life_Cycle|JDBC Connection Life Cycle}}
=Database Metadata=
The database metadata is an object that contains metadata about the database the current connection belongs to.
<syntaxhighlight lang='java'>
Connection c = ...;
DatabaseMetaData dmd = c.getMetaData();
</syntaxhighlight>
=ResultSet=
Some implementation will fail on ResultSet.first() invocation, if the result sent is not explicitly created to be ResultSet.TYPE_SCROLL_INSENSITIVE. Creating scrollable ResultSets may have performance implications. ResultSet.next() is always safe to use, in an integrator pattern.


=Query=
=Query=
<syntaxhighlight lang='java'>
...
private DataSource dataSource;
...
Connection c;
try {
    c = dataSource.getConnection();
    PreparedStatement s = c.prepareStatement("SELECT * FROM SOME_TABLE");
    ResultSet rs = s.executeQuery();
    while(rs.next()) {
        //
        // columns are 1-based
        //
        String column1Value  = rs.getString(1);
        Long column2Value  = rs.getLong(2);
        ...
    }
}
finally {
    c.close();
}
</syntaxhighlight>
<font color=darkgray>More low-level JDBC code example with comments in [[SIA]] page 57.</font>


=Create=
=Create=

Latest revision as of 20:03, 14 October 2018

Internal

JDBC Connection Life Cycle

JDBC Connection Life Cycle

Database Metadata

The database metadata is an object that contains metadata about the database the current connection belongs to.

Connection c = ...;
DatabaseMetaData dmd = c.getMetaData();

ResultSet

Some implementation will fail on ResultSet.first() invocation, if the result sent is not explicitly created to be ResultSet.TYPE_SCROLL_INSENSITIVE. Creating scrollable ResultSets may have performance implications. ResultSet.next() is always safe to use, in an integrator pattern.

Query

...

private DataSource dataSource;

...

Connection c;

try {

    c = dataSource.getConnection();

    PreparedStatement s = c.prepareStatement("SELECT * FROM SOME_TABLE");

    ResultSet rs = s.executeQuery();

    while(rs.next()) {

        //
        // columns are 1-based
        //

        String column1Value  = rs.getString(1);
        Long column2Value  = rs.getLong(2);

        ...
    }
}
finally {

    c.close();
}

More low-level JDBC code example with comments in SIA page 57.

Create

Update

Delete