JDBC  «Prev  Next »

Lesson 4 What is SQL?
Objective Describe the role of SQL and its relation to JDBC

How can SQL be used with Java Servlets?

Explain the SQL commands needed for this course.SQL, Structured Query Language, is a way of sending commands to a database. Not all database programs understand SQL.
You send SQL commands to JDBC and any translation that is needed will be done there for you.
SQL statements start with a verb.
The most important are SELECT, UPDATE, and INSERT.
The SELECT verb gets you information from a database.
For example, if your database has a table called PEOPLE that holds names, phone numbers, and ages of individuals, you might use this line of SQL:

SELECT NAME, AGE FROM PEOPLE

Result Set

This would give back a list of results, and each result would be a name and an age from the PEOPLE table.
To get all the fields, or columns, rather than just name and age, you could list them all, or you could use the shortcut, *, which means all fields like this:

SELECT * FROM PEOPLE

This would dump out all the information in the table. Usually, you do not want every column and you certainly do not want every record.
To retrieve only the 25-year olds, you would use this SQL:
SELECT * FROM PEOPLE WHERE AGE=25

This would give you all three columns for each 25-year old.
Since you already know the age, perhaps you do not want to repeat it on every line of output. In that case, you could do this:

SELECT NAME, PHONE FROM PEOPLE WHERE AGE=25

The exact field names you use in your SQL depend on the design of your database.
You will see the syntax of UPDATE statements, which change values already in the database, in later lessons.
INSERT statements, which are similar, add records to the database. DELETE statements remove them.
Create a data source in the next lesson.

Database SQL - Quiz

The following quiz poses questions with respect to databases and SQL.
Database SQL - Quiz