JDBC Learning Note _php Tutorial

Source: Internet
Author: User
L. How to connect to a database
Answer: 1) ODBC (Open Database Connectivity)
A C language-based interface for accessing SQL as the underlying database engine, which provides a consistent interface for communicating with and accessing data from the database.
2) JDBC
Java version of ODBC

2. JDBC Application Programming Interface
Answer: The JDBC application programming interface is:
1) Standard data access interface, can be connected to different databases;
2) A set of classes and interfaces for the Java programming language.
The JDBC application programming interface can:
1) connect to the database;
2) Send SQL query string to 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 large bunch of classes that implement JDBC classes and interfaces;
2) provides a class that implements the Java.sql.Driver interface.

4. Four types of JDBC driver
Answer: 1) Jdbc-odbc Bridge
JDBC access provided by ODBC driver
2) Local API
Some Java driver translate JDBC calls into local client APIs
3) jdbc-net
Pure Java driver, which transfers JDBC calls to the DBMS, regardless of the network protocol. The call is then converted to the DBMS protocol via the server.
4) Local Protocol
Pure Java driver, direct the JDBC call to the network protocol used by the DBMS

5. JDBC Developer Interface
A: 1) The main function of JDBC under the Java.sql--java 2 Platform, Standard Edition (J2SE)
2) Javax.sql--java 2 platform JDBC Enhancements, Enterprise Edition (Java EE)

6. Use the URL to confirm the database
A: We use the URL to determine a database (the correct driver, the correct host, the correct protocol, the correct protocol, the correct user name and password);
Syntax: Protocol:subprotocol:subname
Example: Jdbc:db2:MyTest
Jdbc:db2://localhost:6789/mytest

7. javax.sql Package JDBC2.0 Enhancements
Answer: 1) Data source interface;
2) connection pool;
3) distributed transactions;
4) row set;

8. Create a basic JDBC application
Answer: 1) Step one: Register a driver;
2) Step Two: Establish a connection to the database;
3) Step three: Create a statement;
4) Step four: Execute the SQL statement;
5) Step Five: Process the results;
6) Step six: Close the JDBC object

9. Register a driver (step one)
Answer: 1) driver is used to connect to the database;
2) The JDBC application programming interface uses the first driver that can successfully connect to a given URL;
3) Multiple driver can be loaded at the same time

10. To register a driver method:
A: 1) Use class loader (load; instantiate; register 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.sqlServer.SQLServerDriver");
D. Class.forName ("Oracl.jdbc.driver.OracleDriver");
E. Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
2) instantiation of a driver
A. Driver DRV = new COM.cloudscape.core.RmiJdbcDriver ();

1. Establish a connection to the database (step two)
A: DriverManager calls the getconnection (urlstring) method, actually calls the Connect (urlstring) method of driver;
1) When a driver corresponds positively to a database Url,drivermanager establishes a connection;
2) When there is no driver match, returns null then the next driver is tested;
3) If no connection is established, throw a Sqlexcepiton exception

2. Some JDBC URLs that are used frequently
Answer: 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 (user name and password required)

3. Driver Connection method
Answer: 1) Create a direct call to the specified driver instance;
2) Avoid problems with general access
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 three)
A: 1) the statement three interfaces:
A. Statement;
B. PreparedStatement (inherited from statement);
C. CallableStatement (inherited from PreparedStatement);
2) Use Method Connection.createstatement () to get a statement object

5. PreparedStatement objects
Answer: 1) Call proparedstatement more efficient than statement;
2) inherit from statement;
3) Syntax: PreparedStatement pstm = connection.preparestatement (sqlString);

6. CallableStatement Objects
A: 1) call the stored procedure in the database through CallableStatement;
2) inherit from PreparedStatement;
3) CallableStatement cstm = Connection.preparecall ("{Call return_student[?,?]}");
Cstm.setstring (1, "8623034");
Cstm.registeroutparameter (2, types.real);
Cstm.execute ();
float GPA = cstm.getfloat (2);

7. Comparison of statement interfaces
Answer: | Statement | PreparedStatement | CallableStatement
------------------------------------------------------------------------------
Write Code Location | Client | Client | Server-side
------------------------------------------------------------------------------
Write Code Location | Client | Server-side | Server-side
------------------------------------------------------------------------------
Writing Code Technology | Java,sql Operations | Java,sql Operations | The program language of the database, such as PL/SQL
------------------------------------------------------------------------------
Configurable | High | First high, later low | Low
------------------------------------------------------------------------------
Portability | High | Assuming support preparedstatement words high
------------------------------------------------------------------------------
Transmission Efficiency | Low | For the first time low, later high | High

8. Execute SQL Statement (step four)
A: The SQL statement is transmitted to the MO database connection by means of an interface method, and the returned result may be a data table that can be accessed through Java.sql.ResultSet.
1) Statement interface method:
A. ExecuteQuery (sqlString): Executes the given SQL declaration, returning a result set (ResultSet) object;
B. Executeupdate (sqlString): Executes the given SQL declaration, which can be an insert, update, or delete declaration, or a SQL DDL declaration;
C. Execute (sqlString): Executes the given SQL declaration.

9. Processing results (step five)
A: 1) Use the access method of the result set (ResultSet) object to obtain the data;
A. Next (): Next record
B. First (): one record
C. Last (): Final record
D. Previous (): Previous record
2) Get data by field name or index
3) The result set maintains a pointer to the current row, and the initialization position is before the first record.

10. Close the JDBC object (step six)
Answer: 1) first close the recordset;
2) Second closing statement;
3) Finally close the connection object.

11. Three relationships for data tables and classes:
A: 1) a table corresponding to a class;
2) a table corresponding to related classes;

http://www.bkjia.com/PHPjc/508517.html www.bkjia.com true http://www.bkjia.com/PHPjc/508517.html techarticle L. How to connect to a database answer: 1) The ODBC (Open database Connectivity) is a C language-based interface for accessing SQL as the underlying database engine, and it provides a consistent interface for the numeric ...

  • 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.