Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

DBCP Jakarta Project...

Thread view: 
gbattine - 04 Jul 2006 17:16 GMT
Hi guys,
i need a disperate help,because i'm crazing with this problem.
I'm developing a jsf application with tomcat server and i have to
develop interactions beetwen the application and a mysql db.
I've done it with jdbc 1.0, now i've to use jdbc 2.'0 and datasource,so
i've thinked to use Jakarta DBCP project.
I've read documentation but i'm not able to go on.
Can someone help me?
Can you post me a class that care connession with mysql db?

This is my code...

package giu;
/*
* Classe dedicata alla gestione del Database.
* Gestisce l'apertura e la chiusura della connessione col Database
* Fornisce i metodi per l'esecuzione delle query sul Database
*/
import java.sql.*;
import java.util.Vector;

public class Database {
  private String nomeDB;       // Nome del Database a cui connettersi
  private String nomeUtente;   // Nome utente utilizzato per la
connessione al Database
  private String pwdUtente;    // Password usata per la connessione al

Database
  private String errore;       // Raccoglie informazioni riguardo
l'ultima eccezione sollevata
  private Connection db;       // La connessione col Database
  private boolean connesso;    // Flag che indica se la connessione è

attiva o meno

  public Database(String nomeDB) { this(nomeDB, "", ""); }

  public Database(String nomeDB, String nomeUtente, String pwdUtente)
{
     this.nomeDB = nomeDB;
     this.nomeUtente = nomeUtente;
     this.pwdUtente = pwdUtente;
     connesso = false;
     errore = "";
  }

  // Apre la connessione con il Database
  public boolean connetti() {
     connesso = false;
     try {

        // Carico il driver JDBC per la connessione con il database
MySQL
        Class.forName("com.mysql.jdbc.Driver");

        // Controllo che il nome del Database non sia nulla
        if (!nomeDB.equals("")) {

           // Controllo se il nome utente va usato o meno per la
connessione
           if (nomeUtente.equals("")) {

              // La connessione non richiede nome utente e password
              db =
DriverManager.getConnection("jdbc:mysql://localhost/" + nomeDB);
           } else {

              // La connessione richiede nome utente, controllo se
necessita anche della password
              if (pwdUtente.equals("")) {

                 // La connessione non necessita di password
                 db =
DriverManager.getConnection("jdbc:mysql://localhost/" + nomeDB +
"?user=" + nomeUtente);
              } else {

                 // La connessione necessita della password
                 db =
DriverManager.getConnection("jdbc:mysql://localhost/" + nomeDB +
"?user=" + nomeUtente + "&password=" + pwdUtente);
              }
           }

           // La connessione è avvenuta con successo
           connesso = true;
        } else {
           System.out.println("Manca il nome del database!!");
           System.out.println("Scrivere il nome del database da
utilizzare all'interno del file \"config.xml\"");
           System.exit(0);
        }
     } catch (Exception e) { errore = e.getMessage(); }
     return connesso;
  }
  public boolean eseguiAggiornamento(String query) {
             int numero = 0;
             boolean risultato = false;
             try {
                Statement stmt = db.createStatement();
                numero = stmt.executeUpdate(query);
                risultato = true;
                stmt.close();
             } catch (Exception e) {
                e.printStackTrace();
                errore = e.getMessage();
                risultato = false;
             }
             return risultato;
          }
  public Vector eseguiQuery(String query) {
             Vector v = null;
             String [] record;
             int colonne = 0;
             try {
                Statement stmt = db.createStatement();     // Creo lo
Statement per l'esecuzione della query
                ResultSet rs = stmt.executeQuery(query);   // Ottengo
il
ResultSet dell'esecuzione della query
                v = new Vector();
                ResultSetMetaData rsmd = rs.getMetaData();
                colonne = rsmd.getColumnCount();

                while(rs.next()) {   // Creo il vettore risultato
scorrendo
tutto il ResultSet
                   record = new String[colonne];
                   for (int i=0; i<colonne; i++) record[i] =
rs.getString(i+1);
                   v.add( (String[]) record.clone() );
                }

                rs.close();     // Chiudo il ResultSet
                stmt.close();   // Chiudo lo Statement
             } catch (Exception e) { e.printStackTrace(); errore =
e.getMessage(); }

             return v;
          }

  // Chiude la connessione con il Database
  public void disconnetti() {
     try {
        db.close();
        connesso = false;
     } catch (Exception e) { e.printStackTrace(); }
  }

  public boolean isConnesso() { return connesso; }   // Ritorna TRUE
se la connessione con il Database è attiva
  public String getErrore() { return errore; }    // Ritorna il
messaggio d'errore dell'ultima eccezione sollevata
  public Connection getConnection() { return db; }
gbattine - 05 Jul 2006 16:39 GMT
Can someone help me?
Please...
gbattine ha scritto:

> Hi guys,
> i need a disperate help,because i'm crazing with this problem.
[quoted text clipped - 150 lines]
> messaggio d'errore dell'ultima eccezione sollevata
>    public Connection getConnection() { return db; }
none - 05 Jul 2006 21:53 GMT
why not set up a connection pool in tomcat? and let it do the work for you.

try this URL for config and code examples:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

Tim

> Can someone help me?
> Please...
[quoted text clipped - 154 lines]
>>messaggio d'errore dell'ultima eccezione sollevata
>>   public Connection getConnection() { return db; }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.