2016-11-03
JDBC (Java Database connection) is an API for executing SQL statements, which is composed of classes and interfaces written in the Java language, which provides a standard application interface that allows developers to pass SQL statements to any kind of database. There is no need to write separate accessors for various databases
1) Establish a connection to a database.
Connection con = drivermanager.getconnection ("Jdbc:odbc:CallCenter", "sa", "");
(2) Send SQL statements to the database.
stmt = Con.createstatement ();
rs = Stmt.executequery ("Select Cid,cpin from Tcustomer WHERE cid= ' Z1 '");
(3) processing the results returned by the database.
while (Rs.next ())
{
String Theint = rs.getstring ("CID");
String str = rs.getstring ("CpIn");
...
}
Using JDBC, one is the JDBC API, which is for programmers, and one is the JDBC Driver API, which is oriented toward the underlying drive.
Java.sql.DriverManager: Handles driver onboarding and provides support for generating new database connections.
Java.sql.Connection: Represents a connection to a specific database.
Java.sql.Statement: Represents a specific container to execute SQL statements against a specific database.
Java.sql.ResultSet: Controls the access to row data for a particular statement.
One of the java.sql.Statement has two sub-types:
(1) Java.sql.PreparedStatement: The SQL statement used to perform precompilation.
(2) Java.sql.CallableStatement: Used to perform a call to a database inline procedure.
The JDBC Driver API refers to the Java.sql.Driver interface, which encapsulates drivers for different databases (such as access, Foxpro, SQL Server, and so on). Because it is the database underlying processing, it must be provided to Java.sql.Connection, java.sql. Implementation of Statement, Java.sql.PreparedStatement and Java.sql.ResultSet.
If the target DBMS provides an inline procedure with an out parameter, the Java.sql.CallableStatement interface must also be provided. In the Java.sql.Driver interface, each database driver must provide a class that allows the system to be managed by Java.sql.DriverManager. A more useful driver is to provide an implementation of JDBC on top of ODBC, providing the JDBC-ODBC bridge to the ODBC interface. The so-called Jdbc-odbc Bridge is a JDBC driver that implements JDBC operations by converting JDBC operations to ODBC operations. It is implemented by the SUN.JDBC.ODBC package, which contains a local library for accessing ODBC, which implements JDBC for all ODBC-available databases.
With the ODBC Sub-Protocol, the following line of code can be used for display loading.
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
At load time, the ODBC driver will create its own instance and register it in the JDBC Driver Manager. Because JDBC is placed behind ODBC, it is simple and efficient to implement.
Java Database Technology