Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. package moduli;
  2.  
  3. /*
  4. * To change this template, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7.  
  8.  
  9. /**
  10. *
  11. * @author lee
  12. */
  13. /*
  14. * Classe dedicata alla gestione del Database.
  15. * Gestisce l'apertura e la chiusura della connessione col Database
  16. * Fornisce i metodi per l'esecuzione delle query sul Database
  17. */
  18.  
  19. import Phoibos.*;
  20. import java.sql.*;
  21. import java.util.Vector;
  22.  
  23. public class Database {
  24. private static String nomeDB; // Nome del Database a cui connettersi
  25. private static String nomeUtente; // Nome utente utilizzato per la connessione alDatabase
  26. private static String pwdUtente; // Password usata per la connessione alDatabase
  27. private static String errore; // Raccoglie informazioni riguardo l'ultimaeccezione sollevata
  28. private static Connection db; // La connessione col Database
  29. private static boolean connesso; // Flag che indica se la connessione è attiva omeno
  30.  
  31. static void start() {
  32. Database.nomeDB = "Energroup";
  33. Database.nomeUtente = "root";
  34. Database.pwdUtente = "";
  35. connesso = false;
  36. errore = "";
  37. }
  38.  
  39.  
  40.  
  41. public Database(String nomeDB, String nomeUtente, String pwdUtente) {
  42. this.nomeDB = nomeDB;
  43. this.nomeUtente = nomeUtente;
  44. this.pwdUtente = pwdUtente;
  45. connesso = false;
  46. errore = "";
  47.  
  48. }
  49.  
  50. // Apre la connessione con il Database
  51. public static boolean connetti() {
  52. connesso = false;
  53. try {
  54.  
  55. // Carico il driver JDBC per la connessione con il database MySQL
  56. Class.forName("com.mysql.jdbc.Driver");
  57.  
  58. // Controllo che il nome del Database non sia nulla
  59. if (!nomeDB.equals("")) {
  60.  
  61. // Controllo se il nome utente va usato o meno per la connessione
  62. if (nomeUtente.equals("")) {
  63.  
  64. // La connessione non richiede nome utente e password
  65. db = DriverManager.getConnection("jdbc:mysql://localhost/" +nomeDB);
  66. } else {
  67.  
  68. // La connessione richiede nome utente, controllo se necessita nche della password
  69. if (pwdUtente.equals("")) {
  70.  
  71. // La connessione non necessita di password
  72. db = DriverManager.getConnection("jdbc:mysql://localhost/" +nomeDB + "?user=" + nomeUtente);
  73. } else {
  74.  
  75. // La connessione necessita della password
  76. db = DriverManager.getConnection("jdbc:mysql://localhost/" +nomeDB + "?user=" + nomeUtente + "&password=" + pwdUtente);
  77. }
  78. }
  79.  
  80. // La connessione è avvenuta con successo
  81. connesso = true;
  82. } else {
  83. System.out.println("Manca il nome del database!!");
  84. System.out.println("Scrivere il nome del database da utilizzareall'interno del file \"config.xml\"");
  85. System.exit(0);
  86. }
  87. } catch (Exception e) { errore = e.getMessage(); e.printStackTrace(); }
  88. return connesso;
  89. }
  90.  
  91. // Esegue una query di selezione dati sul Database
  92. // query: una stringa che rappresenta un'istruzione SQL di tipo SELECT daeseguire
  93. // colonne: il numero di colonne di cui sarà composta la tupla del risultato
  94. // ritorna un Vector contenente tutte le tuple del risultato
  95. public static Vector eseguiQuery(String query) {
  96. Vector v = null;
  97. String [] record;
  98. int colonne = 0;
  99. try {
  100. Statement stmt = db.createStatement(); // Creo lo Statement perl'esecuzione della query
  101. ResultSet rs = stmt.executeQuery(query); // Ottengo il ResultSetdell'esecuzione della query
  102. v = new Vector();
  103. ResultSetMetaData rsmd = rs.getMetaData();
  104. colonne = rsmd.getColumnCount();
  105.  
  106. while(rs.next()) { // Creo il vettore risultato scorrendo tutto ilResultSet
  107. record = new String[colonne];
  108. for (int i=0; i<colonne; i++) record[i] = rs.getString(i+1);
  109. v.add( (String[]) record.clone() );
  110. }
  111. rs.close(); // Chiudo il ResultSet
  112. stmt.close(); // Chiudo lo Statement
  113. } catch (Exception e) { e.printStackTrace(); errore = e.getMessage(); }
  114.  
  115. return v;
  116. }
  117.  
  118. public static String[] VecToStringS(Vector v){
  119.  
  120. String[] tmp;
  121. String[] s =new String[v.size()+1];
  122.  
  123.  
  124. s[0] = "Selezionare...";
  125. for(int k=0;k<v.size();k++)
  126. {
  127. tmp =(String[]) v.elementAt(k);
  128.  
  129. s[k+1] = tmp[0];
  130.  
  131. }
  132. return s;
  133.  
  134. }
  135.  
  136. public static String[] VecToStringSN(Vector v){
  137.  
  138. String[] tmp;
  139. String[] s =new String[v.size()+2];
  140.  
  141.  
  142. s[0] = "Selezionare...";
  143. s[1] = "Crea nuovo...";
  144. for(int k=0;k<v.size();k++)
  145. {
  146. tmp =(String[]) v.elementAt(k);
  147.  
  148. s[k+2] = tmp[0];
  149.  
  150. }
  151. return s;
  152.  
  153. }
  154.  
  155. public static void VecToString(String[] s,Vector v){
  156.  
  157. String[] tmp;
  158.  
  159.  
  160.  
  161. for(int k=0;k<1;k++)
  162. {
  163. if(v.size()>0){
  164. tmp =(String[]) v.elementAt(k);
  165. for(int i=0;i<tmp.length;i++){
  166. s[i] = tmp[i];
  167. }
  168.  
  169. }}
  170.  
  171. }
  172.  
  173. public static String[][] VecToStringMatrice(int col,Vector v){
  174. if(v!=null){
  175. String[] record = null;
  176. String[][] tutticlienti =new String[v.size()][col];
  177.  
  178. for(int i=0,p=0,k=0;i<tutticlienti.length;i++,p++)
  179. {
  180. for(int j=0;j<tutticlienti[0].length;j++)
  181. {
  182. if ( k<p+1 )
  183. record = (String[]) v.elementAt(k++);
  184. tutticlienti[i][j]=record[j];
  185.  
  186.  
  187. }
  188.  
  189. }
  190. return tutticlienti;
  191. }
  192. else{
  193. String[][] tutticlienti =new String[1][col];
  194. return tutticlienti;
  195. }
  196.  
  197.  
  198. }
  199.  
  200.  
  201.  
  202. // Esegue una query di aggiornamento sul Database
  203. // query: una stringa che rappresenta un'istuzione SQL di tipo UPDATE daeseguire
  204. // ritorna TRUE se l'esecuzione è adata a buon fine, FALSE se c'è stataun'eccezione
  205. public static boolean eseguiAggiornamento(String query) {
  206. int numero = 0;
  207. boolean risultato = false;
  208. try {
  209. Statement stmt = db.createStatement();
  210. numero = stmt.executeUpdate(query);
  211. risultato = true;
  212. stmt.close();
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. errore = e.getMessage();
  216. risultato = false;
  217. }
  218. return risultato;
  219. }
  220.  
  221.  
  222. // Chiude la connessione con il Database
  223. public static void disconnetti() {
  224. try {
  225. db.close();
  226. connesso = false;
  227. } catch (Exception e) { e.printStackTrace(); }
  228. }
  229.  
  230. public static boolean isConnesso() { return connesso; } // Ritorna TRUE se con il Database è attiva
  231.  
  232. public static String getErrore() { return errore; } // Ritorna il messaggiod'errore dell'ultima eccezione sollevata
  233.  
  234. public static boolean InsertINTO(String table,String[] into, String[] values ){
  235.  
  236. String query= "INSERT INTO "+table+" (";
  237.  
  238. for(int i=0; i<into.length-2; i++){
  239. query+= into[i]+",";
  240. }
  241.  
  242. query+= into[into.length-1]+") VALUES (";
  243.  
  244. for(int i=0; i<values.length-2; i++){
  245. query+= values[i]+",";
  246. }
  247.  
  248. query+= values[values.length-1]+") ;";
  249.  
  250. return eseguiAggiornamento(query);
  251. }
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement