Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP and DB.

Similar presentations


Presentation on theme: "JSP and DB."— Presentation transcript:

1 JSP and DB

2 A web application is nothing more than a front end for a database (DB).
what makes web pages dynamic is precisely the fact that there is a significant amount of data behind them. In JAVA, we USE JDBC API to access DBs from an application.

3 JDBC JAVA Database Connectivity.
Provides a mean for JAVA applications to access different kind of DBMS through a JDBC driver. The JDBC API provides a call-level API for SQL-based database access.

4 Through JDBC you can: Establish a connection with a database.
Send an SQL statements to the DB. And process the results.

5 Establishing a JDBC connection
First, you need to establish a connection with the data source you want to use. A data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver. Typically, a JDBC application connects to a target data source using one of two classes: DriverManager: This fully implemented class connects an application to a data source, which is specified by a database URL. OR, DataSource: 

6 Establishing a JDBC connection
In both methods you need to provide the DB URL, user name and password (collectively knows as a connection string). I.e: Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:8888/ebookshop", "myuser", “mypass");

7 Establishing a JDBC connection
Typically, in the database URL, you specify the name of an existing database to which you want to connect. For example, the URLjdbc:mysql://localhost:3306/mysql  represents the database URL for the MySQL database named mysql.

8 Create an SQL statement
create an object of type java.sql.Statement. Statement stmt = conn.createStatement(); The Statement class has 40 methods, plus some more inherited ones. Nevertheless, two methods are likely to satisfy most of your needs: executeQuery and executeUpdate.

9 Create an SQL statement
We want to create a statement that retrieve a book_Id, title and author from a DB through JDBC driver: 1. Define the JDBC connection object: Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:8888/ebookshop", "myuser", “mypass");

10 2. create an object from the Statement class and pass to it the previous conn object: Statement stmt = conn.createStatement(); 3. Defiene a string that containing the actual SQL statement: String sql = "select book_id, title, author from books where category_id=1 order by author, title"; 4. Execute the Statement against the DB: ResultSet rs = stmt.executeQuery(sql);

11 Accessing the data from the ResultSet:
At any given time, you can only access the row of the result set pointed to by the so-called cursor. The usual way of accessing the rows of the result set is to start from the first one and “go down” in sequence.

12 while (rs.next()) { out.println(rs.getString(3) + ", " + rs.getString(2) + "<br/>"); } would produce the following output: Damon Williams, Pro PayPal E-Commerce. Michael Bowers, Pro CSS and HTML Design Patterns.

13 The next method moves the cursor down one row
The next method moves the cursor down one row. After the cursor goes past the last row, next() returns false, and the while loop terminates. Initially, the cursor is positioned before the first row. Therefore, you have to execute next() once in order to access the very first row.

14 The ExecuteUpdate() Method
You use the executeUpdate method to execute the SQL statements insert, update, and delete. For example, if you want to add a new book category to the online bookshop example, you do something like this: String sql = "insert into categories (category_id, category_name)" + " values (4, 'Comic Books')"; stmt.executeUpdate(sql);


Download ppt "JSP and DB."

Similar presentations


Ads by Google