JDBC. Eseguire una query String query = "SELECT * FROM COFFEES"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while.

Slides:



Advertisements
Similar presentations
Uguaglianza equals() e clone(). Fondamenti di Java Uguali o identici?
Advertisements

Java Database Conectivity Julio M. Faerman. Arquitetura Java Database Connectivity # 2 AplicaçãoJDBC API Driver Manager JDBC Driver SGBDR AplicaçãoJDBC.
Clonazione La clonazione... Ovvero: come costruire una copia (probabilmente che ritorni true su equals?)
Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel.
7878 1,3 8 1,3 4 1,3 4,6 1,3 7,8 1,3 4,5 6 4,5 6 7,8 7 4,6 5,6 7,8 5,6 4 I cicli grigio e verdino sono semplici, quello bianco no Ogni operazione è assegnata.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Java II--Copyright © Tom Hunter. Chapter 18 JDBC.
Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino.
Database By: Greg 4/10/2017 Java 9 - GUI.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Helia / Martti Laiho, JDBC Java Database Connection.
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
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 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
Object-Oriented Enterprise Application Development Introduction to JDBC.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
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.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
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.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
JDBC By 朱志興. Four driver types of JDBC (1) JDBC-ODBC bridge plus ODBC driver: Java Database Client JDBC- ODBC bridge ODBC driver Database Server A B C.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
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.
Connecting to MySQL using Java By:. – Required to use Java.sql so that we can use Connection and Queries using strings. – Javax.swing.* needed for components.
Basics of JDBC.
JDBC (Java Database Connectivity)
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
SQL and Java The vision for Java is to be the concrete and nails that people use to build this incredible network system that is happening all around us.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC Java Data Base Connectivity נערך ע"י: אורי רוטנברג הנחיה: ד"ר תמר בניה קורס: סדנא ב-Java.
1 JDBC – Java Database Connectivity THETOPPERSWAY.COM.
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
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Database Management Systems
Objectives In this lesson, you will learn about:
null, true, and false are also reserved.
Interacting with Database
Using a Database with JDBC
JDBC Example.
Lecture 11 Database Connection
Clonazione La clonazione... Ovvero: come costruire una copia
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC

Eseguire una query String query = "SELECT * FROM COFFEES"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String s = rs.getString("COF_NAME"); float n = rs.getFloat("PRICE"); System.out.println(s + " " + n); } rs.close(); stmt.close();

rs.next() sempre… query=“SELECT count(*) AS C”; rs=stmt.executeQuery(query); if (rs.next()) int m = rs.getString("C"); else System.out.println(“ahhhhhhh”); }

Ogni operazione sui dati va incluso in un blocco try catch per gestire eventuali eccezioni try { … } catch(SQLException se) { //fai qualcosa } catch(Exception e) { //fai qualcos’altro }

Eseguire un update

Transazioni in JDBC Scelta della modalità delle transazioni: un metodo definito nell'interfaccia Connection : setAutoCommit(boolean autoCommit) con.setAutoCommit(true) – (default) "autocommit": ogni operazione è una transazione con.setAutoCommit(false) – gestione delle transazioni da programma con.commit() con.rollback() – non c'è start transaction 6

try { con.setAutoCommit(false); Statement st=con.createStatement(); st.executeUpdate(“…”); … con.commit(); } catch (Exception ex) { try { con.rollback();} catch (SQLException sqx) } Transazioni in JDBC

CONNECTION POOL import java.sql.*; public class MyConnectionPool { // array di connessioni al database Connection con[]; // array delle disponibilità delle connessioni boolean busy[]; // registra chi sta tenendo occupata la connessione String who[]; // numero di connessioni attive int numCon; // incremento dimensione del pool per accogliere nuove richieste int inc;

// parametri di accesso al database String dbUrl; String dbUsername; String dbPswd; String driverString; /** Costruttore */ public MyConnectionPool (String dbUrl, String dbUsername, String dbPswd, int numCon, int inc, String driverString ) throws Exception { this.dbUrl = dbUrl; this.dbUsername= dbUsername; this.dbPswd = dbPswd; this.numCon = numCon; this.inc = inc; this.driverString = driverString; newConnections(); }

/** * newConnections */ private synchronized void newConnections() throws Exception { // alloca gli array globali (connessioni e info) con = new Connection[numCon]; busy = new boolean[numCon]; who = new String[numCon]; Class.forName(driverString); for (int i = 0; i < numCon; i++) { con[i] = DriverManager.getConnection(dbUrl,dbUsername,dbPswd); busy[i] = false; who[i] = ""; } }

/** * extendConnections */ public synchronized void extendConnections() throws Exception { // copia dei vecchi vettori Connection con2[] = con; boolean busy2[] = busy; String who2[] = who; // creazione dei nuovi vettori estesi con = new Connection[numCon+inc]; busy = new boolean[numCon+inc]; who = new String[numCon+inc];

//ciclo per trasferire le vecchie connessioni nei nuovi array for (int i = 0; i < numCon; i++) { con[i] = con2[i]; busy[i] = busy2[i]; who[i] = who2[i]; } //ciclo per creare le nuove connessioni da aggiungere alle precedenti for (int i = numCon; i < numCon+inc; i++) { con[i] = DriverManager.getConnection(dbUrl,dbUsername,dbPswd); busy[i] = false; who[i] = ""; } numCon += inc; }

/** getConnection - assegna una connessione all’utente who */ public synchronized Connection getConnection(String who) throws Exception { int indFree = findFreeConnection(); if (indFree < 0) { // se arriva qui, il pool è saturo: lo estende e // richiama ancora findFreeConn…() extendConnections(); indFree = findFreeConnection(); if( indFree < 0 ) return null; // se arriva qui non ci sono proprio più risorse } // salvo catastrofi verrà sempre eseguito questo codice busy[indFree] = true; who[indFree] = who; return con[indFree]; }

/** getConnection */ public synchronized Connection getConnection() throws Exception { return getConnection(“noName”); } /** * releaseConnection - la connessione viene solo “liberata” */ public synchronized void releaseConnection(Connection c) { for (int i = 0; i < numCon; i++) { if (con[i] == c) { who[i] = ""; busy[i] = false; } } }

/** findFreeConnection - scandisce il vettore e restituisce l’indice */ protected int findFreeConnection() { for(int i = 0; i < numCon; i++) if ( ! busy[i] ) return i; return -1; } /** printStatusConnection */ public String printStatusConnection() { String result = ""; for (int i = 0; i < numCon; i++) result += "Conn. " + i + ": " + busy[i] + " used by: " + who[i]; return result; } } // chiude la classe