Advertisement
Guest User

GestioneDB

a guest
Jun 13th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package DataBase;
  2.  
  3. /**
  4.  *
  5.  * Classe per la connessione al Database.
  6.  * Viene utilizzato il pattern SINGLETON --> unica connessione fino alla chiusura del programma
  7.  *
  8.  * @author Summo Pierluigi
  9.  *
  10.  * **/
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15.  
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.sql.Statement;
  22.  
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Properties;
  26.  
  27. import javax.sql.PooledConnection;
  28. import javax.swing.JFrame;
  29. import javax.swing.JOptionPane;
  30.  
  31. /**
  32.  *
  33.  * Classe contentente metodi inerenti al Database
  34.  *
  35.  * */
  36.  
  37. public final class ConnessioneDatabase {
  38.  
  39.     /**
  40.      * conn permette la connessione al database
  41.      *
  42.      * JDBC acquisisce il driver utile per la connessione
  43.      *
  44.      * DB_URL è il link al database
  45.      * **/
  46.      protected Connection conn = null;
  47.      protected static final ConnessioneDatabase connection = new ConnessioneDatabase();
  48.  
  49.      protected static final String JDBC = "com.mysql.jdbc.Driver";
  50.      protected static final String DB_URL = "jdbc:mysql://localhost/gestione_anagrafica";
  51.      
  52.     /*
  53.      *
  54.      * Connessione al Database tramite Driver JDBC
  55.      *
  56.      * @param DB_URL --> Stringa contenente l'URL del Database
  57.      * @param conn --> Connection:  Classe per effettuare la connessione
  58.      *
  59.      */
  60.     protected static void connessione(){
  61.         Properties properties = new Properties();
  62.         InputStream input = null;
  63.  
  64.         try {
  65.             input = new FileInputStream("config.properties");
  66.         } catch (FileNotFoundException e1) {
  67.             JOptionPane.showMessageDialog(new JFrame(),"Impossibile accedere al database, dati accesso non trovati", "Attenzione",JOptionPane.ERROR_MESSAGE);
  68.        
  69.         }
  70.  
  71.         // load a properties file
  72.         try {
  73.             properties.load(input);
  74.         } catch (IOException e1) {
  75.             JOptionPane.showMessageDialog(new JFrame(),"Impossibile accedere al database, dati accesso non trovati", "Attenzione",JOptionPane.ERROR_MESSAGE);
  76.  
  77.         }
  78.         String username = properties.getProperty("dbuser");
  79.         String password = properties.getProperty("dbpassword");
  80.  
  81.         try {
  82.             Class.forName(JDBC);
  83.             connection.conn = DriverManager.getConnection( DB_URL, username,password);
  84.         }
  85.         catch(ClassNotFoundException e){
  86.             JOptionPane.showMessageDialog(new JFrame(),
  87.                     "Impossibili accedere al database, Driver non trovati", "Attenzione",JOptionPane.WARNING_MESSAGE);
  88.                 }
  89.         catch(SQLException e){
  90.  
  91.             JOptionPane.showMessageDialog(null,"Errore nella connessione al database! Contattare l'amministratore di sistema. Il sistema verrà chiuso ora.");
  92.         }
  93.                
  94.     }
  95.  
  96.  
  97.     /**
  98.      *
  99.      * Questo metodo permette di verificare se  è già presente una connessione aperta
  100.      * ed in caso utilizzare la stessa
  101.      *
  102.      * Pattern Singleton
  103.      *
  104.      * **/
  105.  
  106.     public static Connection getInstance(){
  107.        
  108.         if (connection.conn == null) {
  109.             connessione();
  110.         }
  111.         return connection.conn;
  112.     }
  113.    
  114.    
  115.     /**
  116.      *
  117.      * Esegue le query di Update o insert
  118.      * @param query
  119.      * @param col
  120.      * @param testo
  121.      */
  122.     public static boolean eseguiQuery(String query, String testo, String col) {
  123.         boolean flag = false;
  124.         try {
  125.             PreparedStatement statExecute = getInstance().prepareStatement(query);
  126.             statExecute.setString(1, col);
  127.             statExecute.executeUpdate();
  128.             flag = true;
  129.             } catch (SQLException e) {
  130.                
  131.             flag = false;
  132.         }
  133.        
  134.         return flag;
  135.     }
  136.  
  137.  
  138.     /**
  139.      * Importa i commenti, è richiamato dalla classe --> ManageDatabase
  140.      *
  141.      * @param query
  142.      * @param riga
  143.      */
  144.     public static String importaDatiCommento(String query, String riga) {
  145.        
  146.         ConnessioneDatabase.getInstance();
  147.        
  148.         ResultSet resCommento = null;
  149.         String commento = "";
  150.         try {
  151.             PreparedStatement statKeep = getInstance().prepareStatement(query);
  152.             statKeep.setString(1, riga);
  153.             resCommento = statKeep.executeQuery();
  154.             while(resCommento.next()){
  155.                 commento = resCommento.getString(1);
  156.             }
  157.         } catch (SQLException e) {
  158.            
  159.             JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione del testo", "Attenzione",JOptionPane.WARNING_MESSAGE);     
  160.  
  161.         }
  162.        
  163.         return commento;
  164.     }
  165.  
  166.  
  167.     /**
  168.      *
  169.      * Server per acquisire il nome delle colonne di una tabella dal database
  170.      *
  171.      * @param tableName
  172.      * @return
  173.      */
  174.     public static List<String> acquisisciNomeColonne(String tableName) {
  175.  
  176.         ConnessioneDatabase.getInstance();
  177.         Statement statKeepColumn = null;
  178.         ResultSet resColumn = null;
  179.         List<String> columnName = new ArrayList<String>(10);
  180.         try {
  181.             statKeepColumn = getInstance().createStatement();
  182.             resColumn = statKeepColumn.executeQuery(String.format("DESCRIBE %1$S", tableName));
  183.             while(resColumn.next()){
  184.                 columnName.add(resColumn.getString(1));
  185.             }
  186.  
  187.         } catch (SQLException e) {
  188.             JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione del nome delle colonne", "Attenzione",JOptionPane.WARNING_MESSAGE);    
  189.         }
  190.         return columnName;
  191.     }
  192.  
  193.  
  194.     /**
  195.      *
  196.      * Acquisisce i dati per le tabelle
  197.      *
  198.      * @param query
  199.      * @param nUMCOLDIP
  200.      * @return
  201.      */
  202.     public static List<String> acquisisciDatiTabella(String query, List<Integer> nUMCOLDIP) {
  203.        
  204.         ConnessioneDatabase.getInstance();
  205.         List<String> columnData = new ArrayList<String>();
  206.         int ripetizioni = nUMCOLDIP.size();
  207.        
  208.         try {
  209.             Statement statKeepData = getInstance().createStatement();
  210.             ResultSet resData = statKeepData.executeQuery(query);
  211.             while(resData.next()){
  212.                 for(int i = 0; i < ripetizioni; i++){
  213.                     columnData.add(resData.getString(nUMCOLDIP.get(i)));
  214.                 }
  215.             }
  216.         } catch (SQLException e) {
  217.             JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione dei dati", "Attenzione",JOptionPane.WARNING_MESSAGE);      
  218.  
  219.         }
  220.  
  221.         return columnData;
  222.     }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement