Enterprise Systems. Lecture 02: JDBC. Behzad BORDBAR

Similar documents
Contents. Introducing the course. Aim: to learn engineering of multi-tired web based systems. Road map of the course. Roadmap..

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

Databases 2012 Embedded SQL

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

Introduction to Databases

Database Access with JDBC. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Java Database Connectivity

JDBC Programming: Intro

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

SQL in a Server Environment

Pieter van den Hombergh. March 25, 2018

Accessing databases in Java using JDBC

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Database Application Development

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

JDBC, Transactions. Niklas Fors JDBC 1 / 38

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

JDBC 3.0. Java Database Connectivity. 1 Java

DB Programming. Database Systems

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

JDBC [Java DataBase Connectivity]

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

SQL: Programming Midterm in class next Thursday (October 5)

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

UNIT III - JDBC Two Marks

Running SQL in Java and PHP

Database Application Development

Java E-Commerce Martin Cooke,

INTRODUCTION TO JDBC - Revised spring

Running SQL in Java and PHP

Java Database Connectivity

13 Creation and Manipulation of Tables and Databases

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Java Database Connectivity

JDBC - INTERVIEW QUESTIONS

INTRODUCTION TO JDBC - Revised Spring

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

SQream Connector JDBC SQream Technologies Version 2.9.3

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Database Application Development

Logging and Recovery. 444 Section, April 23, 2009

Student Number: Please fill out the identification section above as well as the one on the back page, and read the instructions below. Good Luck!

Servlet 5.1 JDBC 5.2 JDBC

PERSİSTENCE OBJECT RELATİON MAPPİNG

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

Chapter 5: Advanced SQL" Chapter 5: Advanced SQL"

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Working with Databases and Java

DB I. 1 Dr. Ahmed ElShafee, Java course

Database connectivity (II)

Using IBM-Informix datatypes with IDS 10 and web application server Keshava Murthy, IBM Informix Development

Chapter 16: Databases

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

Persistency Patterns. Repository and DAO

Database in Applica.on Development. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Lab # 9. Java to Database Connection

Server-side Web Programming

Embedded SQL. csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Meraji Winter 2018

CSE 135. Three-Tier Architecture. Applications Utilizing Databases. Browser. App. Server. Database. Server

JAVA AND DATABASES. Summer 2018

Secure Programming. CS3524 Distributed Systems Lecture 16

COMP102: Introduction to Databases, 23

Departamento de Lenguajes y Sistemas Informáticos

COMP 430 Intro. to Database Systems. SQL from application code

Database Application Development Part 2 - Chapter

Non-interactive SQL. EECS Introduction to Database Management Systems

SQL DML and DB Applications, JDBC

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Database Applications (15-415)

Enterprise JavaBeans. Layer:08. Persistence

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Lecture 2. Introduction to JDBC

Programming in Java

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

Top 50 JDBC Interview Questions and Answers

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

CHAPTER 44. Java Stored Procedures

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

How to program applications. CS 2550 / Spring 2006 Principles of Database Systems. SQL is not enough. Roadmap

Database Application Development

Topic 12: Database Programming using JDBC. Database & DBMS SQL JDBC

Instructor: Jinze Liu. Fall 2008

UNIT-3 Java Database Client/Server

Chapter 9 SQL in a server environment

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Unit 2 JDBC Programming

Transcription:

Enterprise Systems Lecture 02: JDBC Behzad BORDBAR 22

Contents Running example Sample code for beginners Properties to configure Statements and ResultSet Pitfalls of using ResultSet getobject() vs. getxxx() PreparedStatements Advantages SQL injection 23

Running example: A (seriously) simplified DB for handling on-line book orders. 24

Running example: continue A Customer can have many Orders, but each Order belongs to precisely one Customer An Order can have many OrderDetail records, but each OrderDetail belongs to precisely one Order. Each OrderDetail record refers to precisely one Book. However there may be many different OrderDetail records (spread over different Orders), that refer to the same Book. All the primary keys ID fields are automatically generated integers and therefore the primary key of each table does not need to be included when inserting new records. 25

Running example: continue Referential integrity rules are maintained on all foreign keys (ID fields which not in the first position in the tables of the figure above). Hence, for example, it will cause an error to try to insert an Order record with an Orders.CustomerID for which there is no corresponding Customers record. OrderDetail.quantity is an integer. Books.BookPrice and OrderDetails.Charge are intended to hold monetary amounts and hence are represented in SQL as type DECIMAL. This type is handled in Java via the java.math.bigdecimal class. 26

Running example (Recap and familiarise your eyes): A (seriously) simplified DB for handling on-line book orders. What is Surrogate? How does it work in PostgreSQL? Create SEQUENCE Use nextval() 27

Running example: continue custfirstname, custlastname, custaddress, custemail, bookisbn, bookname, orderstatus are Strings. Orders.orderDate is a Timestamp object in Postgresql (use a Date/Time in Access) which is handled in Java as a java.sql.timestamp object. Timestamp objects can be set to the current time: long millisecs = System.currentTimeMillis() ; Timestamp ts = new java.sql.timestamp(millisecs) ; TODO: check out Timestamp API 28

Don t use float/double for money! For monetary amounts never use float or double because of the issues with round-off and inaccuracy of translating between a floating representation and a decimal one. Remember: 2.1 in double is greater than 2.1 in float! Instead use SQL type DECIMAL, NUMERIC, MONEY, CURRENCY or something similar 29

Sample database Script shop_create.sql is available for your use to sets up the shop database in PostgreSQL (use the '\i' command in psql to import the script). Script, shop_drop.sql to delete all the tables and sequences created by the creation script so that you can easily clear out your data and start again. TODO: Review your database notes and learn how to do the above. 30

Java application interacting with DB Java Application via JDBC JDBC DB Two steps: make connection, use it! Application asks for the driver to be loaded Application asks DriverManager for a connection to a particular database DriverManager asks all loaded drivers, and one of them responds with connection. DriverManager gives connection to the Java application Connection is used 31

Sample code for beginners public class ApplicationMakesConnectionViaJDBC { public static void main(string[] args) { // first Register the driver try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { //Can't load JDBC drive, make sure classpath is correct e.printstacktrace(); } System.out.println("Driver loaded- continue "); 32

Sample code for beginners..continue // Let us set the url, my localmachine String url="jdbc:postgresql://localhost/bxb"; // If using Schools database, use the following //String url="jdbc:postgresql://dbteach/dbname"; String username ="bxb"; String password ="XYZpassword"; Very naughty! Just for training purposes 33

Sample code for beginners..continue // start the connection Connection connection= null; try { connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { //"We have problems with the connection e.printstacktrace(); System.exit(1); } //Connection seems to be OK //follow with the code for using connection"); 34

Sample code for beginners..continue Sample code: available at jumpstartjdbc/applicationmakesconnection ViaJDBC.java for your closer inspection. Username and passwd can be passed through a JDialog box. Sample code: jumpstartjdbc/ ConnectionWithUsernamePasswdFromDialogb ox.java. Don't forget to modify the url Database details can be read through a property file 35

Properties file: Database.properties # Properties file for a local PostgreSQL JDBC connection jdbc.drivers=org.postgresql.driver database.name=jdbc:postgresql://lo calhost/<database-name> database.user=<username> #Keys are in red. 36

Properties file: Database.properties # Properties file for a UBham SoCS PostgreSQL JDBC connection jdbc.drivers=org.postgresql.driver database.name=jdbc:postgresql://db teach/<database-name> database.user=<username> TODO: follow instruction in http://supportweb.cs.bham.ac.uk/servers/pos tgres/ to get a database setup 37

Using properties file public static final String PROPERTIES_FILE_NAME = "database.properties" ; Properties props = new Properties() ; FileInputStream in = new FileInputStream(PROPERTIES_FILE_NAME) ; if (in == null)// to avoid obscure errors throw new FileNotFoundException(PROPERTIES_FILE_NAME) ; props.load(in) ; String drivers = props.getproperty("jdbc.drivers") ; Class.forName(drivers) ; 38

Use Properties file to get a Connection object from DriverManager String database = prop.getproperty("database.name"); // start the connection Connection connection= null; try { connection = DriverManager.getConnection(database, username, password); } catch (SQLException e) { System.out.println("We have problems with the connection"); e.printstacktrace(); 39 }

Using the Connection obj. invoke these methods on the java.sql.connection: Statement createstatement() Returns a statement object that is used to send SQL to the data PreparectStatement preparestatement(string sql) Returns an object that can be used for sending parameterized SQL statements. CallableStatement preparecall(string sql) Returns an object that can be used for calling stored procedures. DataBaseMetaData getmetadata() Gets an object that supplies database configuration information. 40

Using the Connection obj. boolean isclosed() Reports whether the conn. to database is currently open or not. void setreadonly(boolean yn) Restores/removes read-only mode, allowing certain database optimizations. void commit() Makes all changes permanent since the previous commit/ rollback. void setautocommit(boolean yn) Restores/removes auto-commit mode, which does an automatic commit after each statement. void close() Closes the connection and releases the JDBC resources for it. 41

Statement A Statement is obtained from a Connection: Statement stmt = con.createstatement() ; stmt.executeupdate with a string argument containing the text of an SQL update query (INSERT, DELETE or UPDATE). This returns an integer count of the number of rows updated. stmt.executequery with a string argument containing the text of an SQL SELECT query. This returns a ResultSet object which is used to access the rows of the query results. stmt.execute execute arbitrary SQL statement which may be of any type. However, extracting the results, whether an integer or a ResultSet, is less convenient. This is usually only used where you want generalized access to the database that allows programmatic generation of queries. 42

Example of Statement int count = stmt.executeupdate( "INSERT INTO Customers " + "(CustomerFirstName, CustomerLastName, CustomerAddress) " + "VALUES ('David', 'Cameron', '10 Downing Street, London')"); ResultSet rs = stmt.executequery("select * FROM Customers"); // do something with count and rs 43

Example of syntax of SQL for special characters if a name, O'Neill, is to be inserted: ResultSet rs = stmt.executequery( "SELECT * FROM Customers " + "WHERE CustomerLastName = 'O''Neill' ) ; 44

How to obtain table schema If you do not know exactly the table structure of the ResultSet, use ResultSetMetaData object. ResultSetMetaData rsmd = rs.getmetadata() ; int colcount = rsmd.getcolumncount() ; for (int i = 1 ; i <= colcount ; i++) { if (i > 1) out.print(", "); out.print(rsmd.getcolumnlabel(i)) ; } out.println() ; 45

ResultSet Given any ResultSet object, you can step through it to obtain its rows, or, more specifically, the fields of its rows. Think of the ResultSet as a set of record in form of a two dimensional array on which the methods getblob(), getbigdecimal(), getdate{), getbytes(), getlnt{),getlong(), getstring()and so on, can be invoked. 46

Using getobject() on ResultSet while (rs.next()) { for (int i = 1 ; i <= colcount ; i+ +) { if (i > 1) out.print(", "); out.print(rs.getobject(i)) ; } out.println() ; 47 }

Three points about using next() the column numbers start at 1, not 0 you need to call next() to get the first record. It returns false when there is no further record. 48

Only one ResultSet can be open for given Statement Only one ResultSet can be open for given Statement. Trying to be smart to reuse the Statement twice removes the first ResultSet. So, if you are going to use a ResultSet a second time, create a new Statement object. 49

Only one ResultSet can be open for given Statement TODO: check that the following is not working: ResultSet resultl, result2 While (resultl.next()) { /* more code */ Result2 = mystmt.executequery( somesqlstring 2 ); // Ooops! You can t use mystmt twice and expect that the first ResultSet remains the same. You must use 50 another Statement.

getobject() can take column name This is better: while (rs.next()) { out.println(rs.getobject("customerid") + ", " + rs.getobject("customerfirstname") + ", " + rs.getobject("customerlastname") + ", " + rs.getobject("customeraddress") ) ; } 51

Which one better, getobject() or getint()/getstring()/? getobject() handles null better: if primitive type (int/string ) you must use wasnull() method to check if the value is null IMMEDIATELTLY after return of value. It is easier to check for null objects! Moreover, if printing, when using getobject(), the string null will be printed. 52

PreparedStatements insated of Statements Advantages of PreparedStatements: Better performance: PreparedStatements are more efficient because the SQL is compile once Parameters are not passed as concatenated Strings, so eliminate the chance of SQL injection TODO: study SQL injection, if you don t know about it. 53

PreparedStatements Use SQL text with character? for parameters parameters can be cleared using clearparameters() Parameters can be set using mutators such as setstring(), setint(),... Execution as usual is carried out via executeupdate() or executequery() 54

Example of constructing a PreparedStatement PreparedStatement pstmt = con.preparestatement( "INSERT INTO Customers " + "(CustomerFirstName, CustomerLastName, CustomerAddress) "+ "VALUES (?,?,?)") ; // parameters are denoted by? Count starts // from 1 NOT 0 55

Executing a PreparedStatement pstmt.clearparameters() ; pstmt.setstring(1, "Joan") ; pstmt.setstring(2, "D'Arc") ; // NOTE: no explicit quoting required pstmt.setstring(3, "Tower of London") ; count = pstmt.executeupdate() ; System.out.println ("\ninserted " + count + " record successfully\n") ; // don t forget to clearparameters() pstmt.clearparameters() ; pstmt.setstring(1, "John") ; pstmt.setstring(2, "D'Orc") ; pstmt.setstring(3, "Houses of...") ; count = pstmt.executeupdate() ; System.out.println ("\ninserted " + count + " 56 record successfully\n") ;