Framing. Accessing DBMS from your code. A possible way of writing applications accessing DBMS. ODBC: Open Data Base Connectivity

Similar documents
Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

The JAVA Way: JDBC and SQLJ

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Self-test Database application programming with JDBC

JDBC (Java / SQL Programming) CS 377: Database Systems

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Database Application Development. Overview. SQL in Application Code. Chapter 6

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

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

CS/CE 2336 Computer Science II

CS346: Database Programming.

Applets, RMI, JDBC Exam Review

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Package sjdbc. R topics documented: February 20, 2015

Java and RDBMS. Married with issues. Database constraints

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

SQL and programming languages

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Java Server Pages and Java Beans

Java and Microsoft Access SQL Tutorial

Database Access Through Java Technologies

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

CSI 2132 Lab 8. Outline. Web Programming JSP 23/03/2012

LSINF1124 Projet de programmation

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

More SQL: Assertions, Views, and Programming Techniques

Using JML to protect Java code against SQL injection. Johan Janssen June 26, 2007

Java DataBase Connectivity (JDBC)

Security Module: SQL Injection

Introduction Web Portal Main Page Group Management Create group Modify Group Member List... 5

Statement Level Interface. Call Level Interface. Static SQL. Status. Connections. Transactions. To connect to an SQL database, use a connect statement

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

1 SQL Data Types and Schemas

FileMaker 14. ODBC and JDBC Guide

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Using Netbeans and the Derby Database for Projects Contents

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Database Migration from MySQL to RDM Server

Java MySQL Connector & Connection Pool

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

How to Improve Database Connectivity With the Data Tools Platform. John Graham (Sybase Data Tooling) Brian Payton (IBM Information Management)

FileMaker 8. Installing FileMaker 8 ODBC and JDBC Client Drivers

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

MAKING ORACLE AND SQLJ WORK FOR YOU John Jay King, King Training Resources

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Introduction to Triggers using SQL

Specialized Programme on Web Application Development using Open Source Tools

Querying Databases Using the DB Query and JDBC Query Nodes

Apéndice C: Código Fuente del Programa DBConnection.java

FileMaker 12. ODBC and JDBC Guide

Enterprise Java. Where, How, When (and When Not) to Apply Java in Client/Server Business Environments. Jeffrey Savit Sean Wilcox Bhuvana Jayaraman

Accesssing External Databases From ILE RPG (with help from Java)

Configuring Apache Derby for Performance and Durability Olav Sandstå

HP Vertica Integration with SAP Business Objects: Tips and Techniques. HP Vertica Analytic Database

1.264 Lecture 15. SQL transactions, security, indexes

Comparing the Effectiveness of Penetration Testing and Static Code Analysis

Hive Interview Questions

FileMaker 11. ODBC and JDBC Guide

Database Programming with PL/SQL: Learning Objectives

Brazil + JDBC Juin 2001, douin@cnam.fr

FileMaker 13. ODBC and JDBC Guide

Microsoft SQL Server Features that can be used with the IBM i

Specialized Programme on Web Application Development using Open Source Tools

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Exploiting Java Technology with the SAS Software Barbara Walters, SAS Institute Inc., Cary, NC

Week 5: Embedded SQL. Embedded SQL 4. Application Program. Interactive vs. Non-Interactive SQL. Update Statements

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Oracle to MySQL Migration

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Strategic Procurement: The SAS Solution for Supplier Relationship Management Fritz Lehman, SAS Institute Inc., Cary, NC

Product Overview and Examples

Informatica Data Replication FAQs

Part IV: Java Database Programming

Building Web Applications, Servlets, JSP and JDBC

Java Application Developer Certificate Program Competencies

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Chapter 2 Database System Concepts and Architecture

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Architecting the Future of Big Data

SpagoBI exo Tomcat Installation Manual

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

A Generic Database Web Service

Database Toolkit: Portable and Cost Effective Software

Spring,2015. Apache Hive BY NATIA MAMAIASHVILI, LASHA AMASHUKELI & ALEKO CHAKHVASHVILI SUPERVAIZOR: PROF. NODAR MOMTSELIDZE

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Transcription:

JDBC

Framing Accessing DBMS from your code A possible way of writing applications accessing DBMS You ll see other ways via Web technologies ODBC: Open Data Base Connectivity C/C++ API JDBC: Java Data Base Connectivity Java API Both define interfaces, i.e. Set of methods you can use to do SQL queries (both DDL and DML) 2

JDBC Architecture 3

Advantages Separate your code from the native language of the DBMS You can use JDBC with any DBMS that provides a driver (or for which a bridge is available, see later) Not necessarily a relational DBMS But JDBC always accepts SQL-like statements! You can re-use your code with any DBMS Provided you have the relative driver 4

Lab setup PostgreSQL provides native Java drivers Postgresql JDBC library must be included in NetBeans projects project s properties libraries run and/or compile add postgresql JDBC driver 8

Typical actions within the code Load the DBMS driver Create a Connection with the DBMS Any number of times Submit SQL statements Retrieve results Process data/errors Commit or rollback Close the connection 9

Code spots Using (at least) one java package to interact with the JDBC driver import java.sql.*; in your import list javax.sql: advanced feature (we will not cover them) Load the driver Class.forName( org.postgresql.driver ); Catch a ClassNotFound exception!!! Connect to the DBMS String url= jdbc:postrgesql://delmastrof.iit.cnr.it/u01db ; Connection con = DriverManager.getConnection(url, u01, m-s-t+20+12 ); user password 10

(A bit more) General format of the URL string Connect to the DBMS String url= jdbc:postrgesql://delmastrof.iit.cnr.it/u01db ; Connection con = DriverManager.getConnection(url, u01, m-s-t+20+12 ); 11

Submit SQL statements DML Statement st = con.createstatement(); ResultSet rs = st.executequery( SELECT * FROM net WHERE gw= 192.168.2.1 ); Or, you can use PreparedStatements: DDL PreparedStatement pst = con.preparestatement( S E L E C T * F R O M n e t W H E R E a d m i n =? ) ; pst.clearparameters(); pst.setstring(1, gianni ); ResultSet rs = pst.executequery(); Statement st = con.createstatement( INSERT INTO net VALUES ( andrea, 192.168.5.1, 192.168.5.0/24 ) ); int numrows = st.executeupdate(); 12

Getting results back DDL statements return the number of rows modified Statement st = con.createstatement( INSERT INTO net VALUES ( andrea, 192.168.5.1, 192.168.5.0/24 ) ); int numrows = st.executeupdate(); DML statements return a ResultSet (an iterator) ResultSet rs = st.executequery(query); while(rs.next()){ String ad = rs.getstring( admin ); // or, String ad = rs.getstring(1); if admin is the first // field of the table } Move in the ResultSet with methods previous(), absolute(int), relative(int), first(), last() 13

ResultSet and Statements A ResultSet is associated with the previous statement Don t issue another query via the same statement before getting back all results: WRONG ResultSet rs = st.executequery(query); while(rs.next()){ S t r i n g a d = r s. g e t S t r i n g ( a d m i n ) ; ResultSet rs1 = st.executequery(query1); } CORRECT ResultSet rs = st.executequery(query); Statement st1 = con.createstatement(); while(rs.next()){ S t r i n g a d = r s. g e t S t r i n g ( a d m i n ) ; ResultSet rs1 = st1.executequery(query1); } 14

Data types Most are already defined by Java integer, String, Other are defined in the java.sql package java.sql.date, java.sql.time, java.sql.timestamp, Other within the driver package Org.postgresql.geometric.PGcircle, Org.postgresql.geometric.PGline, See http://jdbc.postgresql.org/documentation/publicapi/ index.html 15

Transactions By default, each statement is a transaction To alter this, Connection con = ; con.setautocommit(false); // con.getautocommit() to retrieve the current commit mode con.commit(); to close a transaction explicitly 16

Exceptions Methods throw SQLException getsqlstate() return a SQL state identifier geterrorcode() return a vendor-specific error code So, typically try{ Statement st = con.createstatement(); ResultSet rs = st.executequery(query); while(rs.next()){ // process results } } catch SQLException(e){ // process exception here } 17

Getting metadata Use ResultSetMetaData type to get metadata about a ResultSet Information such as the name and type of fields Have a look at host.java In general, use the Java API specs for documentation http://java.sun.com/j2se/1.4.2/docs/api/index.html 18

Secure connections String url = jdbc:postrgesql://delmastrof.iit.cnr.it/u01db? user=u01&password=m-s-t+20+12&ssl=true ; Connection con = DriverManager.getConnection(url); 19

Secure connections String url = jdbc:postrgesql://delmastrof.iit.cnr.it/u01db? user=u01&password=m-s-t+20+12&ssl=true& sslfactory=org.postgresql.ssl.nonvalidatingfactory ; Connection con = DriverManager.getConnection(url); String url = "jdbc:postgresql://delmastrof.iit.cnr.it/u01db ; Properties props = new Properties(); props.setproperty("user", u01"); props.setproperty("password", m-s-t+20+12"); props.setproperty("ssl","true"); props.setproperty("sslfactory", "org.postgresql.ssl.nonvalidatingfactory"); // no cert. validation Connection conn = DriverManager.getConnection(url, props); See http://jdbc.postgresql.org/documentation/84/connect.html http://jdbc.postgresql.org/documentation/84/ssl-client.html 20

Useful examples provafile.java Managing I/O from/to file provaio.java Managing input from keyboard provainet.java Managing network data types 21

NetBeans working directory header of any relative path in your source code 22