Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of JDBC?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the concept of JDBC". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The concept of JDBC: doMaim objects (javaBean entities)

UserDao (DataAccessObject)

Factory mode, Factory eliminates the dependence of UserDaoTest business classes on the concrete implementation of UserDaoJdbc classes

Read the configuration file and load the information by reading the configuration file by the factory. The implementation class uses the factory to obtain the object that implements the interface class.

The concept of JDBC: factories generally use singleton mode

Factory-- > produce products (Dao this product)

Class loader, not only can load.class, but also load other files

Transaction concept and processing

1. Atomicity: cannot be split, the statements that make up the transaction form a logical processing unit, and cannot execute only part of it

two。 Consistency: after the transaction is completed, the integrity constraints of the data are maintained.

3. Isolation: the impact of one transaction on another transaction

4. Persistence: the result of the transaction can be saved by *.

Step: open transaction connection.setAutoCommit (false)

Commit transaction: connection.commit ()

Roll back the transaction: connection.rollback ()

Jdbc commits automatically by default, and client connections also commit automatically. To open a transaction, turn off autocommit.

SavePoint: savePoint

Savepointsp=null

Sp=conn.setSavepoint ()

Conn.rollback (sp)

-

The concept of JDBC: transactional JTA across multiple databases

JTA Container weblogic,websphere

Submit in two phases.

Using a JNDI server

Javax.transaction.UserTransactiontx=

(UserTransaction) ctx.lookup ("jndiName")

Tx.begin ()

/ / connection1connection2 (may come from different databases)

Tx.commit (); / / tx.rollback ()

-

Isolation level of the transaction:

Connction.setTransactionIsolation (Connection.TRANSACTION_READ_COMMITTED)

Isolation level

Read unsubmitted (Readuncommitted) may cause dirty reading, non-repeatable reading, phantom reading.

Read submitted (Readcommitted) may not be repeatable, phantom reading

Repeatableread may cause phantom reading (phantom data, perform inserting a piece of data, which may be seen on another client)

Serializable (Serializable)

The implementation of the isolation level varies from database to database.

About stored procedures (used a lot in a two-tier architecture)

Systems with a three-tier architecture are rarely used. Skip it and skip it.

Several other useful API

Methods provided by the jdbc3.0 specification

Insert a record, it can return the primary key

PreparedStatement.getGeneratedKeys ()

PreparedStatementps=connection.prepareStatement (sql,Statement.RETURN_GENERATED_KEYS)

Ps.executeUpdate ()

ResultSetrs=st.getGeneratedKeys ()

Rs.getInt (1)

Batch processing: can greatly increase the speed of a large number of additions, deletions and changes (not absolutely), related to specific databases, and may not necessarily improve performance

PreparedStatement.addBatch ()

PreparedStatement.executeBatch ()

Scrollable result sets and paging features

Statementst=conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)

ResultSet.TYPE_SCROLL_SENSITIVE-- is sensitive to the database, that is, the records in the database have changed, and the result assembly is aware (but some databases do not necessarily follow this specification, mysql does not)

ResultSet.CONCUR_UPDATABLE-- is updatable, that is, changing the contents of the result set can affect the database

ResultSet.CONCUR_READ_ONLY-- read-only

Scrollable result set

Statementst=connection.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)

ResultSetrs=st.executeQuery (sql)

Rs.beforeFirst ()

Rs.afterLast ()

Rs.first ()

Rs.isFirst ()

Rs.last ()

Rs.isLast ()

Rs.absolute (9)

Rs.moveToInsertRow ()

Conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)

Rs.updateString ("colname", "newvalue")

Rs.updateRow ()

Mysql paging for example:

Selectid,name,money,birthdayfromuserlimit150,10

Start with 150 records in the user table and take 10 records

Jdbc metadata information database metadata information, parameter metadata information

DatabaseMetaDatameta=connection.getMetaData ()

Database-related information can be obtained through DatabaseMetaData, such as:

Database version, database name, database vendor information, whether to support transactions, whether to support a certain transaction isolation level

Whether scrolling the result set is supported, etc.

ParameterMetaDatapmd=preparedStatement.getParameterMetaData ()

Parameter information can be obtained through ParameterMetaData.

ParameterMetaDatapmd=ps.getParameterMetaData ()

Intcount=pmd.getParameterCount ()

System.out.println ("count=" + count)

For (inti=1;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report