First, the concept of JDBC
1. JDBC (Java database Connectivity) Java DB connection
A) API support primarily for Java database applications
2, the main function of JDBC
A) Create and manage connections to data sources
b) Send SQL data command to data source
c) Extract the result set returned by the reason data source
3. JDBC can be divided into two layers
A) Driver management interface
b) JDBC API
4. Common basic JDBC API defined in java.sql package
A) class DriverManager: basic services for managing a set of JDBC drivers
b) Interface connection: Get a connection to the database
c) Interface statement: Used to execute a static SQL statement and return the resulting result set object
D) interface resultset: A data table that represents a database result set, typically a statement generation that executes a query database
E) class SqlException: Exception operations on databases
Ii. Basic application of JDBC
1. Basic steps for writing a JDBC application
A) Import the JDBC package or include the JDBC package
Configuration path, build path (Building path)
(b) Loading the JDBC driver
1//1. Load Driver 2 3 class.forname ("Oracle.jdbc.driver.OracleDriver"); 4 5 oracle.jdbc.driver.* for package name
(c) Establishing a connection to the database
1 Connection conn = drivermanager.getconnection (URL, user, password);
(d) Executing SQL statements, interacting with the database
1//3. New Statement Object 2 3 Statement st = Conn.createstatement (); 4 5//4. Execute SQL statement 6 7 String sql = "SELECT * FROM TEST "; 8 9 //Receive result set ResultSet rs = st.executequery (SQL);
(e) Closing the established connection
Various closures
2. Example
A. Loading drivers
A method for loading drivers is available in class classes
public static Class forname (String className)
Throws ClassNotFoundException
The descriptor for the Oracle driver class is: Oracle.jdbc.driver.OracleDriver
B. Establishing a connection to a database
DriverManager provides the Getconnection method to get the connection object (connecting conn) for the specified database:
public static Connectioin getconnection (string URL, string username, string pwd) throws SQLException
The URL format for the Oracle database is:
jdbc:oracle:thin:@< host name or IP>: port number:< database name >
C. Acquisition of Statement objects (Statement st)
Provides a way to get statement objects in the connection class
Statement getstatement () throws SQLException
Methods for executing SQL commands in the D.statement class
Boolean Execute (String SQL) throws SQLException
Result executeQuery (String SQL) throws SQLException
Int executeupdate (String SQL) throws SQLException
E.resultset can provide methods for working with result sets
Move result set Action pointer
Boolean next () throws SQLException
Get its value by field name
String getString (String columename) throws SQLException
Three: Advanced JDBC applications
The 1.PreparedStatement interface is a sub-interface of the statement, allowing the same SQL statement to be used multiple times with different parameters
The 2.Connection interface provides a way to create a PreparedStatement object that specifies the SQL statement
PreparedStatement PreparedStatement (String Sql) throws SQLException
1 Demo:2 3//question mark as placeholder 4 5 String Sql = "INSERT into tableName values (?,?)" 6 7 PreparedStatement pre = Conn.preparedstatement (SQL), 8 9 Pre.setint (1,99), pre.setstring (2, "ASD"); 12 Int count = Pre.executeupdate ();
3. Transaction processing
Transaction: is a complete unit of operation consisting of related SQL operations, which is used as a whole to either do it all or not
Four properties of a transaction:
Atomicity: Either to do it all or not to do it all
Consistency: Transactions make data from one consistent state large to another consistent state
Persistence: The operation of a transaction on a database is permanent
Isolation: The operation of one transaction does not affect the operation of other transactions i.e. the transaction is independent of the transaction
Transactional operations in 4.java
Conn.setautocommit (FALSE)//cancels the automatic commit of the transaction Conn.commit ();//Transaction commit Conn.rollback ();//Rollback of transaction
JDBC and Java database programming