A Quick Introduction to JDBC

advertisement
A Quick Introduction to JDBC
Sven Helmer
A Quick Introduction to JDBC – p. 1/14
Introduction
Java Database Connectivity is a standardized interface
for connecting a database to a Java application
It allows you to
create queries on the fly,
send those queries to the database,
and collect the answer
A Quick Introduction to JDBC – p. 2/14
Getting Started
You need a database system installed somewhere
On alcor.inf.unibz.it:5432 you can find a
Postgres installation
You need a Java SDK
You need a JDBC driver, for Postgres there is a Type 4
driver (pure Java implementation with network protocol
support):
http://jdbc.postgresql.org/download.html
The location of the jar file has to be added to your
classpath
A Quick Introduction to JDBC – p. 3/14
Getting Started (2)
Generally, you have to do the following to use a
database within a Java application:
connect to a database
create a statement
execute a query
collect the result
close the connection
A Quick Introduction to JDBC – p. 4/14
Connecting to a Database
DriverManager is the class that establishes the
connection
It also loads any Type 4 drivers in the classpath
Other types have to loaded manually, e.g. via
Class.forName
An alternative way of connecting to a database is via a
DataSource object (not covered here)
The URL for the data source, the user name, and the
the password are specified via strings:
String url = "jdbc:postgresql://localhost/foobar";
String user = "dbsuser";
String password = "xxxxx";
con = DriverManager.getConnection(url, user, passwo
A Quick Introduction to JDBC – p. 5/14
Database URL
In Postgres the database URL has one of the following
forms:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
where
host is the hostname (defaults to localhost)
port is the port number the server is listening on
(default is 5432)
database is the name of the database
A Quick Introduction to JDBC – p. 6/14
Creating Statements
Statements manage queries for you by
sending a query to the database
generating an object containing the result
st = con.createStatement();
A Quick Introduction to JDBC – p. 7/14
Executing Queries
The method execute or executeQuery is used to
send a query to a database
executeQuery returns one result set
execute is used when the query may return more
than one
The method executeUpdate is used for insert, delete,
and update statements
It returns the number of modified tuples
Example:
rs = st.executeQuery("select * from student");
A Quick Introduction to JDBC – p. 8/14
Collecting the Result
The interface of the class ResultSet includes
methods to retrieve the result of an executed query
There are methods to get metadata (to figure out
what the result looks like)
and methods to get the actual data
A Quick Introduction to JDBC – p. 9/14
Getting the Metadata
Methods for fetching the metadata include finding out
about
the number of columns
the names of the columns
the types of the columns
rsmd = rs.getMetaData();
int noOfCols = rsmd.getColumnCount();
int i;
for(i = 1; i <= noOfCols; i++) {
System.out.print(rsmd.getColumnLabel(i) + "\t");
}
System.out.println();
A Quick Introduction to JDBC – p. 10/14
Getting the Data
You can step through the result set tuple by tuple
and fetch each attribute value of the current tuple
while (rs.next()) {
for(i = 1; i <= noOfCols; i++) {
String a = rs.getString(i);
System.out.print(a + "\t");
}
System.out.println();
}
A Quick Introduction to JDBC – p. 11/14
Closing the Connection
Once you are finished, it makes sense to free all the
resources again
There are close methods for
result sets
statements
connections
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
A Quick Introduction to JDBC – p. 12/14
Error Handling
If an error occurs during the execution of a JDBC
method, an SQLException will be thrown
So you have to run code connected to your database
connection in a try/catch block
More details and an example can be found in the
example code found on the course web page
A Quick Introduction to JDBC – p. 13/14
A Quick Introduction to JDBC – p. 14/14
Download