Jdbc basic query method

Source: Internet
Author: User

Jdbc basic query method

The three most basic interfaces for jdbc database operations are Statement PrepareStatment CallableStatement.

(1) Statement

createStatement() 

createStatement(int resultSetType, int resultSetConcurrency) 

CreateStatement(Int resultSetType, int resultSetConcurrency, int resultSetHoldability) (JDBC 3.0)

resultSetType-Result set type, which isResultSet. TYPE_FORWARD_ONLY (the result set cannot be rolled),ResultSet. TYPE_SCROLL_INSENSITIVE (rolling result set, affected by database updates))OrResultSet. TYPE_SCROLL_SENSITIVE (rolling result set, noAffected by database updates)One

resultSetConcurrency-Concurrency type; it isResultSet.CONCUR_READ_ONLY(Read-only result set) orResultSet. CONCUR_UPDATABLE (updatable result set).

resultSetHoldability-BelowResultSetConstant:ResultSet.HOLD_CURSORS_OVER_COMMIT(After modification is submitted, the result set is not closed) orResultSet. CLOSE_CURSORS_AT_COMMIT (disable the result set after modification is submitted)

Note: The use of resultSetType, resultSetConcurrency, and resultSetHoldability is limited.

Some databases do not support rolling result sets. They can update result sets, but do not close the result sets after the modification is submitted. You can use conn. getMetaData () to obtain the DataBaseMetaData object and determine whether the above functions are supported.

(2) PrepareStatement

prepareStatement(String sql) 

prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 

prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 

 

prepareStatement(String sql, int autoGeneratedKeys) 

prepareStatement(String sql, int[] columnIndexes) 

prepareStatement(String sql, String[] columnNames) 

AutoGeneratedKeys can be Statement. RETURN_GENERATED_KEYS/NO_GENERATED_KEYS

Eg: PrepareStatement st = conn. prepareStatemnt (SQL, Statement. RETURN_GENERATED_KEYS );

ResultSet rs = st. getGeneratedKeys ();

If (rs. next ()){

Int key = ts. getInt (1 );

}

 

(3) CallableStatement

PrepareCall(String SQL)

prepareCall(String sql, int resultSetType, int resultSetConcurrency) 

prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.