Sei sulla pagina 1di 33

Elena Comberlato

Matr. 093102

Base di Dati per la Gestione informativa


di un Consorzio di Allevamento di Bestiame
ed implementazione su Oracle con interfaccia JDBC

Introduzione

La Base di Dati riguarda un consorzio di allevamento di bestiame. Quest'ultimo utilizza la Base di Dati
per avere una panoramica generale dell’azienda per quanto riguarda l’aspetto dei suoi impiegati, i dati
riferiti agli allevamenti, le loro effettive quantità prodotte, il magazzino per il deposito del prodotto e le
vendite ai clienti.
Disponendo anche di strutture per il deposito del prodotto, le quali sono rappresentate come unita che
eseguono gli scambi delle merci.
I clienti che acquistano dal deposito, sono individuati nella Base di Dati mediante la partita IVA.

Raccolta Informazioni
Informazioni riguardanti il consorzio e la fornitura di carne ai clienti.

• La società dispone di un certo numero di allevamenti che hanno un nome distinto, un indirizzo, capienza
massima di bestie, dimensione (in metri quadrati).

• Ogni allevamento ha un gruppo di lavoratori i quali hanno una data inizio lavoro presso l'allevamento.
Gli allevatori sono di diverse qualifiche come: addetti alla stalla, addetti alla salute delle bestie, al
mangime, al pascolo etc.

• Gli impiegati dei magazzini si distinguono dall’attributo Ruolo. Abbiamo supposto che anche la
direzione fosse stabilita dentro i magazzini come le segretarie, i ragionieri etc.

• Gli impiegati hanno le seguenti informazioni: Codice Fiscale (che gli identifica), Nome, Cognome, Data
di Nascita, Anno assunzione, Stipendio, Sesso, Indirizzo, Numero di Telefono.

• La società ha un certo numero di magazzini i quali sono distinti attraverso un Codice identificativo
univoco. Ogni magazzino ha: Nome, Città, Indirizzo, Numero di Telefono, una capienza massima.

• Un allevamento fornisce a più magazzini, quest'ultimi possono essere forniti da diversi allevamenti.
NB: Non si può scaricare più carne dell’effettivo spazio in magazzino.

• Inoltre il consorzio fornisce i suoi prodotti al pubblico. Dei clienti si possiedono le seguenti
informazioni: nome, indirizzo, partita IVA, numero telefono.

• Per ogni cliente, viene registrata la quantità di carne per ogni tipo, il prezzo e la data dell'acquisto.
Progettazione Concettuale
Applicando un approccio di tipo BOTTOM-UP, TOP-DOWN(mista) si evidenziano delle prime entità
principali come:

• IMPIEGATO
• ALLEVAMENTO
• CARNE
• CLIENTE

Le entità rappresentano, attraverso la loro semantica, il generico impiegato, il generico allevamento, il


tipo di carne, il generico cliente con il qual è in rapporto la nostra azienda.

Queste entità vengono messe in relazione mediante le associazioni:

• LAVORA_SU mette in relazione IMPIEGATO con ALLEVAMENTO.


• MACELLA mette in relazione ALLEVAMENTO con CARNE.
• FORNITO_A mette in relazione CARNE con CLIENTE.

A questo punto una prima bozza è lo schema scheletro seguente .

• Primo raffinamento

Dalle specifiche del progetto, l’introduzione di una gerarchia di specializzazione che evidenzia i concetti:

• IMPIEGATO CHE LAVORA IN MAGAZZINO


• IMPIEGATO CHE LAVORA IN ALLEVAMENTO

Un' ulteriore struttura gerarchica e quella che distingue gli impiegati che lavorano nell'allevamento :

• VETERINARIO
• ALLEVATORE

La struttura gerarchica finale appare rappresentata in questo modo.


• Secondo raffinamento

In seguito nasce la necessità di introdurre il concetto di magazzino attraverso l’entità MAGAZZINO.


Entità la quale effettivamente esegue lo scambio della merce con l’esterno rappresentato dalle entità di
CLIENTE.

L’entità CARNE è un’entità debole la quale è messa in relazione con MAGAZZINO mediante
l’associazione SCARICA_IN. Risulta debole perchè non esiste un insieme di attributi chiave e si
presentato molte tuple ripetute.

• Terzo raffinamento

Si effettua un nuovo raffinamento quando si vuole esprimere (presente nelle specifiche del progetto) la
relazione che c’è tra magazzino e cliente, partendo dalla richiesta di rappresentare le quantità di carne
ritirate, la data del ritiro e il prezzo. Così si presenta lo schema dopo un successivo raffinamento.
FORNITURA_TIPO
MAGAZZINO

PRENDE

LOCALE
OFFRE

Alla fine introducendo anche gli attributi sulle entità e sulle associazioni avremo lo schema
finale ER Esteso:
Ristrutturazione
La fase di ristrutturazione dello schema E-R esteso consiste nella:

- Rimozione delle strutture di specializzazione / generalizzazione

Consiste nel rimuovere la struttura di specializzazione / generalizzazione che è creata nella posizione
dell’entità IMPIEGATO:

1. Eliminare le specializzazioni VETERINARIO e ALLEVATORE;


2. Eliminare la generalizzazione IMPIEGATO.

L’eliminazione delle specializzazioni non comporta l’introduzione di ridondanze per il fatto che soltanto
due attributi caratterizzano le particolari entità specializzanti quindi introduciamo soltanto un campo per il
quale possono verificarsi valori nulli.
L'eliminazione della generalizzazione IMPIEGATO inizialmente può sembrare sbagliata perché
ripetiamo gli stessi campi per due entità. Se facciamo riferimento alle esigenze del sistema informativo, si
vede che non sono frequenti le query sui dati degli impiegati essendo più legati alla parte finanziaria della
società. Ci verrà più utile avere dei dati separati per i due tipi d’impiegati.

- Rimozione delle entità deboli

Nel nostro schema abbiamo soltanto un’entità debole, l’entità CARNE. La rimozione dell’entità debole
consiste nell’accorpare all’insieme degli attributi anche gli attributi che costituiscono la chiave dell’entità
ALLEVAMENTO che è anche entità dominante. Così individuando come chiave dell’entità CARNE
l’insieme dei due attributi: NomeAllev, DataProd, TipoCarne.

- Rimozione degli attributi composti

Gli attributi composti:


• Per quanto riguarda l’indirizzo nell’entità IMPIEGATO, ho preferito inserire un nuovo attributo
semplice indirizzo che sarà una stringa che indicherà l’indirizzo, invece che introdurre degli altri
campi.

• Per quanto riguarda Dati_Allev nell’entità ALLEVAMENTO ho preferito inserire dei nuovi attributi
semplici che indicano i dati sullo specifico allevamento. Quest'ultimi Dati sono importanti per le
interrogazioni da parte dell’azienda.

Alla fine della ristrutturazione lo schema si presenta:


Progettazione Logica
La progettazione logica consiste nel mapping dallo schema E-R in quello relazione. Si illustrano soltanto
gli schemi a livello intensionale.

1- L’entità IMP_MAGAZZINO in relazione con l’entità MAGAZZINO viene tradotta nella


seguente relazione, avente tra i suoi attributi anche Cod_Mag (chiave primaria dell'entità MAGAZZINO)
che funge come chiave esterna. Questa è derivata dalla soluzione dell’associazione 1: N LAVORA_IN.
Codice Fiscale(CF) è chiave primaria della relazione IMP_MAGAZZINO:

• IMP_MAGAZZINO(Cod_Mag, CF, Nome, Cognome, NTel, Stipendio, Indirizzo, Ruolo, sesso,


Anno_Assunz);

• MAGAZZINO(Cod_Magazzino, Città, Nome, Tel, Capienza, Indirizzo, Mod_Pagamento)

2- L'entità IMP_ALLEV è in relazione con l'entità ALLEVAMENTO attraverso l’associazione


LAVORA_SU. Il problema dell’attributo d’associazione Data_Inizio (rappresenta la data d’inizio lavoro
nell'allevamento) è stato risolto introducendolo come attributo sulla relazione IMP_ALLEV perchè è
l'entità partecipante nel lato molti. Le relazioni finali sono:

• IMP_ALLEV(CF, Nome, Cognome, NTel, Stipendio, NomeAllev, Sesso, Indirizzo, Anno_assunz,


Tipo, Data_Inizio, Specializzazione, Professione );

•ALLEVAMENTO( Nome, Dim, Capienza_Max, Indirizzo).

3- Le entità CARNE e MAGAZZINO sono correlate dall’associazione SCARICA_IN, binaria di


tipo uno-a-molti. Vengono mappate nelle seguenti relazioni:

•CARNE(Quantità,TipoCarne,DataProd, NomeAllev, Cod_Magazzino).

4- L’entità FORNITURA_TIPO è stata tradotta come una relazione identificata esternamente dalle
chiavi di magazzino e cliente (PIVA e Cod_Magazzino). La relazione finale avrà come chiave gli
identificatori esterni insieme con TipoCarne e Data. Quest'ultimo attributo rappresenta quando è stata
effettuata la fornitura. Cosi si rappresenterà alla fine la relazione FORNITURA_TIPO :

•FORNITURA_TIPO(Data, TipoCarne, Prezzo, Quantità, P_IVA_Cliente, Cod_Magazzino)

5- L’altra entità in relazione con FORNITURA_TIPO è CLIENTE che eseguendo il mapping si


mostra così:

•CLIENTE(P-IVA, Nome, Tel, Indirizzo)

6- In fine rimane da mappare l’entità BESTIA. Quest'ultima è in associazione con allevamento di


tipo binaria molti-a-molti. La soluzione è di creare una nuova relazione ALLEVA che avrà nel suo
schema gli identificatori delle due entità partecipanti nell’associazione. La rappresentazione intensionale
sarà:

•ALLEVA(NomeAllev, TipoCarne, Stagione, Mangime)

7- Così sarà lo schema dell’entità BESTIA:

•BESTIA(TipoCarne, Stagione, Mangime )

Alla fine della progettazione logica lo schema si presenta:


Normalizzazione dello schema logico
• IMP_MAGAZZINO(Cod_Mag, CF, Nome, Cognome, NTel, Stipendio, Indirizzo, Ruolo, sesso,
Anno_Assunz);

2NF: SI perchè la chiave primaria è composta di un solo attributo (non possono esserci
dipendenze funzionali parziali).
3NF: NO perchè esiste la DF {Ruolo, Anno_Assunzione}→{Stipendio} che Stipendio è un attributo non-
primo e Ruolo, Anno_Assunzione non sono superchiave della relazione.

• IMP_ALLEV(CF, Nome, Cognome, NTel, Stipendio, NomeAllev, Sesso, Indirizzo, Anno_assunz,


Tipo, Data_Inizio, Specializzazione, Professione );

2NF: SI perchè la chiave primaria è composta di un solo attributo (non possono esserci
dipendenze funzionali parziali).
3NF: NO perchè esiste la DF {Professione, Anno_Assunzione}→{Stipendio} che Stipendio è un
attributo non-primo e Professione, Anno_Assunzione non sono una superchiave.

•ALLEVAMENTO( Nome, Dim, Capienza_Max, Indirizzo);

2NF: SI perchè la chiave primaria è composta di un solo attributo (non possono esserci
dipendenze funzionali parziali).
3NF: SI perchè tute le DF hanno la parte determinante una superchiave.

•CARNE(Quantità,TipoCarne,DataProd, NomeAllev, Cod_Magazzino).

2NF: SI, perchè non esistono DF parziali.


3NF: SI, perché tute le DF hanno la parte determinante una superchiave.

• MAGAZZINO(Cod_Magazzino, Città, Nome, Tel, Capienza, Indirizzo, Mod_Pagamento)

2NF: SI perchè la chiave primaria è composta di un solo attributo (non possono esserci
dipendenze funzionali parziali).
3NF: NO perché esiste la DF {Città, Nome} → {Indirizzo} ecc.

•FORNITURA_TIPO(Data, TipoCarne, Prezzo, Quantità, P_IVA_Cliente, Cod_Magazzino)

2NF: SI perchè non esistono DF parziali.


3NF: SI perché tute le DF hanno parte determinante una superchiave.

•CLIENTE(P-IVA, Nome, Tel, Indirizzo)

2NF: SI perchè la chiave primaria è composta di un solo attributo (non possono esserci
dipendenze funzionali parziali).
3NF: SI perché esiste l'insieme di attributi{Nome, Indirizzo} che è chiave candidata per la relazione.
Quindi tutte le DF hanno parte determinate una superchiave.

•BESTIA(TipoCarne, Stagione, Mangime )

2NF: SI perchè non esistono DF parziali.


3NF: SI perchè tutti gli attributi appartengono all'insieme che forma la chiave della relazione. Quindi
tutte le DF hanno parte determinate una superchiave.

•ALLEVA(NomeAllev, TipoCarne, Stagione, Mangime)

2NF: SI perchè non esistono DF parziali.


3NF: SI perchè tutti gli attributi appartengono all'insieme che forma la chiave della relazione. Quindi
tutte le DF hanno parte determinate una superchiave.

Progettazione fisica
La progettazione fisica della Base di Dati verrà scritta in Java attraverso il connettore per database JDBC,
che consente l'accesso alla base di dati in Oracle.
Segue il codice delle tabelle, compilato ed eseguito.

import java.sql.*;

public class CreateAlleva {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table ALLEVA " +
"(NOMEALLEV varchar(30), " +
"STAGIONE varchar(8), " +
"MANGIME varchar(12), "+
"TIPOCARNE varchar(20))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateAllevamento {


public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table ALLEVAMENTO " +
"(NOME varchar(16) PRIMARY KEY, " +
"CAPIENZA_MAX int, " +
"DIM int, " +
"INDIRIZZO varchar(30))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateBestia {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
// String url = "jdbc:oracle:thin:@//server.local:1521/prod";

Connection con;
String createString;
createString = "create table BESTIA " +
"(TIPOCARNE varchar(20), " +
"STAGIONE varchar(11), "+
"MANGIME varchar(12))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateCarne {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table CARNE " +
"(QUANTITA int, " +
"TIPOCARNE varchar(15), " +
"DATAPROD date, " +
"NOMEALLEV varchar(30), " +
"COD_MAGAZZINO char(5))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateCliente {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table CLIENTE " +
"(PIVA char(11) PRIMARY KEY, " +
"NOME varchar(15), " +
"TEL varchar(20), "+
"INDIRIZZO varchar(30))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateFortituraTipo {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table FORNITURATIPO " +
"(DATA date, " +
"TIPOCARNE varchar(20), " +
"PREZZO float, " +
"QUANTITA int, " +
"PIVA_CLIENTE char(11), "+
"COD_MAGAZZINO char(5))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;
public class CreateImpAllev {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table IMP_ALLEV " +
"(CF char(16) PRIMARY KEY, " + "NOME varchar(15), " + "COGNOME varchar(15) , "
+
"PROFESSIONE varchar(20), " + "SPECIALIZZAZIONE varchar(20), " +
"NTEL varchar(20), " + "STIPENDIO int, " + "NOMEALLEV varchar(30), " +
"INDIRIZZO varchar(30), " + "ANNO_ASSUNZ date, " +
"SESSO char(1), " + "DATA_INIZIO date)";

Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateImpMagazzino {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table IMP_MAGAZZINO " +
"(CF char(16) PRIMARY KEY, " + "COD_MAG char(5), " + "NOME varchar(15) NOT
NULL, " +
"COGNOME varchar(15) NOT NULL, " + "NTEL varchar(20) DEFAULT 'non
disponibile', " + "STIPENDIO int DEFAULT NULL, " +
"INDIRIZZO varchar(30), " + "SESSO char(1), " + "RUOLO varchar(30), " +
"ANNO_ASSUNZIONE date) ";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class CreateMagazzino {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String createString;
createString = "create table MAGAZZINO " +
"(COD_MAGAZZINO char(5) PRIMARY KEY, " +
"CITTA varchar(15), " +
"NOME varchar(15), " +
"TEL varchar(20), " +
"CAPIENZA int, " +
"INDIRIZZO varchar(20), " +
"MOD_PAGAMENTO varchar(10))";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate(createString);

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class InsertAlleva {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select NOMEALLEV, TIPOCARNE from ALLEVA";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();
stmt.executeUpdate("insert into ALLEVA " +
"values('Lallevamento spa', 'inverno', 'erba','manzo')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("NOME ALLEVAMENTO e TIPOCARNE:");


while (rs.next()) {
String f = rs.getString("NOMEALLEV");
String s = rs.getString("TIPOCARNE");

System.out.println(s + " " + f);


}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}

import java.sql.*;

public class InsertBestia {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select TIPOCARNE, STAGIONE from BESTIA";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into BESTIA " +


"values('manzo', 'inverno', 'erba')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("TIPOCARNE e STAGIONE:");
while (rs.next()) {
String s = rs.getString("TIPOCARNE");
String f = rs.getString("STAGIONE");
System.out.println(s + " " + f);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class InsertCarne{

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select TIPOCARNE, NOMEALLEV from CARNE";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into CARNE " +


"values(12, 'manzo', '08-set-2008', 'Lallevamento spa','mag01')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("NOME ALLEVAMENTO e TIPOCARNE:");


while (rs.next()) {
String f = rs.getString("NOMEALLEV");
String s = rs.getString("TIPOCARNE");

System.out.println(s + " " + f);


}

stmt.close();
con.close();

} catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}

}
}

import java.sql.*;

public class InsertFornituraTipo {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select DATA, TIPOCARNE, PIVA_CLIENTE from FORNITURATIPO";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into FORNITURATIPO " +


"values('16-mar-2006', 'manzo', 15, 150 , '9872766678', 'mag01')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("data tipo carne e partita iva del cliente:");


while (rs.next()) {
Date s = rs.getDate("DATA");
String t = rs.getString("TIPOCARNE");
String v = rs.getString("PIVA_CLIENTE");

System.out.println(s + " " + t + " " + v);


}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
import java.sql.*;

public class InsertImpAllev{

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select NOME, COGNOME, NOMEALLEV from IMP_ALLEV";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into IMP_ALLEV " +


"values('RSSCLD50R15H501Y', 'Mario', 'Rossi', 'allevatore', 'manzo',
'3473499137', 1000, 'Lallevamento spa', " +
" 'via Bologna 2', '01-gen-2001', 'M', '01-gen-2001')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("Nome cognome e allevamento dove lavora:");


while (rs.next()) {
String s = rs.getString("NOME");
String f = rs.getString("COGNOME");
String a = rs.getString("NOMEALLEV");
System.out.println(s + " " + f + " " + a);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class InsertImp_Magazzino {


public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select NOME, COGNOME, RUOLO from IMP_MAGAZZINO";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into IMP_MAGAZZINO " +


"values('cmblne84p48i531v', 'mag01', 'elena', 'comberlato', '3280716271',
1200, 'via venezia 25,malo,VI', 'F', 'magazziniere', '08-set-2009')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("nome cognome e ruolo:");


while (rs.next()) {
String s = rs.getString("NOME");
String t = rs.getString("COGNOME");
String v = rs.getString("RUOLO");

System.out.println(s + " " + t + " " + v);


}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class InsertMagazzino {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;
String query = "select NOME, MOD_PAGAMENTO from MAGAZZINO";

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

stmt.executeUpdate("insert into MAGAZZINO " +


"values('mag01', 'Ferrara', 'Cles carni', '0554976465', 789, 'via vincenzo
monti,8', 'contanti')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("Nome magazzino e modalita'di pagamento:");


while (rs.next()) {
String s = rs.getString("NOME");
String f = rs.getString("MOD_PAGAMENTO");
System.out.println(s + " " + f);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}

import java.sql.*;

public class Join{

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
Connection con;
String query = "select ALLEVAMENTO.INDIRIZZO, CARNE.QUANTITA " +
"from ALLEVAMENTO, CARNE " +
"where ALLEVAMENTO.INDIRIZZO like 'via Bologna 11, Malo (VI)' and CARNE.NOMEALLEV
= ALLEVAMENTO.NOME ";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);
System.out.println(": ");
while (rs.next()) {
int n = rs.getInt("QUANTITA");
System.out.println("la quantita' di carne comprata dall'allevamento in via
Bologna a Malo è " + n);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}

import java.sql.*;

public class Join2 {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String query = "select IMP_MAGAZZINO.NOME " +
"from IMP_MAGAZZINO, MAGAZZINO " +
"where MAGAZZINO.CITTA like 'Ferrara' and " +
"MAGAZZINO.COD_MAGAZZINO = IMP_MAGAZZINO.COD_MAG";
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);
System.out.println(": ");
while (rs.next()) {
String nome = rs.getString("NOME");
System.out.println(" " + nome);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}

import java.sql.*;

public class Join3 {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
String query = "select ALLEVA.NOMEALLEV, BESTIA.MANGIME " +
"from BESTIA, ALLEVA " +
"where ALLEVA.NOMEALLEV like 'Lallevamento spa' " +
"and ALLEVA.TIPOCARNE = BESTIA.TIPOCARNE ";

Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);
System.out.println(": ");
while (rs.next()) {
String nome = rs.getString("MANGIME");
System.out.println(" " + nome);

stmt.close();
con.close();
}}

catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}

import java.sql.*;

public class ScrollableResultSet {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);

ResultSet srs = stmt.executeQuery("SELECT * FROM MAGAZZINO ");


srs.absolute(4);
int rowNum = srs.getRow(); // rowNum should be 4
System.out.println("rowNum should be 4 " + rowNum);
srs.relative(-3);
rowNum = srs.getRow(); // rowNum should be 1
System.out.println("rowNum should be 1 " + rowNum);
srs.relative(2);
rowNum = srs.getRow(); // rowNum should be 3
System.out.println("rowNum should be 3 " + rowNum);

srs.absolute(1);
System.out.println("after last? " + srs.isAfterLast() );
if (!srs.isAfterLast()) {
String name = srs.getString("NOME");
String city =srs.getString("CITTA");
System.out.println(name + " " + city);
}

srs.close();
stmt.close();
con.close();

} catch(BatchUpdateException b) {
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
System.out.println("");

} catch(SQLException ex) {
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}

import java.sql.*;

public class SetSavepoint1 {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {

con = DriverManager.getConnection(url,"SYSTEM","080984");

con.setAutoCommit(false);
//in una grande quantita' di un ordine abbassare il prezzo, selezionando il tipo di
carne
String query = "select CODMAGAZZINO from FORNITURATIPO " +
" where TIPOCARNE = ? ";
String update = "UPDATE FORNITURATIPO SET PREZZO = ? " +
" WHERE CODMAGAZZINO = ? ";

PreparedStatement getQuantita = con.prepareStatement(query);


PreparedStatement updateQuantita = con.prepareStatement(update);

getQuantita.setString(1 , "manzo");
//qui c'è l'errore
ResultSet rs = getQuantita.executeQuery();

Savepoint save1 = con.setSavepoint();

while (rs.next())
{
String codmagazzino = rs.getString("CODMAGAZZINO");
float oldPrice = rs.getFloat("PREZZO");
float newPrice = oldPrice + (oldPrice - 10);
updateQuantita.setFloat(1, newPrice);
updateQuantita.setString(2, codmagazzino);
updateQuantita.executeUpdate();
System.out.println("il nuovo prezzo dell'ordine al magazzino" + codmagazzino + "
e' " +
newPrice);
if (newPrice < 1000) {
con.rollback(save1);
}

con.commit();

Statement stmt = con.createStatement();


rs = stmt.executeQuery("SELECT CODMAGAZZINO, " +
"PREZZO FROM FORNITURATIPO");

System.out.println();
while (rs.next()) {
String carne = rs.getString("TIPOCARNE");
float price = rs.getFloat("PREZZO");
System.out.println("Current price of " + carne +
" is " + price);
}
con.commit();

con.close();

} catch (Exception e) {
e.printStackTrace();
}

}}

import java.sql.*;

public class TypeConcurrency {

public static void main(String args[]) {

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);

ResultSet srs = stmt.executeQuery("SELECT * FROM CLIENTE");

int type = srs.getType();

System.out.println("srs is type " + type);

int concur = srs.getConcurrency();

System.out.println("srs has concurrency " + concur);


while (srs.next()) {
String p_iva = srs.getString("PIVA");
String name = srs.getString("NOME");
String tel = srs.getString("TEL");
String indirizzo = srs.getString("INDIRIZZO");

System.out.println(p_iva + " " + name + " " + tel + " " + indirizzo);
}

srs.close();
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}

import java.sql.*;

public class BatchUpdate {

public static void main(String args[]) throws SQLException {

ResultSet rs = null;
PreparedStatement ps = null;

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

con.setAutoCommit(false);

stmt = con.createStatement();

stmt.addBatch("INSERT INTO CLIENTE " +


"VALUES(00846739270, 'CLES SNC', 0445602702, 'via Venezia 25,Malo (VI)')");
stmt.addBatch("INSERT INTO CLIENTE " +
"VALUES(09872766678, 'LA CARNE SPA', 0444556788 , 'via Ponte Vecchio
11,Vicenza')");

int [] updateCounts = stmt.executeBatch();


con.commit();
con.setAutoCommit(true);

ResultSet uprs = stmt.executeQuery("SELECT * FROM CLIENTE");


System.out.println("Table CLIENTE after insertion:");
while (uprs.next()) {
String name = uprs.getString("NOME");
String p_iva = uprs.getString("PIVA");
System.out.println(name + " " + p_iva);
}

uprs.close();
stmt.close();
con.close();

} catch(BatchUpdateException b) {
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
System.err.println("");

} catch(SQLException ex) {
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}

import java.sql.*;

public class BatchUpdateAllevamento {

public static void main(String args[]) throws SQLException {

ResultSet rs = null;
PreparedStatement ps = null;

String url = "jdbc:oracle:oci8:@";


try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}

Connection con;
Statement stmt;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,"SYSTEM","080984");

con.setAutoCommit(false);

stmt = con.createStatement();

stmt.addBatch("INSERT INTO ALLEVAMENTO " +


"VALUES('Lallevamento spa', 700, 25 , 'via Bologna 11, Malo (VI)')");

int [] updateCounts = stmt.executeBatch();


con.commit();
con.setAutoCommit(true);

ResultSet uprs = stmt.executeQuery("SELECT * FROM ALLEVAMENTO");

System.out.println("Table ALLEVAMENTO after insertion:");


while (uprs.next()) {
String name = uprs.getString("NOME");
String ind = uprs.getString("INDIRIZZO");
System.out.println(name + " " + ind);
}

uprs.close();
stmt.close();
con.close();

} catch(BatchUpdateException b) {
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
System.err.println("");

} catch(SQLException ex) {
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}

Potrebbero piacerti anche