JDBC and Java database programming

Source: Internet
Author: User

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 statements 6 7 String sql = "SELECT * from TEST"; 8 9  // Receive result set Ten  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:    3  //    4   String Sql =" INSERT into tableName values (?,? ) "   6   7  PreparedStatement pre = Conn.preparedstatement (SQL);    9  pre.setint (1,99); 10   12  13  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 (); // Commit a transaction   conn.rollback (); // Rollback of a transaction

&NBSP;

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.