database access using jdbc bcis 3680 enterprise programming

28
Database Access Using JDBC BCIS 3680 Enterprise Programming

Upload: philippa-palmer

Post on 19-Jan-2016

250 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Access Using JDBC BCIS 3680 Enterprise Programming

Database Access Using JDBC

BCIS 3680 Enterprise Programming

Page 2: Database Access Using JDBC BCIS 3680 Enterprise Programming

2

Overview Connecting to databases

JDBC driver Connection steps

Connection String Classes used Methods used

ResultSet Reading values from ResultSet

Page 3: Database Access Using JDBC BCIS 3680 Enterprise Programming

3

Working with Databases The java.sql package contains many classes

for working with databases, e.g., Connection Statement ResultSet

They are designed to be independent of the vendor of the DBMS you connect to. This abstraction is achieved by adding a driver

layer between these classes and the DBMS.

Page 4: Database Access Using JDBC BCIS 3680 Enterprise Programming

4

Database Access from JSP To connect to a database for dynamic

contents, you want to establish a JDBC connection inside your JSP page. Other options are available, e.g., ODBC. But JDBC

is straightforward and you don’t have to configure DSNs on the local machine.

JDBC is a technology developed by Sun to allow to access virtually any database system from JSP pages or Java applications.

Page 5: Database Access Using JDBC BCIS 3680 Enterprise Programming

5

Using JDBC Driver to Work with MySQL Download the connector zip from the course

website. The file unzips to the mysql-connector-java-5.1.7 folder, which contains the JDBC connector file called mysql-connector-java-5.1.7.bin.jar.

In the deployment folder for your web application, create a WEB-INF subfolder as usual. Under that subfolder, create a subfolder named lib.

Copy the connector JAR file - mysql-connector-java-5.1.7-bin.jar - into \WEB-INF\lib.

Import classes from the java.sql package. Do this by using a page directive for importing.

<%@ page import="java.sql.*" %>

Page 6: Database Access Using JDBC BCIS 3680 Enterprise Programming

6

Steps to Use Database in JSP Load the JDBC driver Compose the connection string Establish the connection Create the statement object Execute a query or update Process the results Close the connection

Page 7: Database Access Using JDBC BCIS 3680 Enterprise Programming

7

Loading the JDBC Driver The driver acts as the bridge between the

JDBC classes and the database. The driver is a piece of software that knows

how to talk to the DBMS. To load a driver:

Class.forName("com.mysql.jdbc.Driver"); This is just another way to say:

com.mysql.jdbc.Driver aDriver =

new com.mysql.jdbc.Driver(); Calling the newInstance() method (such as p.

331 in Metlapalli book) is redundant.

Page 8: Database Access Using JDBC BCIS 3680 Enterprise Programming

8

Defining the Connection String Use the jdbc: protocol followed by specific

subprotocal and the name of the database. For some subprotocals, you may need to specify

the database server host name and port number, user name and password, etc.

The connection string is different for different database management systems.

Example:jdbc:mysql://localhost:3306/forta?

user=root&password=bcis3680 It’s a good idea to save this as a String

variable.

Page 9: Database Access Using JDBC BCIS 3680 Enterprise Programming

9

Parts of a Connection Stringjdbc:mysql://localhost:3306/forta?user=root&password=bcis3680

• Note what characters (:, ?, &, etc.) connect the different parts.

Part Meaning

jdbc JDBC protocol

mysql Subprotocol for MySQL

//localhost

Network notation for the local computer

3306 Default TCP port to receive connection for MySQL. If you have configured a different port, use the number for that port instead.

forta Name of the MySQL database you want to access.

user=root Specifying the username of the MySQL user account that accesses the database on behalf of the WebApp. In production environments you don’t want to use “root”.

password=bcis3680

Providing password for the user account used. Note this is visible in your JSP source code.

Page 10: Database Access Using JDBC BCIS 3680 Enterprise Programming

10

Establishing the Connection Create a connection to the database server. To make the actual network connection, you

need to pass the connection string to the getConnection() method of the DriverManager class, as follows:Connection cxn = DriverManager.getConnection(cxnString);

Page 11: Database Access Using JDBC BCIS 3680 Enterprise Programming

11

Creating the Statement Object The connection object only establishes a

connection to the database. To actually issue SQL statements to the DBMS, you

need a Statement object. When we are at a DBMS console (e.g., MySQL console),

we can interact with it directly by running SQL commands (interactive access).

But JSP code can’t talk to the DBMS console interactively. So we need to set up a way for the code to send SQL commands to the DBMS when needed (programmatic access).

We create a Statement object by calling a method of the connection object.

Statement stm = cxn.createStatement();

Page 12: Database Access Using JDBC BCIS 3680 Enterprise Programming

12

Executing SQL Statements in Code A single Command object can be used to run

multiple SQL statements. But we need to call the right method, depending on the type of the statement.

For SQL statements that modify the database – executeUpdate() For creating, deleting, or updating row(s) in table(s). This method is a value-returning method.

Returns an integer indicating how many rows were affected. Returns 0 if the update fails.String sql = "CREATE TABLE product " +

"(productID char(5), name varchar(15))";

stm.executeUpdate(sql);

Page 13: Database Access Using JDBC BCIS 3680 Enterprise Programming

13

Executing SQL Statements in Code For SQL statements that do not modify the

database – executeQuery() For retrieving data from table(s). This method also is a value-returning method. But it returns a ResultSet object containing the

selected rows from table(s) in the database.String sql = "SELECT * FROM customers";

ResultSet rs = stm.executeQuery(sql);

Page 14: Database Access Using JDBC BCIS 3680 Enterprise Programming

14

The next() Method The ResultSet class has a next() method. When a ResultSet is returned, the cursor is “parked”

outside of it. Data in the ResultSet is not read or displayed automatically.

We must call the next() method to move the cursor into the rows in the ResultSet to read data.

Each call to next() moves the cursor forward by one row. The next() method is a value-returning method. It

performs an action (moving into next row) and returns a boolean value: true if the cursor moved into a readable row. false if the cursor didn’t move into a readable row, meaning:

All rows in the ResultSet have been read, or The ResultSet is empty.

Page 15: Database Access Using JDBC BCIS 3680 Enterprise Programming

15

Non-Empty ResultSet If the ResultSet is not empty (the query found

matching data): Calling next() for the first time moves the cursor into

the first row in the data – returns true. We know the ResultSet is not empty.

As long as the last row of data has not been reached, each time next() is called, the cursor moved into a readable row – returns true.

Calling next() when the cursor is at the last row of data moves the cursor out of the last row. Now the cursor is at a place where there is nothing to read – returns false.

So, in the case of a non-empty ResultSet, when false is returned from calling next(), we know we’re done with reading data.

Page 16: Database Access Using JDBC BCIS 3680 Enterprise Programming

16

ResultSet with Three Rows

Page 17: Database Access Using JDBC BCIS 3680 Enterprise Programming

17

Accessing Row 1

Page 18: Database Access Using JDBC BCIS 3680 Enterprise Programming

18

Accessing Row 2

Page 19: Database Access Using JDBC BCIS 3680 Enterprise Programming

19

Accessing Row 3

Page 20: Database Access Using JDBC BCIS 3680 Enterprise Programming

20

Done with Accessing ResultSet

Page 21: Database Access Using JDBC BCIS 3680 Enterprise Programming

21

Empty ResultSet If the ResultSet is empty (the query found no

matching data), calling next() for the first time was not able to move the cursor into any readable data – returns false.

So, if false is returned after we called next() for the very first time, we know the ResultSet is empty and contains no data.

Page 22: Database Access Using JDBC BCIS 3680 Enterprise Programming

22

Empty ResultSet

Page 23: Database Access Using JDBC BCIS 3680 Enterprise Programming

23

How Do You Know It Is Empty

Page 24: Database Access Using JDBC BCIS 3680 Enterprise Programming

24

FYI – Scrollable ResultSet ResultSet by default is forward-only. For some particular reasons, e.g., to check whether

a ResultSet is empty before you ever wants to access it, you can change its behavior to scrollable or insensitive to the direction of scrolling.

This is done by creating a different type of Statement object. For example,

Statement queryStatement = connection.createStatement(

ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

Then, you will be able to run the previous() method in addition to next().

Page 25: Database Access Using JDBC BCIS 3680 Enterprise Programming

25

Reading Data While we are at a row in the ResultSet, we

can read data stored in that row, one column at a time.

To read data, use a get???() method, where ??? represents a data type that is compatible with the data type of that column (as defined in the table’s schema).

For example, if the column is of a text type (e.g., varchar), then use getString().

If the column has integer as its data type, then use getInt().

Page 26: Database Access Using JDBC BCIS 3680 Enterprise Programming

26

Reading Data When using get???() methods, an argument

needs to be passed to indicate which column in the row we want to read.

We can pass the name of the column in the form of a String.

Alternative, if we know the order of the columns in the ResultSet, we can pass the column’s position in the form of an integer. Note the counting starts at 1, not 0. So to read the

third column the method call will be get???(3).

Page 27: Database Access Using JDBC BCIS 3680 Enterprise Programming

27

Reading Data Often a ResultSet contains multiple rows of data.

Since the order and data types of the columns are uniform throughout the rows, the data reading actions are the same for every row.

We therefore want to code the actions as the loop body of a loop and let them be repeated for as many times as there are rows in the ResultSet. Typically a while loop is used.

To get out of the loop, we use the return value of the next() method as the “sentinel value”. After the last row is read, calling next() moves the

cursor into “nothingness”. The method call returns false – the loop stops.

Page 28: Database Access Using JDBC BCIS 3680 Enterprise Programming

28

When You’re Done Disconnect from the database connection:

Close ResultSet Close Statement Close Connection