This document discusses database connectivity in Java, including:
1) Using SQL queries like INSERT, UPDATE, DELETE to manipulate data as well as ORDER BY and aggregate functions.
2) The ResultSet object maintains a cursor to process query results and has methods to access fields and navigate rows.
3) It is important to close Statement, ResultSet and Connection objects to free up resources when finished with database operations.
2. Database Connectivity - STEP 3: Query
SQL Queries
• The INSERT INTO statement is used to insert new
records in a table.
• INSERT INTO table_name(field1, field2,field3,…)
VALUES (value1,value2,value3,...);
• The UPDATE statement is used to update existing
records in a table.
• UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
• The DELETE statement is used to delete rows in a
table.
DELETE FROM table_name
WHERE some_column=some_value;
SQL Queries
• The ORDER BY keyword is used to sort the result-set.
• SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name
ASC|DESC;
• Aggregate Functions
• SELECT Avg(fieldname) FROM tablename
• SELECT Count(fieldname) FROM tablename
• SELECT Max(fieldname) FROM tablename
• SELECT Min(fieldname) FROM tablename
• SELECT Sum(fieldname) FROM tablename
Instructor: Tanzila Kehkashan
2
3. Database Connectivity - Step 4: Process Results
• ResultSet object maintains a cursor pointing to its current row of data.
• ResultSet.FETCH_FORWARD • ResultSet.FETCH_REVERSE
• ResultSet.TYPE_SCROLL_SENSITIVE • ResultSet.TYPE_SCROLL_INSENSITIVE
• ResultSet.CONCUR_UPDATABLE • ResultSet.CONCUR_READ_ONLY
ResulSet object methods Description
boolean absolute(int row) • Moves the cursor to the given row number in this ResultSet object.
void afterLast() • Moves the cursor to the end of this ResultSet object, just after the last row.
void beforeFirst() • Moves the cursor to the front of this ResultSet object, just before the first row.
boolean getBoolean(int) • Retrieves value of column index in current row of this ResultSet object as Boolean.
boolean getBoolean(String) • Retrieves value of column name in current row of this ResultSet object as Boolean.
byte getByte(int)
byte getByte(String)
Date getDate(int)
Date getDate(String)
double getDouble(int)
double getDouble(String)
Instructor: Tanzila Kehkashan
3
4. Database Connectivity - Step 4: Process Results
ResulSet object methods Description
float getFloat(int)
float getFloat(String)
int getInt(int)
int getInt(String)
long getLong(int)
long getLong(String)
Object getObject(int)
Object getObject(String)
short getShort(int)
short getShort(String)
String getString(int)
String getString(String)
Time getTime(int)
Time getTime(String)
URL getURL(int)
Instructor: Tanzila Kehkashan
4
5. Database Connectivity - Step 4: Process Results
ResulSet object methods Description
URL getURL(String)
boolean isAfterLast()
boolean isBeforeFirst()
boolean isClosed()
boolean isFirst() Retrieves whether the cursor is on the first row of this ResultSet object.
boolean isLast()
boolean first() Moves the cursor to the first row in this ResultSet object.
boolean last() Moves the cursor to the last row in this ResultSet object.
boolean next() Moves the cursor froward one row from its current position.
boolean previous() Moves the cursor to the previous row in this ResultSet object.
Statement getStatement() Retrieves the Statement object that produced this ResultSet object.
int getRow() Returns current row number
int getFetchDirection()
void setFetchDirection(int) Gives a hint as to direction in which the rows in this ResultSet object will be processed.
void refreshRow() Refreshes the current row with its most recent value in the database.
Instructor: Tanzila Kehkashan
5
6. Database Connectivity - Step 5: Close
It is a good idea to close the Statement and Connection objects when you have finished with them.
• Close the ResultSet object
• close()
• Close the Statement object
• close()
• Close the connection
• close()
Instructor: Tanzila Kehkashan
6