Java Database Technology [1]

Source: Internet
Author: User

Preface

Databases, programmers will always face the development process. Its importance is self-evident. I learned a lot from VB, Delphi, and Java, and the database technology followed by ODBC, ADO, BDE, JDBC, JDO, and hibernate. Remember that in graphical programming, most of them are developed using controls, such as datasource, which is very convenient. Java is a class style, which is fully embodied in databases.
From today on, I want to learn these database technologies and take notes. Today we are introducing the most familiar JDBC

JDBC (Java database connectivity)

Is JDBC old in the face of object-oriented databases? Do we still need to learn about JDBC today? You may ask this question as a newcomer. First of all, no one can shake the qualifications and seniority of relational databases, even if it takes a few years to eat old Ben, and we need to use databases now. This is a fact. Secondly, JDBC is a good guy in the technology of accessing relational databases. Because it not only supports the wide variety of databases we often use (that is, it is not related to the supplier), but also supports cross-platform development (that is, it is not related to the platform ).
Most importantly, JDBC development is simple and common.
Different database vendors provide specific drivers for JDBC. Therefore, the first step to start JDBC is to develop the driver.

JDBC drever -- drivermanager

Load:
1. Use the-dproperty = value parameter to pass the command line option to the Java application.
Java-djdbc. Drivers = JDBC. ODBC. jdbcodbcdriver querydb

2. Use the static class method forname ()
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver");
Class. forname ("com. Imaginary. SQL. msql. msqldriver"); // msql

Registration:
Oracle driver example
Class. forname ("oracle. JDBC. Driver. oracledriver");
Java. SQL. drivermanager. registerdriver (New Oracle. JDBC. driveroracledriver ());

Open:
Connection drivermanager. getconnection (URL, userid, password );
Con. Close ();
Common URL format: JDBC: appropriate sub-Protocol: // driver-specific components (machine name, port, Database Name );
JDBC: appropriate sub-Protocol: @ driver-specific components (machine name, port, Database Name );

Get data:
String SQL = "select * From onetable";
Statement statement = con. createstatement ();
Resultset results=statement.exe cutequery (SQL );
Statement. colse ();

Obtain Dynamic SQL data ::
Stringsql = select name from idtable where id = ?";
Preparedstatement prepstatement = con. preparestatement (SQL );
Prepstatement. setstring (1, a_id );
Resultset rs1_prepstmt.exe cutequery ();

Rowset independently handles the problem:
String SQL = "select * From onetable";
Rowset rs = new cachedrowset (); // jdbcrowset needs to be permanently connected to the database, and cachedrowset does not need
Rs. setcommand (SQL );
Rs. seturl (URL );
Rs. setusername (userid );
Rs. setpassword (password );
Rs.exe cute ();
...
Rs. Close ();

Whether the result set resultset can be rolled:
Whether the jdbcs used by the databasemetadata object supports rolling resultset. One of its methods, supportsreseltsettype, is used to test:
Databasemetadata dbmd = connection. gtmetadata ();
Boolean okiedoie = md. supportsresultsettype (resultset. type_scroll_sensitive );
If (okiedoie ){
Statement statement = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
}
Type_scroll_sensitive indicates that the resultset can be rolled and is sensitive to database changes caused by other objects.
Concur_updatable indicates that the resultset can be updated in the result set.

JDBC metadata
Metadata is data about data.
The datebasemetadata object can provide information about the structure of a specific database.
Resultsetmetadata

JDBC Data Source
When using driver for development, the URL is written to death, causing a lot of inconvenience to maintenance. To solve this problem, jdbc2.0 uses the method of JNDI ing the actual data source-datasource using JNDI.

Before contacting the data source, let's first introduce the JNDI.

JNDIJava Naming and Directory Interface. Designed to standardize access to directories and naming services.
These services include: NIS/NIS +, LDAP (Lightweight Directory Access Protocol), RMI (Java Remote Method Invocation) reiregistry, and CORBA Common Object Service (COS) name Service NDS, DNS, Windows Registry, etc.

The naming service is a basic function in computer systems. The naming service associates a name with an object on the computer and allows you to conveniently find the corresponding object by name. For example, a file system in a computer contains a naming service. You can find the corresponding file object through the file name.
Directory Service is an extension of the Naming Service. directory service not only stores the matching between objects and object names, but also stores various attributes of this object, you can query, modify, add, or delete these attributes.

The JNDI structure includes two sets of Apis:
Jndi api (Application Programming Interface), Java applications access various named directory services through the jndi api.
Jndi spi (Service Provider Interface) and jndi spi allow various named directory services to be transparently added to the JNDI structure.

JNDI Context
Context is a collection of objects that are named and bound to objects. It provides parsing (that is, the search operation of returned objects ). Other operations include name binding and unbinding, and listing the name of the binding. Note that the name of a context object can be bound to another context object with the same naming conventions. This is called subcontext. Context provides the following interfaces to access the Naming Service:
BIND (name, object OBJ) establishes a matching relationship named after an object, also called binding
Lookup (string name) searches for the corresponding object by name
Rebind (name, object OBJ) to re-bind a name to the object, overwrite the original binding
Unbind (name) unbinds the matched object
......
To retrieve or parse objects from the Naming Service or directory by name, use the lookup method of context:
Object OBJ = contxt. Lookup (name ).

JDBC data source -- datasource

Configure the data source (Tomcat)
Server. xml:
<Context Path = docbase = DEBUG = reloadable =>
<Resource Name = JNDI name auth = container or application type = Class Name/> <! -- Container-container management application-Web Application Management -->
<Resoureparams name = JNDI Name>

<Parameter>
<Name> username </Name>
<Value> Eric Han </value>
</Parameter>
...
</Resoureparams>
</Context>

Web. xml:
<Resource-ref>
<Descrtiption> resource reference description </descrtiption>
<Res-ref-Name> JNDI name of the referenced resource </RES-ref-Name>
<Res-type> Class Name of the referenced resource </RES-type>
<Res-auth> administrator </RES-auth> <! -- Container-container management application-Web Application Management -->
</Resource-ref>

ACCESS DATA SOURCES
Context CTX = new initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("JDBC/one_mapping ");
Connection con = Ds. getconnection ();

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.