Chapter 7-Java Database Connectivity
Chapter 7-Java Database Connectivity
ORACLE
oracle.jdbc.driver.OracleDrive jdbc:oracle:thin:@hostname:port
r Number:databaseName
jdbc:sybase:Tds:hostname: port
Sybase com.sybase.jdbc.SybDriver
Number/databaseName
1. Connecting to a Database
• The first step to establishing a connection using
JDBC involves registering the driver class.
• To do that, use the forName method of the Class
class, specifying the package and class name of the
driver. For example, to register the MySQL
connector:
Class.forName(“com.mysql.jdbc.Driver”);
• Note that the forName method throws
ClassNotFoundException, so you have to enclose
this statement in a try/catch block
Cont..
• After you register the driver class, you can call the
static getConnection method of the DriverManager
class to open the connection. This method takes
three String parameters: the database URL, the
user name, and a password. i.e
String url = “jdbc:mysql://localhost/databaseName”;
String user = “root”;
String pw = “pw”;
con = DriverManager.getConnection(url, user, pw);
• java.sql.Connection
• Represents a single logical DB connection; used for
sending SQL statements
2. Creating Statements
After you connect to a database, you get a
Connection object.
The Connection class contains the methods for
creating SQL statements.
Statement interface represents a SQL statement
There are three types of Statement objects
1. Simple Statements
It represents a simple SQL statement.
Cont..
Connection interface methods for creating
Statement object
public Statement createStatement() throws
SQLException. Creates a simple SQL statement.
The result set of this statement will be read-only
and forward scrolling only.
Statement statement =
connection.createStatement();
statement.executeUpdate(“INSERT INTO Employees
VALUES (101, 20.00,‘Gashaw’, ‘Alene’)”);
Cont..
• public Statement createStatement(int resltSetType, int
concurrency) throws SQLException.
• Creates a simple SQL statement whose result set will have
the given properties. The resultSetType is either
• TYPE_FOR-WARD_ONLY,
• TYPE_SCROLL_INSENSITIVE, or
• TYPE_SCROLL_SENSITIVE, which are static fields in
the java.sql.ResultSet interface.
• The concurrency type is CONCUR_READ_ONLY or
CONCUR_UPDATABLE, for denoting whether the
Resultset is updatable or not.
Prepared Statements
Executing a Prepared
Statement
Executing a Prepared Statement
• After the values of all the parameters are set, the
prepared statement is executed using one of the
following methods in the PreparedStatement
interface
public ResultSet executeQuery() throws
SQLException.
• Use this method if the SQL statement returns a
resultset, like a SELECT statement.
Cont..
public int executeUpdate() throws SQLException
Use this method for statements like INSERT,
UPDATE, or DELETE. The return value is the
number of rows affected.
public boolean execute() throws SQLException.
• This method executes any type of SQL statement.
• Use the getResultSet() method to obtain the
result set if one is created.
Scrollable Result Sets
Statement stat = conn.createStatement(type,
concurrency);
• For a prepared statement, use the call
PreparedStatement stat =
conn.prepareStatement( command,type, concurrency );
• The possible values of type are:
TYPE_FORWARD_ONLY The result set is not scrollable.
TYPE_SCROLL_INSENSITIVE The result set is scrollable but not
sensitive to database changes
TYPE_SCROLL_SENSITIVE The result set is scrollable and
sensitive to database changes.
Updatable Result Sets
• If you want to be able to edit resultset data and have the changes
automatically reflected in the database, you need to create an
updatable result set.
• Updatable result sets don't have to be scrollable, but if you present
data to a user for editing, you usually want to allow scrolling as well.
Parameter Description