Java Database Connectivity: Possibly Remote
Java Database Connectivity: Possibly Remote
possibly remote
Database
6.1 Preliminaries
2
Connecting to the DB
(username, password, ...)
3
Executing commands
"update" "query"
4
Closing connection
74
6.2.1 LOADING THE DRIVER
• Goal: loading the Driver (i.e. making it available for our program)
• What is a Driver: in general a .jar file (Java Archive) which can be
reached by the Java environment at run time (see later).
• Where to get the Driver from:
– names like pgjdbc1.jar, pgjdbc2.jar, jdbc7.1-1.1.jar.
– may be part of the installed Java environment
– may be part of the Database environment (comes with the install
package)
– from the Web: java.sun.com, from the Database’s site or from other
vendors.
• Important: you have to make the driver reachable by the Java envi-
ronment BEFORE running your Java program, i.e. you have to set up
the CLASSPATH. E.g. (for UNIX bash)
export CLASSPATH=.:pgjdbc1.jar
Example:
try {
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException c)
{
System.out.println("Database driver not found...");
}
• this loads the Driver class as part of the DriverManager class, used in
the next step
• the driver’s documentation gives the class name to use
• here we used the driver for PostgreSQL 7.2.2 and Java 1.1.8.
• there is a common driver called ”Open Database Connectivity (ODBC)”
which works for a lot of databases.
75
Other examples:
Class.forName("sun.jdbc.odbc.JdbcObdcDriver");
Class.forName("jdbc.DriverXYZ");
Class.forName("postgresql.Driver");
About Classes:
Class.forName:
java.lang.Object
|
+--- java.lang.Class
76
6.2.2 CONNECTING TO THE DATABASE SERVER
String url="jdbc:postgresql://twoquid.cs.kun.nl/test";
• you should get rights to connect (user name, password) BEFORE con-
necting (set it up at the server side).
77
• it is possible that the server needs a setting like: ”accept TCP/IP
connections”
• you should have an open port (set up the firewall for the server or
contact the server administrator). Check it BEFORE connecting.
About classes:
java.lang.Object
|
+--java.sql.DriverManager
78
public interface Connection
(defined in: java.sql)
"A connection (session) with a specific database. Within
the context of a Connection, SQL statements are executed
and results are returned."
methods (e.g.):
void close()
"Releases a Connection’s database and JDBC resources
immediately instead of waiting for them to be automatically
released."
Statement createStatement()
"Creates a Statement object for sending SQL statements to
the database." We will use it in the next step.
void commit()
"Makes all changes made since the previous commit/rollback
permanent and releases any database locks currently held by
the Connection."
boolean isClosed()
"Tests to see if a Connection is closed."
It has fields, too.
• database servers have generally a default port, for example PostgreSQL
uses the port number 5432. You don’t have to care about it: the driver
knows the default port number - or you should set up the server and
invoke the driver with the right port number.
• of course, a DB server handles usually several databases for ex. in a
sw. company one for helping in the person administration of the firm,
one for a project to a hospital, one for testing the database scalability,
etc. Every database may contain a lot of tables and relations between
them. Now we can connect to one of the databases.
• We have used the Java documentation for jdk1.2 . It might be different
from those for JEE2.
79
6.2.3 EXECUTING COMMANDS
Remark: It has a lot of methods for all kind of contents and indexing on
column index, column name, etc. Cursor can be used on other ways, too.
It has fields, too.
81
6.2.4 CLOSING THE CONNECTION
import java.sql.*;
class JDBCTester {
try {
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException c)
{
System.out.println("Database driver not found...");
}
82
String url="jdbc:postgresql://twoquid.cs.kun.nl/test";
String user="Kees", pass="secret";
try {
Connection con=DriverManager.getConnection(url, user, pass);
Statement stat=con.createStatement();
stat.executeUpdate("create table persons " +
"(first varchar(15), last varchar(15));");
stat=con.createStatement();
stat.executeUpdate("insert into persons " +
"(first, last) values (’Donald’, ’Duck’);");
stat=con.createStatement();
stat.executeUpdate("insert into persons " +
"(first, last) values (’George’, ’Bush’);");
stat=con.createStatement();
ResultSet res=stat.executeQuery("select * from persons;");
while (res.next())
{
System.out.println(res.getString(1)+ " " +res.getString(2));
}
res.close();
stat=con.createStatement();
stat.executeUpdate("drop table persons;");
con.close();
}
// Closing is obligatory, otherwise we get a compiler error.
catch (SQLException s)
{
System.out.println("Couldn’t connect to database... " + url);
}
83
}
84