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
- Import java.sql.*;
- Try {
- class . forname ("Com.pointbase.jdbc.jdbcUniversalDriver");
- Connection con=drivermanager.getconnect ("jdbc:pointbase;//server:port/mydatabase");
- Statement stmt=con.createstatement ();
- String sql="select * FROM MyTable";
- ResultSet res=stmt.executequery (SQL);
- while (Res.next ()) {
- String col1=res.getstring ("MYCOLUMN1");
- int col2=res.getint ("MYCOLUMN2");
- }catch(Exception e) {...}
- }
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
- Import java.sql.*; Import javax.sql.*; Import javax.naming.*, .....
- Try {
- Context ctx=new InitialContext ();
- DataSource ds= (DataSource) ctx.lookup ("Testdatasource");
- Connection con=ds.getconnection ();
- Statement stmt=con.createstatement ();
- String sql="select * from MYTABLE";
- ResultSet res=stmt.executequery (SQL);
- while (Res.next ()) {
- String col1=res.getstring ("MYCOLUM1");
- ...
- }
- Con.close ();
- }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
- Statement stmt=con.createstatement ();
- String sql="select * from MYTABLE";
- ResultSet res=stmt.excutequery (SQL);
Example of querying a database using the PreparedStatement object:
[Java]View Plaincopy
- String sql= "SELECT * from MYTABLE WHERE col1=?" and col2=? " ;
- PreparedStatement pstatement=con.preparestatement (SQL);
- Pstatement.setstring (1, searchvariablestring);
- Pstatement.setint (2, searchvariableint);
- ResultSet Rs=pstatement.executequery ();
It's just a preliminary understanding, and it needs to be applied to the project.