20145239 Java Programming 9th Week of study summarySummary of learning contents of textbookGetting started with JDBC
Introduction to JDBC
1.JDBC is the standard specification for the Java online database, which defines a set of standard classes and interfaces that the application needs to invoke when an online database is called.
2.JDBC Standard: JDBC Application developer interface and JDBC driver developer interface.
Connecting to a database
1. To connect to the database system, you must have a vendor-operated JDBC driver, and you must set the driver jar documentation in CLASSPATH.
2. The object that operates the driver interface is the starting point for JDBC for database access. The Operation object of the connection interface is the database online representative object, in order to get the connection operand, it can be drivermanager by the
Getconnection (): Connection conn = drivermanager.getconnection (Jdbcurl, username, passwd);
3. After the database has been completed, if it is determined that the connection is no longer needed, the connection to the database must be close() closed to release the necessary resources related to the connection, such as online related objects, authorized resources, and so on.
Using statement, ResultSet
1. Use connection to createStatement() build statement objects.
2.Statement is used to execute SQL, and you can test that SQL is executing a query or update, which returns true to indicate that SQL execution will return resultset as the result of the query. execute()
Using PreparedStatement, CallableStatement
1. Use the connection prepareStatement() method to build a pre-compiled SQL statement, where the parameters will be changed to specify the first part of the "? "This placeholder character.PrepareStatement stmt = conn.prepareStatement("INSERT INTO t_message VALUES(?, ?, ?, ?)");
2. When you need to really specify the parameters to execute, then use the corresponding setInt() , and setString() other methods to specify "? "Where the parameters are really supposed to be.
Stmt.setint (); Stmt.setstring (2, "Momor"); Stmt.executeupdate ();
JDBC Advanced
Use DataSource to get online
1. When Messagedao users cannot tell DriverManager about the JDBC URL, user name, password, etc., you can let Messagedao rely on the javax.sql.DataSource interface, through its defined getconnection () Method obtained connection.
scrolling, updating data with resultset
1. When resultset, the default is to use Next () to move the data cursor to the next data, and then use the GetXXX () method to get the data.
2.ResultSet can use previous (), first (), last () and other methods to move the data cursor, you can also call Updatexxx (), UpdateRow () and other methods for data modification.
3. Result set Type 3 settings:
· Resultset.type_forward_only (default) (Forward data cursor only) · Resultset.type_scroll_insensitive (moving data cursor before and after) · Resultset.type_scroll_sensitive (move data cursor forward and backward to reflect data modifications in the database)
4. Update the set of 2 settings:
· Resultset.concur_read_only (default) (for data reading) · Resultset.concur_updatable (for data read, update)
5. API for Data cursor movement:
Absolute position Movement: absolute (), Afterlast (), Beforefirst (), first (), Last ().
Relative position movement: relative (), previous (), Next ().
Determine the current location: Isafterlast (), Isbeforefirst (), IsFirst (), Islast ().
6. Conditions for data modification:
You must select a single table.
You must select a primary key.
You must select all the values for not full.
7. Data update: Call the Updatexxx () method, and then call the Updaterow () method. Cancel Update: Call Cancelrowupdates ().
8. New data: Call Movetoinsertrow () First, then call Updatexxx () to set the data to be added to each field, and then call InsertRow () new data.
9. Delete data column: Call DeleteRow ().
Batch Update
1. Each execution of Excuteupdate () will send SQL once to the database. Batch updates can use the Addbatch () method to collect SQL and use the ExecuteBatch () method to deliver the collected SQL.
BLOBs and Clob
1. To write a document to the database, you can use the BLOB or CLOB data type on the database table field.
2.BLOB is used to store a large number of binary data, such as picture files, audio files and so on. CLOB is used to store large amounts of literal data.
3. java.sql.Blob with java.sql.Clob two classes represent BLOBs and CLOB data respectively.
Trading profile
1. Four basic requirements for transactions acid: atomicity, consistency, isolation behavior, persistence.
2. Update lost: A transaction updates the field of the message, due to the intervention of another transaction lost the effectiveness of the update. If you want to avoid such problems, you can set the isolation level to "readable unacknowledged", which can be set to transaction_uncommitted by Connection's settransactionisolation () to prompt the database to determine this isolation behavior.
3. Dirty read: Read to unclean, incorrect data. If you want to avoid such problems, you can set the isolation level to "readable acknowledgment", which can be set to transaction_committed by Connection's settransactionisolation () to prompt the database to determine this isolation behavior.
4. Unable to read repeatedly: A transaction two times the data in the same field is not consistent. If you want to avoid such problems, you can set the isolation level to "repeatable read", and you can use connection's settransactionisolation () setting to Transaction_repeatable_read to prompt the database to determine this isolation behavior.
5. Phantom read: The number of data strokes read during the same transaction is inconsistent. If you want to avoid such problems, you can set the isolation level to "sequential" and you can use connection's settransactionisolation () setting to Transaction_serializable to prompt the database to determine this isolation behavior.
Metadata Introduction
1.Metadata is the data of the read data.
2. The DatabaseMetaData object can be obtained through the connection Getmetad () method, which can obtain the whole information of the database through various methods provided by this object, and resultset represents the data that is queried, and the fields, types and other information of the data itself. You can obtain information such as domain names, field types, and so on, through the Resulset GetMetaData object, through the related methods provided by this object.
Rowset Introduction
The 1.javax.sql.rowset interface is a collection of classes that represent data, which can be spreadsheet data, XML data, or any data source that has the concept of a column collection. You can use rowset to make additions and deletions to the column collection.
2.JdcRowSet is an online rowset that is kept online with the database during operation and can be considered as the act of Acquisition and Operation Encapsulation.
3.CachedRowSet is an offline rowset that is disconnected from the data source when the data is queried and populated.
This week's code hosting
Learning progress Bar
|
lines of code (new | /Cumulative)
Blog volume ( | new/cumulative)
Learning time (new/cumulative) |
Important growth |
| Goal |
5000 rows |
30 Articles |
400 hours |
|
| First week |
150/150 |
1/2 |
15/15 |
|
| Second week |
350/500 |
1/3 |
20/35 |
|
| Third week |
400/900 |
1/4 |
35/70 |
|
| Week Four |
990/1890 |
1/5 |
40/110 |
|
| Week Five |
992/2882 |
1/6 |
30/140 |
|
| Week Six |
1486/4368 |
2/8 |
30/170 |
|
| Seventh Week |
408/4776 |
2/10 |
15/185 |
|
| Eighth Week |
246/5022 |
2/12 |
15/200 |
|
| Nineth Week |
438/5460 |
2/14 |
15/215 |
|
Resources
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
20145239 Java Programming 9th Week of study summary