JDBC learning notes

xiaoxiao2021-03-06  98

Monday, Monday, 2004-9-13

l. Method of connection to the database: 1) ODBC (Open Database Connectivity) A C language access SQL-based interface interface, which provides a consistent interface for communication and access data. 2) JDBC Java version of ODBC

2. JDBC Application Programming Interface Answer: JDBC App Programming Interface is: 1) The standard data access interface can be connected to a different database; 2) A set of classes and interfaces of the Java programming language. The JDBC application programming interface can: 1) Connect to the database; 2) Send SQL query string to the database; 3) Processing results. The JDBC application programming interface has two main parts: 1) Java application development interface for Java application developers; 2) JDBC driver development interface 3. JDBC Driver A: 1) A lot of JDBC classes and interfaces Class; 2) Provide a class that implements a Java.SQL.Driver interface.

4. The four types of JDBC Driver Answer: 1) JDBC-ODBC Bridge Provides JDBC Access by ODBC Drive 2) Local API Section Java Driver puts JDBC calls to transform the local client API 3) JDBC-NET pure Java Driver, will JDBC calls to DBMS, not related to the network protocol. Then turn the call to the DBMS protocol through the server. 4) Local protocol pure Java Driver, transfer JDBC call directly to network protocols used by DBMS

5. JDBC Developer Interface Answer: 1) Java.sql - JDBC's main function, standard version (J2SE) 2) JAVAX.SQL - Java 2 platform, JDBC Enhancements, Enterprise Edition (J2EE)

6. Use the URL to confirm the database answer: We use the URL to determine a database (correct Driver, the correct host, the correct protocol, the correct protocol, the correct username and password); Syntax: Protocol: Subprotocol: Subname Example: JDBC: DB2: Mytest JDBC: DB2: // localhost: 6789 / mytest

7. Javax.SQL package JDBC2.0 Enhanced Answer: 1) Data Source Interface; 2) Connection Pool; 3) Distributed Transactions; 4) Lines;

8. Create a basic JDBC application answer: 1) Step 1: Register a driver; 2) Step 2: Create a connection to the database; 3) Step 3: Create a statement; 4) Step 4: Execute SQL statements; 5 Step 5: Processing results; 6) Step 6: Close JDBC object

9. Register a Driver (step 1) Answer: 1) Driver is used to connect to the database; 2) JDBC Application Programming Interface Use the first Driver; 3) can load multiple times at the same time Driver

10. Register a Driver method: A: 1) Use class Loader (loading; instantiation; registration into drivermanager) a. Class.forname ("com.ibm.db2.jdbc.app.db2driver"); b. Class. Forname ("com.ibm.db2.jdbc.net.db2driver"); c. Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver; d. class.forname (" oracl.jdbc.driver.OracleDriver " ); E. Class.Forname ("sun.jdbc.odbc.jdbcodbcdriver"); 2) instantiate a driver a. Driver drv = new com.cloudscape.core.rmijdbcdriver (); 2004-9-14 Tuesday

1. Create a connection to the database (Step 2) Answer: DriverManager Call the getConnection (UrlString) method, actually calling Driver's Connect (urlstring) method; 1) When a driver affirms to a database URL, DriverManager is established, DRIVERMANAGER A connection; 2) When there is no Driver match, return null and then the next Driver is checked; 3) If there is no connection, a SQLExcePiton is throwing a SQLExcepiton

2. Some JDBC URLs often use: 1) JDBC-ODBC: JDBC: ODBC: 2) Oracle: JDBC: Oracle: OCI: @ OR JDBC: Oracle: Thin: @ 3) WebLogic MS-SQL: JDBC: WebLogic: MSSQLServer4: @ : 4) DB2: JDBC: DB2: MyTest or jdbc.db2: // localhost: 6789 / mytest (required username and password)

3. Driver connection method A: 1) Create a direct call to the specified driver instance; 2) Avoid general access issues Driver DRV = new com.ibm.db2.jdbc.app.db2driver (); connection con = null; try {Con = DRV.CONNECT ("JDBC: DB2: MyTest", New Properties ())} catch (sqlexception e) {}

4. Create a Statement (Step 3) Answer: 1) Three interfaces of Statement: a. Statement; b. PreparedStatement; c. Callablestatement; 2) Use method conspection.createstatement () Get a Statement object

5. PreparedStatement Object A: 1) more efficient than ProparedStatement call statement; 2) inherited from Statement; 3) Syntax: PreparedStatement pstm = connection.prepareStatement (sqlString); 6 CallableStatement objects A: 1) a database by calling CallableStatement Storage procedure; 2) Inherited from preparedStatement; 3) callablestatement cstm = connection.prepareCall ("{call return_student [?,?]}"); Cstm.setstring (1, "8623034"); cstm.registeroutparameter (2, Types. Cstm.execute (); float gpa = cstm.getfloat (2);

7. STATEMENT interface comparative answer: | statement | preparedState | Callablestatement ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------ Write code location | client | End | server ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- Write code location | Client | Server-Direct | Server ------ -------------------------------------------------- ---------------------- Write code technology | Java, SQL Operation | Java, SQL Operation | Program language, such as PL / SQL ----- -------------------------------------------------- ----------------------- Configurability | High | First high, low | low ------------ -------------------------------------------------- ---------------- Portable | High | Suppose supports preparedstatement words ----------------------- -------------------------------------------------- ----- Transmission efficiency | Low | First low, later high | High

8. Execute SQL Statement (Step 4) A: Transfer the SQL statement to a recognized database connection through the interface method, and the result may be a data table, you can access by java.sql.ResultSet. 1) STATEMENT interface method: a. EXECUTEQUERY (SQLSTRING): Perform a given SQL declaration, return a result set (RESUTETEUPDATE (SQLString): Perform a given SQL declaration, can be insert, update or Delete declaration, or SQL DDL declaration; c. Execute (sqlstring): Perform a given SQL declaration. 9. Processing results (Step 5) Answer: 1) Access data for the access method of the result set (ResultSet); a. Next (): Next Record b. First (): The first record C. Last (): The last record d. Previous (): Previous record 2) The resulting data 3) The result set keeps a pointer to the current row, the initialization position is before the first record.

10. Turn off the JDBC object (step 6) Answer: 1) First turn the record set; 2) Secondly close the declaration; 3) Finally close the connection object.

11. The three relationships corresponding to the data sheets and classes: A: 1) A table corresponds to a class; 2) a table corresponds to the related class; 3) a table corresponding to the entire relationship

12. Several table design of inter-class relations: A: 1) Multi-to-one, 2) One-to-one: 3) One-to-one: 4) Many:

13. SQL data type and its corresponding Java data type A: SQL data type Java data type description ----------------------------- ----------------------------------- Integer or int int in is usually a 32-bit integer Smallint Short is usually a 16-bit integer Number (m, n) decimal (m, n) java.sql.Numeric total number of points is the number of fixed points of M. N-digit DEC (M, N) java.sql.Numeric composite number after decimal Is the fixed point decimal number of M, the decimal number Float (n) Double Computation Accuracy is N-bit binary floating point REAL FLOAT usually 32-bit floating point Double Double Double is usually 64-bit floating point character (n) or char (n) String Length of String Length Strings VARCHAR (N) String Maximum length of the Maximum length of N of the variable length string Boolean Boolean Boolean Date java.sql.date implemented the calendar date according to the specific device Time java.sq L.Time The timestamp TimeStamp java.sql.TimeStamp implemented based on the specific device The day date and time implemented on the specific device BLOB java.sql.blob binary large object clob java.sql.clob character large object Array Java.sql. Array2004-9-14 Wednesday

1. Data Answer: Information about data, such as types or capacity. You can access: 1) Database metadata; a. Returns DataMetAdata reference B. You can use IsReadOnly such method to get information 2) Results Data; a. Use the resultset.getMetadata method to return to ResultSetMetadata Reference B . Can use getColumnCount to get information

2. Transaction Answer: 1) A series of actions as a unsatuable operation; 2) Use transaction processing steps in the JDBC API: a. Use false as the parameter to call the setAutocommit method; b. Perform one or more of the database. c. Call the commit method to complete the change; d. Restore the last submission change, call the Rollback method .try {con.setautocommit (False); statement stm = con.createstatement (); stm.executeUpdate ("Insert Into Student (Name , AGE, GPA) VALUES ('Gzhu', 30, 4.8) "); stm.commit ();} catch (sqlexception e) {Try {con. rollback ();} catch (exception e) {}}}

3. Concurrency control Answer: 1) Set the isolation level method: setTransactioniSolation 2) Isolation level static variables a. Transaction_none: Read-only data dictionary; b. Transaction_read_uncommitted: Read only unloaded data; c. Transaction_read_committed: Read only unloaded data; d. Transaction_repeatable_read: Read the data repeatedly; E. Transaction_Serializable: No matter what the operation is not allowed to move. 3) Example: conount.transactioniolation (connections.transaction_read_uncommitted);

4. JDBC 2.0 Application Programming Interface Enhanced Security Answer A: 1) ResultSet Enhancement: a. You can return; b. You can modify; set example: Statement Stm = con.createstatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_updatable); 2) statement Enhance Batch Updates; 3) Advanced Data Types (Example: Struct).

5. JDBC 2.0 Standard Extended A: 1) JAVA Naming and Directory Interface: Solve the lookup of Object under discrete state; 2) Connecting pool: Save a database connection in memory, do not need to register your drive, improve performance method.

转载请注明原文地址:https://www.9cbs.com/read-96262.html

New Post(0)