Re: Connect to local database

From: Alfonso brugnoli <alfonso.brugnoli_at_tin.it>
Date: Mon, 19 Feb 2001 21:16:53 GMT
Message-ID: <93gk6.48936$Uj1.433541_at_twister2.tin.it>


You can USE ODBC Administrator to create a alias !

IF alias is POLITE :

by JAVA :

// <URL-userID-password> Per accedere al DB Server Oracle Lite ALIFLY   String URL="jdbc:odbc:POLITE"; // Uniform releted Link

  String userID="PAOLO";           // UserId
  String password="KLACID";     // Password

  String Query1;
    //* Apertura del canale verso DB
  database MioDB = new database(URL, userID, password);

//------ Modello

  Query1 = "SELECT Descrizione FROM Modello";
//* conto il numero di tuple presenti in Modello
  NumDati=MioDB.Conta_Tuple(Query1);

//Title:      Demo Conto
//Version:    1.0
//Copyright:  Copyright (c) 2001
//Author:     Paolo Brugnoli
//Company:    Università la Sapienza
//Description:Classe database per apertura canale verso DB Oracle Lite 8i

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);
      // livello di isolamento
      dbconn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
    }
    catch (SQLException e) {
      System.err.println("Fallita connessione al database..");
      System.err.println("La fonte ODBC non esiste.");
      while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
      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
//sottostante e ritorna un Result_Set al chiamante;
  public ResultSet Esegui_Query(String stringa_SQL) {     Statement statement;
    ResultSet result_set = null;
    try {

      statement = dbconn.createStatement();
      result_set = statement.executeQuery(stringa_SQL);
    }
    catch (SQLException e) {
      while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
       System.exit(1);

   }
 return result_set;
 }
//Il metodo Conta_Tuple esegue un comando SQL (select) sul database
//sottostante e ritorna un int con numero tuple selezionate;
  public int Conta_Tuple(String stringa_SQL) {     Statement statement;
    int contatore = 0;

    ResultSet result_set = null;
    try {

      statement = dbconn.createStatement();
      result_set = statement.executeQuery(stringa_SQL);
      while (result_set.next())
          {
          contatore++;
          }

    }
    catch (SQLException e) {
      while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
       System.exit(1);

   }
 return contatore;
 }

//Il metodo STRUTTURA_QUERY esegue un comando SQL (select) sul database
//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);
      meta_data = result_set.getMetaData();
    }
    catch (SQLException e)
    {
      while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
       System.exit(1);

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

    }
    catch (SQLException e)
    {
       while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
       System.exit(1);

    }
 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) {
      while ( e != null )
      {
        System.err.println("STATO SQL : "+e.getSQLState());
        System.err.println("MESSAGGIO : "+e.getMessage());
        System.err.println("ERRORE    : "+e.getErrorCode());
        e = e.getNextException();
      }
      System.exit(1);

    }
  }
}

"Marco de Haan" <ams072_at_wirehub.nl> ha scritto nel messaggio news:981477974.13310_at_newsreader1.wirehub.nl... > How can I make TOAD connect to my local database. > I'm able to connect to remote databases via Internet at the office and to  my

> local database by using SCOTT and TIGER. Using the connect-string "2:"
> doesn't help either.
>
> Any suggestions?
>
>
Received on Mon Feb 19 2001 - 22:16:53 CET

Original text of this message