Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DataBase;
- /**
- *
- * Classe per la connessione al Database.
- * Viene utilizzato il pattern SINGLETON --> unica connessione fino alla chiusura del programma
- *
- * @author Summo Pierluigi
- *
- * **/
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import javax.sql.PooledConnection;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- /**
- *
- * Classe contentente metodi inerenti al Database
- *
- * */
- public final class ConnessioneDatabase {
- /**
- * conn permette la connessione al database
- *
- * JDBC acquisisce il driver utile per la connessione
- *
- * DB_URL è il link al database
- * **/
- protected Connection conn = null;
- protected static final ConnessioneDatabase connection = new ConnessioneDatabase();
- protected static final String JDBC = "com.mysql.jdbc.Driver";
- protected static final String DB_URL = "jdbc:mysql://localhost/gestione_anagrafica";
- /*
- *
- * Connessione al Database tramite Driver JDBC
- *
- * @param DB_URL --> Stringa contenente l'URL del Database
- * @param conn --> Connection: Classe per effettuare la connessione
- *
- */
- protected static void connessione(){
- Properties properties = new Properties();
- InputStream input = null;
- try {
- input = new FileInputStream("config.properties");
- } catch (FileNotFoundException e1) {
- JOptionPane.showMessageDialog(new JFrame(),"Impossibile accedere al database, dati accesso non trovati", "Attenzione",JOptionPane.ERROR_MESSAGE);
- }
- // load a properties file
- try {
- properties.load(input);
- } catch (IOException e1) {
- JOptionPane.showMessageDialog(new JFrame(),"Impossibile accedere al database, dati accesso non trovati", "Attenzione",JOptionPane.ERROR_MESSAGE);
- }
- String username = properties.getProperty("dbuser");
- String password = properties.getProperty("dbpassword");
- try {
- Class.forName(JDBC);
- connection.conn = DriverManager.getConnection( DB_URL, username,password);
- }
- catch(ClassNotFoundException e){
- JOptionPane.showMessageDialog(new JFrame(),
- "Impossibili accedere al database, Driver non trovati", "Attenzione",JOptionPane.WARNING_MESSAGE);
- }
- catch(SQLException e){
- JOptionPane.showMessageDialog(null,"Errore nella connessione al database! Contattare l'amministratore di sistema. Il sistema verrà chiuso ora.");
- }
- }
- /**
- *
- * Questo metodo permette di verificare se è già presente una connessione aperta
- * ed in caso utilizzare la stessa
- *
- * Pattern Singleton
- *
- * **/
- public static Connection getInstance(){
- if (connection.conn == null) {
- connessione();
- }
- return connection.conn;
- }
- /**
- *
- * Esegue le query di Update o insert
- * @param query
- * @param col
- * @param testo
- */
- public static boolean eseguiQuery(String query, String testo, String col) {
- boolean flag = false;
- try {
- PreparedStatement statExecute = getInstance().prepareStatement(query);
- statExecute.setString(1, col);
- statExecute.executeUpdate();
- flag = true;
- } catch (SQLException e) {
- flag = false;
- }
- return flag;
- }
- /**
- * Importa i commenti, è richiamato dalla classe --> ManageDatabase
- *
- * @param query
- * @param riga
- */
- public static String importaDatiCommento(String query, String riga) {
- ConnessioneDatabase.getInstance();
- ResultSet resCommento = null;
- String commento = "";
- try {
- PreparedStatement statKeep = getInstance().prepareStatement(query);
- statKeep.setString(1, riga);
- resCommento = statKeep.executeQuery();
- while(resCommento.next()){
- commento = resCommento.getString(1);
- }
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione del testo", "Attenzione",JOptionPane.WARNING_MESSAGE);
- }
- return commento;
- }
- /**
- *
- * Server per acquisire il nome delle colonne di una tabella dal database
- *
- * @param tableName
- * @return
- */
- public static List<String> acquisisciNomeColonne(String tableName) {
- ConnessioneDatabase.getInstance();
- Statement statKeepColumn = null;
- ResultSet resColumn = null;
- List<String> columnName = new ArrayList<String>(10);
- try {
- statKeepColumn = getInstance().createStatement();
- resColumn = statKeepColumn.executeQuery(String.format("DESCRIBE %1$S", tableName));
- while(resColumn.next()){
- columnName.add(resColumn.getString(1));
- }
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione del nome delle colonne", "Attenzione",JOptionPane.WARNING_MESSAGE);
- }
- return columnName;
- }
- /**
- *
- * Acquisisce i dati per le tabelle
- *
- * @param query
- * @param nUMCOLDIP
- * @return
- */
- public static List<String> acquisisciDatiTabella(String query, List<Integer> nUMCOLDIP) {
- ConnessioneDatabase.getInstance();
- List<String> columnData = new ArrayList<String>();
- int ripetizioni = nUMCOLDIP.size();
- try {
- Statement statKeepData = getInstance().createStatement();
- ResultSet resData = statKeepData.executeQuery(query);
- while(resData.next()){
- for(int i = 0; i < ripetizioni; i++){
- columnData.add(resData.getString(nUMCOLDIP.get(i)));
- }
- }
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(new JFrame(),"Problema con l'acquisizione dei dati", "Attenzione",JOptionPane.WARNING_MESSAGE);
- }
- return columnData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement