`
thecloud
  • 浏览: 880488 次
文章分类
社区版块
存档分类
最新评论

JAVA JDBC Tutorial(1)

 
阅读更多

JDBC is a platform-independent interface between relational databases and Java. In today’s Java world, JDBC is a standard application programming interface(API) for accessing enterprise data in relational databases using Structured Query Language(SQL).

JDBC is a set of programming APIs that allows easy connection to a wide range of databases(especially relational databases) through Java programs.

The JDBC API is defined by two packages:

Java.sql provides the API for accessing and processing data stored in a data source(usually a relational database)using the Java programming language. This package provides the foundation and most commonly used objects(such as Connection, ResultSet, Statement, and PreparedStatement).

Javax.sql provides the API for server-side data source access and processing from the Java programming language. According to the JDK documentation, “this package supplements the java.sql package and, as of the version 1.4 release, is included in the JDK. It remains an essential part of [I2EE].”

More specifically, JDBC is a low-level , simple(has a well-defined API), and portable(since Java is portable across platforms) SQL call-level interface(CLI) written in Java.

JDBC’s detail architecture

The JDBC API does most of the things through the DriverManager class(java.sql.DriverManager). What is DriverManager? It is a connection factory class. In fact, DriverManager is the only class that can create database connections. (Each database connection is represented by an instance of a java.sql.Connection.) The DriverManager uses drivers to create connections. Each vendor(such as Oracle,MySQL, and Sybase) provides a set of drivers.

Java Application using JDBC components

Who provides these JDBC drivers? Usually, a database vendor(such as MySQL,Oracle,Sybase,and so on) writes a JDBC driver(a specific software for a specific database), which is a set of classes/interfaces that implements these interfaces(such as java.sql.Driver) for a particular database system. Following the JDBC architecture, a Java database application uses the DriverManager class to get the java.sql.Connection object, which represents a database connection. Then, using a Connection object, you can create Statement/PreparedStatement/CallableStatement, which can execute SQL queries and stored procedures and return results as ResultSert objects. (ResultSet is a table of data representing a database result set, which is usually generated by executing a statement that queries the database.)

The following are core JDBC classes, interfaces, and exceptions in the java.sql package:

DriverManager: This class loads JDBC drivers in memory. You can also use it to create java.sql.Connection objects to data sources.

Connection: This interface represents a connection with a data source. You can use the Connection object for creating Statement, PreparedStatement, and CallableStatement objects.

Statement: This interface represents a static SQL statement. You can use it to retrieve ResultSet objects.

PreparedStatement: This interface extends Statement and represents a precompiled SQL statement. You can use it to retrieve ResultSet objects.

CallableStatement: This interface represents a database stored procedure. You can use it to execute stored procedures in a database server.

ResultSet: This interface represents a database result set generated by using SQL’s SELECT statement.

SQLException: This class is an exception class that provides information on a database access error or other errors.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics