Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Java JSP Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Java JSP Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Java JSP Application Hosting Web Hosting Sign-Up Java JSP Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Java JSP Application Hosting Resellers web hosting, website hosting, web site hosting Java JSP Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Java JSP Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Java JSP Application Hosting Welcome Java JSP Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Java JSP Application Hosting Fund Raising, Fundraising, web hosting Java JSP Application Hosting Resellers, web Hosting Java JSP Application Hosting Web Design, web Hosting Java JSP Application Hosting Extra Services,  web Hosting Java JSP Application Hosting Traffic Booster, web hosting Java JSP Application Hosting Traffic Booster, web hosting Java JSP Application Hosting Technical Support,  web Hosting Java JSP Application Hosting webmaster tips,  web Hosting Java JSP Application Hosting 30 Day Money Back, web hosting Java JSP Application Hosting Legal Notices for Web Hosting Java JSP Application Hosting Glossary Computer Terms for web Hosting Java JSP Application Hosting Contact Information - web hosting

Site Map
Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Using Joins (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)
Trail: JDBC(TM) Database Access
Lesson: JDBC Basics
Home Page > JDBC(TM) Database Access > JDBC Basics
Using Joins
Sometimes you need to use two or more tables to get the data you want. For example, suppose the proprietor of The Coffee Break wants a list of the coffees he buys from Acme, Inc. This involves information in the COFFEES table as well as the yet-to-be-created SUPPLIERS table. This is a case where a join is needed. A join is a database operation that relates two or more tables by means of values that they share in common. In our example database, the tables COFFEES and SUPPLIERS both have the column SUP_ID, which can be used to join them.

Before we go any further, we need to create the table SUPPLIERS and populate it with values.

The code below creates the table SUPPLIERS:

String createSUPPLIERS = "create table SUPPLIERS " +  
			 "(SUP_ID INTEGER, SUP_NAME VARCHAR(40), " + 
			 "STREET VARCHAR(40), CITY VARCHAR(20), " +
			 "STATE CHAR(2), ZIP CHAR(5))";
stmt.executeUpdate(createSUPPLIERS);

The following code inserts rows for three suppliers into SUPPLIERS:

stmt.executeUpdate("insert into SUPPLIERS values (101, " +
    "'Acme, Inc.', '99 Market Street', 'Groundsville', " + "'CA', '95199'");
stmt.executeUpdate("Insert into SUPPLIERS values (49," +
    "'Superior Coffee', '1 Party Place', 'Mendocino', 'CA', " + "'95460'");
stmt.executeUpdate("Insert into SUPPLIERS values (150, " + 
    "'The High Ground', '100 Coffee Lane', 'Meadows', 'CA', " + "'93966'");

The following code selects the whole table and lets us see what the table SUPPLIERS looks like:

ResultSet rs = stmt.executeQuery("select * from SUPPLIERS");

The result set will look similar to this:

SUP_ID SUP_NAME

STREET

CITY

STATE

ZIP

------

-------------

-------------

-----------

-----

-----

101

Acme, Inc.

99 Market Street

Groundsville

CA

95199

49

Superior Coffee

1 Party Place

Mendocino

CA

95460

150

The High Ground

100 Coffee Lane

Meadows

CA

93966

Now that we have the tables COFFEES and SUPPLIERS, we can proceed with the scenario where the owner wants to get a list of the coffees he buys from a particular supplier. The names of the suppliers are in the table SUPPLIERS, and the names of the coffees are in the table COFFEES. Since both tables have the column SUP_ID, this column can be used in a join. It follows that you need some way to distinguish which SUP_ID column you are referring to. This is done by preceding the column name with the table name, as in "COFFEES.SUP_ID" to indicate that you mean the column SUP_ID in the table COFFEES. The following code, in which stmt is a Statement object, selects the coffees bought from Acme, Inc.:

String query = "
SELECT COFFEES.COF_NAME " +
   "FROM COFFEES, SUPPLIERS " +
   "WHERE SUPPLIERS.SUP_NAME LIKE 'Acme, Inc.' " +
   "and SUPPLIERS.SUP_ID = COFFEES.SUP_ID";

ResultSet rs = stmt.executeQuery(query);
System.out.println("Coffees bought from Acme, Inc.: ");
while (rs.next()) {
    String coffeeName = rs.getString("COF_NAME");
    System.out.println("     " + coffeeName);
}

This will produce the following output:

Coffees bought from Acme, Inc.:
     Colombian
     Colombian_Decaf
Previous page: Using Prepared Statements
Next page: Using Transactions
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting