JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.

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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
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 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.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Dale Roberts 8/24/ Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
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 (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
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.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
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.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
1 Introduction to Web Applications & APIs. 2 Agenda  Architecture of Web Applications Three layer architecture  Web Server (Tomcat) Installation Data.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
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.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
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 Session 14.
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.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
JDBC 21-Aug-18.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
Bolat Azamat, Kim Dongmin
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC CS 260 Database Systems

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

Introduction  JDBC (Java Database Connectivity) is a technology that allows Java applications to communicate with a database  Manages connections between the application and the database  Send DDL and DML statements to the database  Call stored database programs  Java applications interact with database-specific drivers  e.g. Oracle vs. MySQL

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

JDBC Driver Types  Type 1: JDBC-ODBC bridge  JDBC calls are converted to ODBC function calls ODBC (Open Database Connectivity) is intended to be database and OS independent  Useful in situations where a Java application needs to communicate with an existing ODBC driver

JDBC Driver Types  Type 2: Native-API Driver  JDBC calls are converted to native calls of the database API  Useful in situations where an ODBC driver isn’t needed and an existing database library API exists

JDBC Driver Types  Type 3: Network-Protocol Driver  JDBC calls are converted directly or indirectly into the vendor-specific database protocol(s) by a middle-tier application server  Useful in situations where such an application server exists Reduces application ties to vendor-specific database systems

JDBC Driver Types  Type 4: Database-Protocol Driver  JDBC calls sent directly to a vendor-specific database  Useful in situations where the application is tied to a vendor- specific database We’ll use this “thin” driver in our applications

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

Eclipse Project Setup  Download and import the appropriate JDBC driver jar file (Oracle thin client driver available on web)  Copy the jar file to your project in the file system  Done here in a “lib” directory at the project root  Import the jar file to your project  You may need to “refresh” your project first  Add the jar to your project’s build path Select your project > Project > Properties > Java Build Path > Libraries tab > Add JARs

Eclipse Project Setup Step 3: Project > Properties > Java Build Path > Libraries Tab > “Add JARs…” button > jar selection Step 1: jar file manually copied to the project’s lib directory Step 2: Eclipse project refreshed, making the jar file visible Step 4: You should see the jar file here > OK (unseen here)

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

Programming with JDBC  Steps  Import the Java sql package  Create a database connection object using… The JDBC driver identifier and database URL Database user credentials  Create “Statement” objects as needed using… The database connection A string containing the SQL to execute  Execute the statement, which may return a “ResultSet” Iterate through the records in the ResultSet, accessing field values one record at a time  Close the ResultSet, Statement, and Connection objects

Programming with JDBC Import the Java sql package Create a database connection object

Programming with JDBC Create a statement object Create a resultset object Iterate through the records in the resultset accessing field values one record at a time

Programming with JDBC  Executing a statement object  executeQuery(String sql) Useful for executing SELECT statements Returns a ResultSet object  executeUpdate(String sql) Useful for executing INSERT, UPDATE, and DELETE statements Returns the number of rows affected  execute(String sql) Useful for executing DDL statements Returns a boolean value indicating whether a ResultSet object can be retrieved

Programming with JDBC  Using the ResultSet object  next() Retrieves the next record in the results (if it exists) Returns a boolean indicating whether or not another record exists in the result set  getString(String fieldName) Returns the value of the input field name for the current record in the result set and formats it as a String Similar methods exist for other types getInt(String), getDate(String), getObject (String) These also return and format values in the result set

Programming with JDBC  Close these objects in a finally block so that they are closed regardless of whether or not an exception occurs  Some third party libraries will do this for you if you use their database connectivity utilities Close the ResultSet, Statement, and Connection objects

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

Prepared Statements  The Statement objects that we’ve seen thus far execute static SQL commands  Applications often need to execute dynamic queries based on user input  The PreparedStatement class allows for dynamic queries whose values may be provided at runtime  Prepared statements are compiled using placeholders for parameters  These parameters are then inserted using values provided by the user at runtime

Prepared Statements  Why use prepared statements?  More efficient than Statement objects that accept an SQL string constructed at runtime  Prevents SQL injection attacks when used to execute action queries More on this shortly…  Approach  Create a query string using ? as a placeholder for a parameter value Do not include single quotes for strings  Use set methods to specify parameter values for the ? placeholders

Prepared Statements  Examples  Retrieving data  Updating data Parameter assignment begins with 1 (not 0) Call PreparedStatement’s executeQuery() method when executing a SELECT statement Call PreparedStatement’s executeUpdate() method when executing an INSERT, UPDATE, or DELETE statement

Prepared Statements  Type conversions between Oracle data types and Java data types  The same Oracle/Java data types are compatible using the JDBC getXXX() methods

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

SQL Injection Attacks  An SQL injection attack is an attack on a database- driven application in which the attacker executes unauthorized SQL commands  Possible when a query is constructed using user input values  They can be prevented using input validation  Example  sqlInjection/login.htm

SQL Injection Attacks  Injection types  Incorrectly filtered escape characters  Incorrect query termination statement = “SELECT * FROM data WHERE id = “ + someId; User input (stored in someId): 1;DROP TABLE users Rendered as: SELECT * FROM DATA WHERE id=1;DROP TABLE users statement = “SELECT * FROM users WHERE name = ‘” + userName + “’ AND password = ‘” + userPassword + “’”; User input (stored in both variables): ‘ OR ‘t’ = ‘t Rendered as: SELECT * FROM users WHERE name=‘’ OR ‘t’=‘t’ AND password = ‘’ OR ‘t’=‘t’

SQL Injection Attacks  How to prevent SQL injection attacks  Prepared statements will prevent these types of SQL injection attacks Other programming languages have “parameterized” statements similar to JDBC’s “prepared” statements  Filtering Manually parse and remove dangerous characters from user input May be difficult to anticipate all possibilities

Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL injection attacks  Best practices

Best Practices  Close JDBC related objects (connections, statements, result sets, etc.) in a finally block whenever possible  This ensures that these objects will be closed whether or not an exception occurs  The database limits the number of open connections that a user can have Could max out if left open  Use prepared statements whenever a query requires parameters  Safer and more efficient

Best Practices  Minimize database connections whenever possible  These are expensive and can be reused  Some 3 rd party libraries can manage database “connection pools” for you  Decouple your application’s business logic and data models from JDBC usage as much as possible  Allows your application to use other data sources more easily