بِسۡمِ ٱللهِ ٱلرَّحۡمَـٰنِ ٱلرَّحِيمِ
You may have recognized a similarity between the tables of a relational database and simple JavaBean components. When building your applications think of tables as being analogous to JavaBeans. While JavaBeans have properties, data from a table has columns. A table’s schema is like the class that defines a JavaBean — defining the names and types data that instances will hold. Like Java classes, tables are templates for storing a specific set of information like the data from a purchase order or details about inventory items and by themselves are not particularly useful.
It is only when we create instances of a JavaBean class or add rows to a table that we have something worthwhile. Each row is an instance of what the table represents, just as a bean is an instance of its class. Both classes and tables then serve as data models, a useful container for managing information about some real world object or event. Keep this relationship in mind as we learn about JSP database development. It will form the basis for many of our applications.
One of the most common areas for utilizing databases with JSP applications is to retrieve data stored in a table to create a bean for use within the page. The configuration of JSP components from information in the database is pretty straightforward if your table schema (or the results of a join between tables) closely corresponds to your bean’s properties. We simply use the row access methods of the ResultSet class to configure the bean’s properties with the values in the table’s corresponding columns. If there is more than a single row in the result set we must create a collection of beans, one for each row of the results.
You can use JSP scriptlets to configure a bean’s properties when it is created. After establishing the connection, set its properties as appropriate through the data carried in the ResultSet. Don’t forget to import the java.sql package into the page with the <%@ page import=”java.sql.*” %> directive.



0 comments:
Post a Comment