© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
Distributed Application Development B. Ramamurthy.
JDBC CS-328. JDBC Java API for accessing RDBMS Allows use of SQL for RDBMS programming Can be used for: –embedded SQL –execution of stored queries.
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.
CSE470 Software Engineering Fall Database Access through Java.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
Objectives Describe JDBC Describe JDBC Driver Types
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.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
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.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© 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 ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
1 Java DataBase Connectivity JDBC java.sql.*. 2 Java DataBase Connectivity Draft release of JDBC spec (3/96) Java API for executing SQL statements (Since.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Session Management.
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.
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.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
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.
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
Lec - 14.
Introduction to Server-Side Web Development using JSP and Databases
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
Presentation transcript:

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Review  A session is a long-term connection that utilizes the session layer of a network layer protocol.  A session exists for each connection between a user and an instance of a running application.  Session acts as a link between the server and the client events. The session helps the Web server to distinguish between different users.  JSP uses the sessions to store unique data of a particular client connected to a Web application. The different methods of session object include:  getAttribute()  getAttributeNames()  getCreationTime()  getId()  getLastAccessedTime()  getMaxInactiveInterval()  removeAttribute()  setAttribute()  setMaxInactiveInterval()

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Review – Contd…  Session tracking maintains the session information and keeps track of the multiple requests made by the client.  The session tracking feature in the servlets or JSP container maintains the state of a Web browser.  Cookies are text files that are stored on the user computer, and contain the session Id of the user sent by the Web server.  The cookie is sent back to the Web server with every subsequent request made by the user in the same session.  The information in the cookies helps the Web server to identify the user, as the value of each cookie is unique.  The session ID keeps track of requests made within the same session.  The session ID is encoded in the URLs that are created by the JSP pages.  URL Rewriting works with Web browsers that do not support cookies, or the cookies that are disabled on a Web browser.  The hidden field is used to store information about a session. In addition, the hidden form field helps to carry the information from one HTML page to another.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Objectives  Explain Java database connectivity  Describe various JDBC drivers  Explain use of JDBC in JSP  Describe different database operations  Explain use of JDBC connectivity through JavaBeans  Describe database connection pooling process

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity  Provides a programming interface that is used to request a connection between the application and database  JDBC API executes SQL statements and sends the results through a single API  JDBC API executes simple SQL queries in the Java code to retrieve data from database

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity  Five steps

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Loading the Driver The driver is a Java class that translates Java statements to SQL statements The Class.forName() method is used to load the driver A driver class needs to be loaded to load the driver Syntax Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Connecting to Database Connection con= DriverManager.getConnection(jdbc:odbc:Datasource, “userid”, “pwd” )  The java.sql package provides classes and interfaces that are used to interact with the database  The classes send the SQL queries to the database and process the queries  Syntax The JDBC classes for creating a connection are:  Java.sql.Driver  Java.sql.DriverManager  Java.sql.Connection

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Create Statement  Sends queries and command to the database  Created from the Connection object  Syntax Statement stat = con.createStatement()

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Execute Query  Used to send SQL queries to the database  Returns an object of type ResultSet  ResultSet() object provides methods that are used to access data from data source  Syntax String query = “Select * from table_name”; ResultSet resultset = stat.executeQuery(query);

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Processing Results  next() method of the ResultSet object is used to process the results from the database while(rs.next()) { rs.getRow(); result += " "; for ( int i = 1; i <= columns; i++) { result += " " + rs.getObject(i).toString() + " "; } result += " "; } Traversing through records using for loop Points the cursor to next row

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 ResultSet Object Methods MethodsDescription getString() Takes column number as an argument and returns value from the specified column number as a string to the ResultSet object getInt() Takes column number as an argument and returns the value from the specified column number as an integer to the ResultSet object getFloat() Takes column number as an argument and returns the value from the specified column number as float type to the ResultSet object getDate() Takes column number as an argument and returns the value from the specified column number as java.sql.Date to the ResultSet object

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 ResultSet Object Methods- Cont… MethodsDescription findColumn() Takes a column name as a string parameter and returns the column index of the specified column name wasNull() Returns true if the last column value read was SQL NULL getMetaData() Returns the information about the columns of ResultSet object in a ResultSetMetaData class

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 getMetaData() methods MethodsSyntaxDescription getColumnCou nt() int getColumnCount () Returns the number of columns in the ResultSet object getColumnNa me() String getColumnName( int column) Takes column number as a parameter and returns the designated column name getColumnTy pe() int getColumnType( int column) Takes column number as a parameter and returns the designated column’s SQL type from java.sql.Types. The types include Array, char, Integer, Date, and Float

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 getMetaData() Methods-Cont… MethodsSyntaxDescription isReadOnly()boolean isReadOnly(int column) Takes the column number as a parameter and returns true if the designated column is not writable isSearchable()Boolean isSearchable(int column) Takes column number as a parameter and returns true if the specified column can be used in where clause. isNullable()int isNullable(int column) Returns the nullability status of the specified column. The nullability status includes, columnNullable, columnNoNulls, and columnNullableUnknown

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC Connectivity - Code Snippet DB Test <% try { out.println("loading driver... "); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver "); out.println("connecting... "); Connection con= DriverManager.getConnection(jdbc:odbc:Datasource, "userid ", "pwd ") out.println("querying database... "); Statement stat = con.createStatement(); String query = "Select * from table_name "; ResultSet rs = statement.executeQuery(query); Importing Java.sql.* package Loading the driver Creating Connection object Creating Statement Executing the query

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC Connectivity - Code Snippet Cont… while (rs.next()) { out.println(rs.getString(1)+ " "); } rs.close(); Stat.close(); c.close(); } catch(Exception e) { out.println("ERROR! "+e.getMessage()); } %> Processing results Closing connections Catch exception and display error message

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC Drivers Translates Java statements to SQL statements Helps applications to interact with the database, using Java’s built-in Driver Manager JDBC driver manager maintains a list of drivers created for different databases JDBC drivers connect the Java application to the driver specified in the Java program Provides Java applications that are DBMS independent The four types of JDBC drivers are:  JDBC ODBC Bridge driver  Native API driver  Network-protocol Driver  Native protocol driver

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC ODBC Bridge Driver  Type 1 driver  Translates JDBC API to ODBC API  Enables the Java applications to interact with any database  Provides platform dependence, as JDBC ODBC bridge driver uses ODBC  JDBC-ODBC bridge is useful when Java driver is not available for a database

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Native API Driver  Type 2 driver  Provides access to the database through C/C++  Developed using native code libraries  Native code libraries provide access to the database, and improve the performance  Java application sends a request for database connectivity as a normal JDBC call to the Native API driver  Establishes the call, and translates the call to the particular database protocol that is forwarded to the database

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Network Protocol Driver  Type 3 driver  Communicates with the middle layer component that provides data connectivity  Manages multiple Java applications connecting to different databases

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Native Protocol Driver  Type 4 driver  Communicates directly with the database using Java sockets  Improves the performance as translation is not required  Converts JDBC queries into native calls used by the particular RDBMS

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Using JDBC in JSP The java.sql.* package provides database access in Java JDBC can be used in JSP by inserting JDBC query in the scriplet code After compilation, the scriplet code that contains the query is placed in the jspService() method

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Database Operations Accessing database requires JDBC code to perform database operations The JDBC code includes queries for selecting, updating, inserting, or deleting data from the database A connection object needs to be created for database access The Connection object is used to generate SQL statements, which perform database operations

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Basic Database Operations Database OperationsDescription InsertAdds the specified data in the database on execution of the SQL query UpdateEdits the specified data in the database on execution of query DeleteRemoves the specified data from the database on execution of the query SelectRetrieves the specified data from database on execution of query

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC Connectivity through JavaBeans JavaBeans components are reusable Java classes, and can be put together into applications JDBC connectivity through JavaBeans is an efficient way for interacting with database The JDBC components facilitates interaction of Java Swing components with a database JDBC connectivity through JavaBeans provides database access by inserting the JDBC code in scriplets

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JDBC Connectivity through JavaBeans <% Connection connection = null; Statement statement = null; ResultSet results = null; AccountBean Account = new AccountBean(); //code for JDBC %> //html code to display the results Import Java class Initializing variables

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Database Connection Pooling Connection pooling is a process that manages multiple database connection requests Connection pooling process increases the speed of data access, and reduces the number of database connections for an application Connection pooling process helps in improving the performance of application Connection pooling tasks include:  Pre-allocate connections  Manage available connections  Allocate new connections  Close connections

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Summary  The JDBC programming interface is used to request a connection with database to retrieve data  The JDBC API contains some classes and interfaces that connect an application to a database using DBMS/RDBMS, and execute the SQL queries  The Class.forName() method takes string as a parameter which is used to load the driver  java.sql.DriverManager maintains a list of drivers created for databases and connects the java application to the required driver  java.sql.Connection sends a series of SQL statements to the database and processes the SQL queries  The Statement object is created from the Connection object. The Statement object sends queries and commands to the database.  The executeQuery() method is used to send the SQL queries to the database.  The next() method of the ResultSet object is used to process the results from the database. The methods in ResultSet class include:  getString()  getInt()  getFloat()  getDate()

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Summary-Cont…  findColumn()  wasNull()  getMetaData()  The getMetaData() returns the information regarding the columns of ResultSet object in a ResultSetMetaData class. The getMetaData() methods include:  getColumnCount()  getColumnName()  getColumnType()  isReadOnly()  isSearchable()  isNullable()  JDBC drivers translate Java statements to SQL statements.  The JDBC driver manager connect the Java application to the driver specified in the Java program

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Summary-Cont…  JDBC drivers make Java applications DBMS independent. The four types of drivers are:  JDBC-ODBC bridge driver  Native API driver  Network protocol driver  Native protocol driver  The JDBC code includes queries for selecting, updating, inserting, or deleting the required data from the database. The basic database operations are:  Insert data  Update data  Delete data  Select data  Connection pooling is a process that handles multiple database connection requests  The tasks of Connection pooling class include:  Pre-allocate connections  Manage available connections  Allocate new connections  Close connections

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Q & A