Lecture 9 Slides

advertisement
GSTIJP - An Introduction to Java Programming
Session 9: Database Connectivity
Martin R. Nelson
Databases & JDBC
• A database is a structured collection of records or data.
• Data is commonly held in a series of tables, which belong to
the database.
• Java has a series of tools for connecting to various types of
database, termed JDBC - Java Database Connectivity.
• For this session we assume that you are using a MySQL
database with the MySQL Connector/J JDBC driver
correctly installed and configured.
• We also assume that the administrator has created an empty
database called “mrndemo” which is accessible by the user
“mrnuser” with the password “mrnpass”.
A Brief SQL Overview
• SQL stands for Structured Query Language.
• Pronounced ‘Ess-Que-El’ or ‘Sequel’.
• We will use the following SQL statements:
• CREATE - to create a table
• INSERT - to insert data into a table
• SELECT - to retrieve data from a table
• WHERE - to narrow-down data for retrieval
• DROP - delete a table
• Note, unlike Java, SQL is not case sensitive.
Three Key Steps
• Step 1: Register the Driver & Create the Connection
Key interface - Connection
• Step 2: Query the Database
Key interface - Statement
• Step 3: Handle the Results
Key interface - ResultSet
Step 1: Making The Connection
• We first need to import the SQL package:
import java.sql.*;
• We must then register the driver, so as to make it available.
We use the forName method, which belongs to class Class:
Class.forName("com.mysql.jdbc.Driver").newInstance();
• Essentially the Driver class will create an instance of itself,
and then register itself with the DriverManager.
• We then we create an instance of the Connection interface,
using the DriverManager’s getConnection method:
Connection con =
DriverManager.getConnection(URL,username,password);
• All these steps MUST be error trapped!
Step 2: Using SQL Statements
• The Statement interface holds all the required methods for
sending statements to a database and collecting the results.
• We create a statement using the createStatement method,
which belongs to the corresponding Connection.
Statement stmt = con.createStatement();
• We store the actual SQL syntax in a String, called sqlStr for
example.
• We use the executeQuery method to retrieve data, eg. when
using a SELECT statement.
• We use the executeUpdate method to update data, eg. for
INSERT or DROP.
• For example:
String sqlStr = "INSERT some_stuff";
stmt.executeUpdate(sqlStr);
Step 3: Handling Query Results
• The ResultSet interface is essentially a table of data
representing the results of a database query.
• We create a results set using the Statement interface’s
executeQuery method:
ResultSet results = stmt.executeQuery(sqlStr);
• The ResultSet interface has a number of methods to enable
you to browse through and edit the data, including...
• next() and previous() - Move the cursor down/up one row
from its current position. Returns true if the new current row
is valid; false if there are no more rows
• absolute(int row) - Moves to the specified row number.
• deleteRow() - Deletes the current row.
• close() - Releases this ResultSet object’s database and JDBC
resources immediately.
• Note: Any Statement can only ever have one ResultsSet
object in existance at any point.
Time for an Example!
Now to put it all into practise...
Download