Name | Gender | Age |
---|---|---|
Mary | F | 23 |
Robert | M | 31 |
Sally | F | 35 |
Jane | F | 28 |
Tony | M | 30 |
Alice | F | 41 |
Scott | M | 27 |
Larry | M | 49 |
Each row of the table can represent a variety of data objects, for example, items stocked in a store, airline flights, or students in a course. The columns of a table are the individual data variables of the persons. In the case of the PersonData table, the columns are Name, Gender, and Age.
Usually a relational database contains more than one table. The tables in a database contain related information that can be cross referenced.
 
 
Name | Gender | Age |
---|---|---|
Mary | F | 23 |
Robert | M | 31 |
Sally | F | 35 |
Jane | F | 28 |
Tony | M | 30 |
Alice | F | 41 |
Scott | M | 27 |
Larry | M | 49 |
Name |
---|
Mary |
Robert |
Sally |
Jane |
Tony |
Alice |
Scott |
Larry |
Name | Age |
---|---|
Mary | 23 |
Robert | 31 |
Sally | 35 |
Jane | 28 |
Tony | 30 |
Alice | 41 |
Scott | 27 |
Larry | 49 |
Name | Gender | Age |
---|---|---|
Alice | F | 41 |
Name |
---|
Mary |
Sally |
Jane |
Alice |
Name | Gender | Age |
---|
SQL = "SELECT * FROM PersonData WHERE Name = '" + name + "'"
Note that text constants in SQL must be enclosed in single quotes, even when the text constant is obtained from a variable at run time.
 
Name | Gender | Age |
---|---|---|
Beginning of File Buffer Row | ||
Robert | M | 31 |
Tony | M | 30 |
Scott | M | 27 |
Larry | M | 49 |
End of File Buffer Row |
BOF is the abbreviation for the Beginning of File Buffer; EOF is the abbreviation for the End of File Buffer.
while (r.next())
{
    // body of while loop
}
The first r.next() call moves the
cursor from BOF to the first valid row. The while loop continues
until the cursor reaches EOF where
r.next() returns false
so the while loop terminates.
Here are ResultSet methods for obtaining field
values from a row. They return the value of the field in the
requested format.
int getDouble(int columnNumber)
int getInt(int columnNumber)
int getString(int columnNumber)
Other datatypes like short, long, and float are also available.
// Sample Java Application that reads from
import java.sql.*;
public class Main
      
// Create Connection object.
      
// Create Statement object.
      
// Create ResultSet object.
      
// Traverse the result set to print data.
      
// Close the Connection object c.
// the PersonData table of the Person database.
{
   
public static void main(String[] a)
      
throws ClassNotFoundException, SQLException
   
{
      
// Load database driver.
      
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      
Connection c = DriverManager.getConnection(
         
"jdbc:odbc:Person");
      
Statement s = c.createStatement();
      
String query = "SELECT * FROM PersonData";
      
ResultSet r = s.executeQuery(query);
      
String name;
      
char gender;
      
int age;
      
while (r.next())
      
{
         
name = r.getString(1);
         
gender = r.getString(2)getChar(0);
         
age = r.getInt(3);
         
System.out.println(
            
name + " " + gender + " " + age);
      
}
      
c.close();
   
}
}
Mary F 23
Robert M 31
Sally F 35
Jane F 28
Tony M 30
Alice F 41
Scott M 27
Larry M 49
To create a new table PersonData with fields like the PersonData
table use this
SQL statement:
CREATE TABLE PersonData2 (Name Text, String Text, Age Number)
INSERT INTO PersonData2 VALUES ("Nancy", "F", 19);
// Sample Java Application to create a new
import java.sql.*;
public class Main
       // Create Connection object.
       // Create Statement object.
      
// Create PersonData2 table.
       // Insert a row into the table.
       // Close the Connection object c.
   
}
// table in an Access database.
{
    public static void main(String[] a)
      
throws ClassNotFoundException, SQLException
    {
       // Load database driver.
      
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      
Connection c = DriverManager.getConnection(
          "jdbc:odbc:Person");
       Statement s = c.createStatement();
       s.execute(
         
"CREATE TABLE PersonData2 " +
         
"(Name Text, Gender Text, Age Number)");
       s.executeUpdate(
         
"INSERT INTO PersonData2 VALUES " +
         
"('Nancy', 'F', 19)");
       c.close();
}
UPDATE Person SET Age = Age + 1 WHERE Gender = 'F';
DELETE Person WHERE Age > 55;
DROP TABLE Person;
// Sample Java Application to create a new
import java.sql.*;
public class Main
       // Create Connection object.
       // Create Statement object.
       // Create PersonData2 table if
       // Create BufferedReader object.
       // Insert rows into the table.
      
// Close the Connection object c.
// table in an Access database and populate
// it with data from text file.
import java.io.*;
import java.util.*;
{
    public static void main(String[] args)
            
throws SQLException
    {
       // Load database driver.
       try
       {
         
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       }
       catch(ClassNotFoundException e)
       {
         
System.out.println(e);
       }
      
Connection c = DriverManager.getConnection(
         
"jdbc:odbc:Person");
       Statement s = c.createStatement();
       // table does not exist already.
       try
       {
          s.execute(
            
"CREATE TABLE PersonData2 " +
            
"(Name Text, Gender Text, Age Number)");
       }
       catch(SQLException e)
       {
          System.out.println(
            
"PersonData2 table already exists.");
       }
       BufferedReader br = null;
       try
       {
         
br = new BufferedReader(
            
new FileReader("d:\\Person.txt"));
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
       String line, name, gender, sql;
       int age;
       StringTokenizer st;
       try
       {
         
while ((line = br.readLine()) != null)
          {
            
st = new StringTokenizer(line, " ");
            
name = st.nextToken();
            
gender = st.nextToken();
            
age = Integer.parseInt(st.nextToken());
            
sql = "INSERT INTO PersonData2 " +
               
"VALUES ('" + name + "', '" +
               
gender + "', " + age + ")";
            
// The next line is for debugging.
            
System.out.println(sql);
            
s.executeUpdate(sql);
          }
       }
       catch(IOException e)
       {
         
System.out.println(e);
       }
       c.close();
   
}
}
INSERT INTO PersonData2 VALUES ('Mary', 'F', 23)
INSERT INTO PersonData2 VALUES ('Robert', 'M', 31)
INSERT INTO PersonData2 VALUES ('Sally', 'F', 35)
INSERT INTO PersonData2 VALUES ('Jane', 'F', 28)
INSERT INTO PersonData2 VALUES ('Tony', 'M', 30)
INSERT INTO PersonData2 VALUES ('Alice', 'F', 41)
INSERT INTO PersonData2 VALUES ('Scott', 'M', 27)
INSERT INTO PersonData2 VALUES ('Larry', 'M', 49)