JDBC Basic usage

Source: Internet
Author: User

Java EE Technical Specification (ii)--JDBCCategory: Java2012-12-03 14:251060 People readComments (8)CollectionReport

First, understanding JDBC

(1) JDBC is an API to access the database in a unified manner

(2) JDBC provides:

Platform-Independent Database access

Position Transparent

A problem with a proprietary database is transparent

Ii. Understanding the JDBC Driver

(1) JDBC drivers is a class that implements database operations

(2) Drivers are divided into two categories:

2-tier-customer direct and database sessions

3-tier--client and middle tier (WebLogic Server) session representing the database

In fact, the two-tier architecture directly deals with the database

Three-tier architecture loads the middle tier of the database

These are almost no different from. Net. Let's continue to look at a diagram:

What do we want to see in this picture?

The 4th type: Java features. There is no need to install software and clients, and it is not related to the location and platform. ,

1th, 2, 4: are two-tier types, directly connected to the database

The 3rd kind: based on the networkserver centralized monitoring, the shield loses the different platform. On the basis of 1, 2, 4, a resource connected by webserver. is a special type of JDBC

Third, direct access to the database using JDBC

(1) Direct access to the database consists of the following:

Loading the JDBC driver class

Let's take a look at an example of a direct database connection:

(2) using the basic JDBC command

[Java]View Plaincopy
  1. Import  java.sql.*;
  2. Try {  
  3. class  . forname ("Com.pointbase.jdbc.jdbcUniversalDriver");
  4. Connection con=drivermanager.getconnect ("jdbc:pointbase;//server:port/mydatabase");
  5. Statement stmt=con.createstatement ();
  6. String sql="select * FROM MyTable";
  7. ResultSet res=stmt.executequery (SQL);
  8. while (Res.next ()) {
  9. String col1=res.getstring ("MYCOLUMN1");
  10. int      col2=res.getint ("MYCOLUMN2");
  11. }catch(Exception e) {...}
  12. }

Iv. use of Multilayer (TYPE3) Drivers

To use a multilayer architecture, you must configure at least one connection pool (Connection pool and one data source (DataSource)

Five, Connection pool connection

Connection pool:

Eliminate the load required to establish frequent connections

Is the management object used to manage database connections

Provides a shareable, secure connection

Vi. Data Source Sources

Data sources is:

Managed factory objects that provide connections from the connection pool

Binding to Jndi and using the management console configuration

Vii. we access the database through data source sources

(1) Using DataSource:

Find it in Jndi first

Then get a connection from DataSource

Example of using a data source to connect to a database:

[Java]View Plaincopy
  1. Import java.sql.*; Import javax.sql.*; Import javax.naming.*, .....
  2. Try {  
  3. Context ctx=new InitialContext ();
  4. DataSource ds= (DataSource) ctx.lookup ("Testdatasource");
  5. Connection con=ds.getconnection ();
  6. Statement stmt=con.createstatement ();
  7. String sql="select * from MYTABLE";
  8. ResultSet res=stmt.executequery (SQL);
  9. while (Res.next ()) {
  10. String col1=res.getstring ("MYCOLUM1");
  11. ...
  12. }
  13. Con.close ();
  14. }catch(Exception e) {...}

VIII. statements and prepared statements

(1) The statement object contains SQL queries or UPDATE statements that interact with the database

Example of querying a database using the statement object:

[Java]View Plaincopy
    1. Statement stmt=con.createstatement ();
    2. String sql="select * from MYTABLE";
    3. ResultSet res=stmt.excutequery (SQL);

Example of querying a database using the PreparedStatement object:

[Java]View Plaincopy
    1. String sql= "SELECT * from MYTABLE WHERE col1=?" and col2=? "  ;
    2. PreparedStatement pstatement=con.preparestatement (SQL);
    3. Pstatement.setstring (1, searchvariablestring);
    4. Pstatement.setint (2, searchvariableint);
    5. ResultSet Rs=pstatement.executequery ();

It's just a preliminary understanding, and it needs to be applied to the project.

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.