PB: JDBC Connection

From: Alfonso brugnoli <alfonso.brugnoli_at_tin.it>
Date: Mon, 22 Jan 2001 15:37:21 GMT
Message-ID: <RsYa6.13729$dD2.159470_at_twister2.tin.it>


[Quoted] The program do not view the DB ORACLE 8i Lite POLITE ! But it is stored into [Quoted] ODBC - 32 Administrator !?!

Hi , I have a trouble with this JAVA class :



********file database.java************

import java.sql.*;
import java.io.*;
// ************************************************************/
// * Definisco la classe  Database  che accede al Database e  */
// * mi esegue le operazioni di accesso, connessione, query e */
// * update                                                   */
// ************************************************************/

class database {
  Connection dbconn;
  //Il COSTRUTTORE DI CLASSE mi esegue la connessione al database   public database(String URL, String userID, String password) {     try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}

    catch (ClassNotFoundException ex2) {

      System.err.println("Fallita connessione al database. Errore 1");
      System.exit(-1);

}

    catch (Exception ex) {
      System.err.println("Fallita connessione al database. Errore 2");
      ex.printStackTrace();
      System.exit(-1);

}

    try {
      dbconn = DriverManager.getConnection(URL, userID, password);
}

    catch (SQLException e) {
      System.err.println("Fallita connessione al database.. Errore 3");
      System.err.println("La fonte ODBC non esiste.");
      System.exit(-1);

}

    catch (Exception ex) {
      System.err.println("Fallita connessione al database. Errore 4");
      ex.printStackTrace();
      System.exit(-1);

}

  }

  //Il metodo ESEGUI_QUERY esegue un comando SQL (select) sul database [Quoted]   //sottostante e ritorna un Result_Set al chiamante;   public synchronized ResultSet Esegui_Query(String stringa_SQL) {     Statement statement;
    ResultSet result_set = null;
    ResultSetMetaData meta_data;
    try {

      statement = dbconn.createStatement();
      result_set = statement.executeQuery(stringa_SQL);
      statement.close();  //NON USARE    FORSE C'E' UN BUG ??

}

    catch (SQLException e) {
      System.err.println(e.getMessage());
      System.err.println(e.getErrorCode());
      System.exit(1);

   }
 return result_set;
 }

[Quoted]   //Il metodo STRUTTURA_QUERY esegue un comando SQL (select) sul database [Quoted]   //sottostante e ritorna la struttura del Result_Set al chiamante;   public synchronized ResultSetMetaData Struttura_Query(String stringa_SQL) {

    Statement statement;
    ResultSet result_set = null;
    ResultSetMetaData meta_data = null;
    try {

      statement = dbconn.createStatement();
      result_set = statement.executeQuery(stringa_SQL);
[Quoted]       meta_data = result_set.getMetaData();
      statement.close();  //NON USARE    FORSE C'E' UN BUG ??

}

    catch (SQLException e) {
      System.err.println(e.getMessage());
      System.err.println(e.getErrorCode());
      System.exit(1);

   }
 return meta_data;
 }
[Quoted]   //Il metodo ESEGUI_UPDATE esegue un comando SQL (Update o Delete) sul database
[Quoted]   //sottostante e ritorna il numero di righe modificate o cancellate al chiamante;
[Quoted]   public synchronized int Esegui_Update (String stringa_SQL) {     Statement statement;
    int NumRighe = 0;
    try {
      statement = dbconn.createStatement();
      NumRighe = statement.executeUpdate(stringa_SQL);
      statement.close();

}

    catch (SQLException e) {
      System.err.println(e.getMessage());
      System.err.println(e.getErrorCode());

}

 return NumRighe;
  }

  //Il metodo DBchiudi esegue la commit e chiude la connessione al Database   //In questo esempio non viene usato
  public void dbchiudi() {
    try {

      dbconn.commit();
      dbconn.close();

}

    catch (SQLException e) {
      System.err.println(e.getMessage());
      System.err.println(e.getErrorCode());

}

  }  

}

The Main that presents an test is :



********file Start.java************

import java.io.*;
import java.sql.*;
import database;

public class Start
{
  /*

    [Quoted]
  • Main dell'applicazione */ public static void main(String str[]) throws Exception { String URL="jdbc:polite:POLite"; String userID="SCOTT"; String password="TIGER"; // Creo il canale al DB POLITE database MioDB = new database(URL, userID, password); // *** QUERY // Query di creazione String Query1 = "CREATE TABLE Anagrafica"+ "(Nome VARCHAR(32)," + " Cognome VARCHAR(32))"; // Query di inserimento String Query2 = "INSERT INTO Anagrafica"+ "(Nome, Cognome)" + " VALUES ('Cecco','Angiolieri')"; // Query di Selezione String Query3 = "SELECT Nome,Cognome"+ "FROM Anagrafica";

// Eseguo la Query1, Risultato controlla se op รจ andata a buon fine

   int Risultato;
   Risultato = MioDB.Esegui_Update(Query1);

// Eseguo la Query2

   Risultato = MioDB.Esegui_Update(Query2);

// Creo un ResultSet per prendere il contenuto di una selezione // Eseguo la Query3

   ResultSet rs = MioDB.Esegui_Query(Query3);

// Stampo il risultato

   while (rs.next())
   {
    System.out.println(rs.getString("Nome")+" "+rs.getString("Cognome"));    }
  }// fine
}

HELP ME PLEASE !

                                        Paolo Brugnoli
Received on Mon Jan 22 2001 - 16:37:21 CET

Original text of this message