您的位置:首页 >> 编程开发 >> Java >> J2EE >> 正文
J2EE RSS
 

Accessing Databases from Servlets and JSP Pages

http://www.rdxx.com 05年08月10日 20:06 Java频道 我要投稿

关键词: Servlets , JSP , SSI , Databases , CES , Tab , ASE , Servlet

Accessing data in a database or in other data sources is an important task in web programming. This article shows you how to do the most common database manipulations from servlets and JSP pages. As with other types of Java applications, data access from JSPs and servlets is done through Java Database Connectivity (JDBC). We will begin with an introduction to JDBC followed by some examples of servlets that allows you to access the data in the database. This article assumes that you have some basic knowledge of servlets and JSP and that you are familiar with Structured Query Language (SQL).

Introduction to JDBC

JDBC is an API that allows you to store, retrieve, and manipulate data in virtually any data source, and to manipulate the data structures. Although the JDBC 4.0 Specification proposal has been drafted, we will cover the current version, JDBC 3.0, which is included in J2SE 1.4. This section explains the JDBC object model and the most important classes and interfaces, to enable you to develop database-based servlet and JSP applications. Here are the steps required to access data in a database:

  1. Load the JDBC database driver.
  2. Create a connection.
  3. Create a statement.
  4. Create a resultset, if you expect the database server to send back some data.

There are two packages in JDBC 3.0: java.sql and javax.sql. The java.sql package is often referred to as the JDBC core API and is sufficient to do basic data manipulations. The javax.sql package is the JDBC Optional Package API. It provides additional features, including connection pooling, which I'll discuss in another article. The following subsections will discuss the four most important members of the java.sql package: the DriverManager class and the Connection, the Statement, and the ResultSet interfaces.

The DriverManager Class

The DriverManager class is used to obtain a connection to a database. Database servers use their own proprietary protocols for communication, which are different from each other. However, you don't need to worry about these protocols because you can use "translators." These "translators" come in the form of JDBC drivers. Therefore, you only need to write your Java code once to access any database. JDBC drivers are available for most popular databases today. Oracle, Microsoft SQL Server, Sybase, DB2, MySQL, and so on. As long as you can find the JDBC driver for a database server, you can access the database from your Java code. The list of JDBC drivers can be found here.

JDBC drivers normally come in a .jar file. The first thing you need to do once you have the driver is copy it into the WEB-INF\lib directory under your application directory.

Then, from your servlet/JSP page, you use the DriverManager class to load JDBC drivers. The JDBC driver must be located in the WEB-INF/lib directory under the application directory. To load the JDBC driver, you use this code:

try {  Class.forName("JDBC.driver");}catch (ClassNotFoundException e) {  // driver not found}

上一页 下一页


 
 
标签: Servlets , JSP , SSI , Databases , CES , Tab , ASE , Servlet 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站