Think Possibility Integrating Web Applications With Databases.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Distributed Application Development B. Ramamurthy.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Part I Attribution These slides are based on three primary sources: –Sun JDBC Tutorial URL: jdbc/TOC.htmlhttp://java.sun.com/docs/books/tutorial/
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
CSE470 Software Engineering Fall Database Access through Java.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Accessing Databases with JDBC. Introduction to JDBC JDBC provides a standard library for accessing databases by using the JDBC API. JDBC standardizes.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JDBC 15-Apr-18.
JDBC – Java Database Connectivity
Database JDBC Overview CS Programming Languages for Web Applications
Advanced Web Automation Using Selenium
JDBC 21-Aug-18.
JDBC.
JDBC 15-Nov-18.
Interacting with Database
JDBC Example.
Presentation transcript:

Think Possibility Integrating Web Applications With Databases

Think Possibility Database Overview A database is a collection of logically related data. It is usually managed bya a database management system (DBMS). For an RDBMS, the language standard that supports database operations is called Structured Query Language (SQL).

Think Possibility Database Overview An RDBMS allows you to perform four fundamental operations: 1.Create a row in a table. 2.Retrieve one or more rows in a table. 3.Update some data in a table 4.Delete one or more rows in a table.

Think Possibility The JDBC API JDBC is the Java technology API for interacting with a DBMS. The JDBC API includes interfaces that manage connections to the DBMS, statements to perform operations and result sets that encapsulate the result of retrieval operations.

Think Possibility Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly translate between the world of the database, and the world of the Java application. The idea of a universal database access API is not a new one. For example, Open Database Connectivity (ODBC) was developed to create a single standard for database access in the Windows environment. JDBC API aims to be as simple as possible while providing developers with maximum flexibility.

Think Possibility Understanding JDBC Drivers To connect to a database, you first need a JDBC Driver. JDBC Driver: set of classes that interface with a specific database engine. Java Application JDBC Driver Manager JDBC- ODBC Bridge Vendor Specific JDBC Driver Vendor Specific JDBC Driver Database Diagram Source: Marty Hall, Core Web Programming (Prentice Hall.)

Think Possibility JDBC Drivers JDBC drivers exist for every major database including: Oracle, SQL Server, Sybase, and MySQL. For MySQL, we will be using the open source MySQL Connector/J.

Think Possibility Installing the MySQL Driver To use the MySQL Connector/J Driver, you need to download the complete distribution. Once you have extracted the distribution archive, you can install the driver by placing mysql-connector-java- [version]-bin.jar in your classpath, either by adding the FULL path to it to your CLASSPATH enviornment variable. To use the driver within Tomcat, copy the jar file above to: [TOMCAT_HOME]\webapps\ROOT\WEB-INF\lib

Think Possibility Six Steps to Using JDBC 1.Load the JDBC Driver 2.Establish the Database Connection 3.Create a Statement Object 4.Execute a Query 5.Process the Results 6.Close the Connection

Think Possibility 1) Loading the JDBC Driver To use a JDBC driver, you must load the driver via the Class.forName() method. In general, the code looks like this: Class.forName("jdbc.DriverXYZ"); –where jbdc.DriverXYZ is the JDBC Driver you want to load. If you are using a JDBC-ODBC Driver, your code will look like this: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Think Possibility Loading the MySQL Driver With MySQL Connector/J, the name of this class is com.mysql.jdbc.Driver. With this method, you could use an external configuration file to supply the driver class name and driver parameters to use when connecting to a database. try{ Class.forName("com.mysql.jdbc.Driver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage());} Class.forName() will throw a ClassNotFoundException if your CLASSPATH is not set up properly. Hence, it's a good idea to surround the forName() with a try/catch block.

Think Possibility 2) Establish the Connection Once you have loaded your JDBC driver, the next step is to establish a database connection. The following line of code illustrates the basic idea: Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");

Think Possibility Creating a Connection URL The only difficulty in establishing a connection is specifying the correct URL. In general, the URL has the following format: jdbc:subprotocol:subname. –JDBC indicates that this is a JDBC Connection (no mystery there! ) –The subprotocol identifies the driver you want to use. –The subname identifies the database name/location.

Think Possibility Example - ODBC If you are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc:. The rest of the URL is generally your data source name or database system. String url = "jdbc:odbc:basketball"; Connection con = DriverManager.getConnection(url, “admin", “adminpasswd");

Think Possibility Example - MySQL Here's how you might connect to MySQL: String url = "jdbc:mysql://localhost/basketball"; Connection con = DriverManager.getConnection(url); In this case, we are using the MySQL JDBC Driver to connect to the basketball database, located on the localhost machine. If this code executes successfully, we will have a Connection object for communicating directly with the database.

Think Possibility 3) Create a Statement Object The JDBC Statement object sends SQL statements to the database. Statement objects are created from active Connection objects. For example: Statement stmt = con.createStatement(); With a Statement object, you can issue SQL calls directly to the database.

Think Possibility 4) Execute a Query executeQuery() –Executes the SQL query and returns the data in a table (ResultSet) –The resulting table may be empty but never null ResultSet results =stmt.executeQuery("SELECT a, b FROM table"); executeUpdate() –Used to execute for INSERT, UPDATE, or DELETE SQL statements –The return is the number of rows that were affected in the database –Supports Data Definition Language (DDL) statements CREATE TABLE, DROP TABLE and ALTER TABLE

Think Possibility Useful Statement Methods getMaxRows/setMaxRows –Determines the number of rows a ResultSet may contain –Unless explicitly set, the number of rows are unlimited (return value of 0) getQueryTimeout/setQueryTimeout –Specifies the amount of a time a driver will wait for a STATEMENT to complete before throwing a SQLException

Think Possibility 5) Process the Results A ResultSet contains the results of the SQL query. Useful Methods All methods can throw a SQLException –close Releases the JDBC and database resources The result set is automatically closed when the associated Statement object executes a new query –getMetaDataObject Returns a ResultSetMetaData object containing information about the columns in the ResultSet

Think Possibility ResultSet (Continued) Useful Methods –next Attempts to move to the next row in the ResultSet –If successful true is returned; otherwise, false –The first call to next positions the cursor a the first row

Think Possibility ResultSet (Continued ) Useful Methods –findColumn Returns the corresponding integer value corresponding to the specified column name Column numbers in the result set do not necessarily map to the same column numbers in the database –getXxx Returns the value from the column specified by column name or column index as an Xxx Java type Returns 0 or null, if the value is a SQL NULL Legal getXxx types: doublebyte int Date String float short long Time Object

Think Possibility 6) Close the Connection To close the database connection: –stmt.close(); –connection.close(); Note: Some application servers, such as BEA WebLogic maintain a pool of database connections. –This is much more efficient, as applications do not have the overhead of constantly opening and closing database connections.

Think Possibility Designing a Web Application Using Database To build a database application you must design the relationship between the Model objects and the corresponding database representation. To design the Model elements of an application, you should perform the following tasks: –Design the domain objects of your application –Design the database tables that map to the domain objects –Design the business services (the Model) to separate the database code into classes using the DAO pattern.

Think Possibility Case Study: The Basketball League Domain objects represent the real-world business entities of your application. objectID year season title objectID name address city province postal_code division League Registration Player * *

Think Possibility Database Tables Each domain objects has a corresponding DB table. LID year season title objectID name address city province postal_code table_name ID_number > League > ObjectIDs > Player * * LID PID division > Registration

Think Possibility Example Data

Think Possibility Data Access Object (DAO) Pattern The DAO pattern makes it easier to maintain applications that use databases by separating the business logic from the data access logic. The DAO pattern permits the business logic and the data access logic to change independently.

Think Possibility DAO Pattern Example +getLeague +getPlayer +register RegisterService insert > PlayerDAO retrieve insert > LeagueDAO +getLeague +createLeague RegisterService getNextObjectID > ObjectIDDAO Registration servlet LeagueAdmin servlet Player League

Think Possibility Advantages of the DAO Pattern Business logic and data access logic are now separate. The data access objects promote reuse and flexibility in changing the system. Developers writing other servlets can reuse the same data access code. This design makes it easy to change front-end technologies. This design makes it easy to change back-end technologies.

Think Possibility Using a Connection Pool The DAO pattern hides the details of the data access logic from the rest of the system. The DAO classes require connections to access data in a DBMS. To develop a Web application that uses a connection pool (CP) with the DAO pattern, consider the ff: –Build or buy a CP subsystem –Store the CP object in a global “name space” –Design your DAOs to use the CP, retrieving it from the name space –Develop a servlet context listener to initialize the CP and store it in the name space

Think Possibility Connection Pool