Advertisement
Guest User

DataBase.java

a guest
Sep 24th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. package portalestudente;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.Statement;
  9. import java.sql.SQLException;
  10. import java.util.*;
  11.  
  12. /*
  13.  * To change this license header, choose License Headers in Project Properties.
  14.  * To change this template file, choose Tools | Templates
  15.  * and open the template in the editor.
  16.  */
  17.  
  18. /**
  19.  *
  20.  * @author Isa
  21.  */
  22. public class DataBase {
  23.     public Connection connect() throws SQLException {
  24.  
  25.         Connection conn = null;
  26.         Properties connectionProps = new Properties();
  27.         connectionProps.put("user", "root");
  28.         connectionProps.put("password", "vincenzo92");
  29.        
  30.         //DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
  31.         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/portale?autoReconnect=true&useSSL=false", connectionProps);
  32.        
  33.         return conn;
  34.     }
  35.    
  36.     public void query(String q, ArrayList<ArrayList<String>> vs, ArrayList<ArrayList<Integer>> vi, ArrayList<Integer> pi)
  37.     throws SQLException {
  38.     query(q, vs, vi, pi, 0);
  39.     }
  40.    
  41.     public void query(String q, ArrayList<ArrayList<String>> vs, ArrayList<ArrayList<Integer>> vi, ArrayList<Integer> pi, int allInt)
  42.     throws SQLException {
  43.            
  44.             Connection conn = connect();
  45.        
  46.             Statement stmt = null;
  47.  
  48.             stmt = conn.createStatement();
  49.             ResultSet rs = stmt.executeQuery(q);
  50.            
  51.             ResultSetMetaData metaData = rs.getMetaData();
  52.             int numfields = metaData.getColumnCount();
  53.             /////// start debug
  54.            
  55.             while (rs.next()) {
  56.                 for (int i = 2; i <= numfields; i++) {
  57.                     if (i > 1) System.out.print(",  ");
  58.                         String columnValue = rs.getString(i);
  59.                     System.out.print(columnValue + " " + metaData.getColumnName(i));
  60.                 }
  61.                 System.out.println("");
  62.             }
  63.            
  64.             ////// end debug  
  65.             while(rs.next()){ //aggiunto per debug. Non dà più le eccezioni ma il login fallisce sempre
  66.             for (int i = 0; rs.next(); ++i) {
  67.                 // Dovremmo controllare se il primo elemento della riga è null per quando la query non da risultati
  68.                 if (allInt != 0 || pi.size() != 0)
  69.                     vi.add(new ArrayList<Integer>());
  70.    
  71.                 if (allInt == 0)
  72.                     vs.add(new ArrayList<String>());
  73.                
  74.                 for (int j = 0; j < numfields; ++j){
  75.                     if (allInt != 0 || isInt(j, pi) != false)
  76.                         vi.get(i).add(rs.getInt(j));
  77.                     else
  78.                         vs.get(i).add(rs.getString(j));
  79.                 }
  80.             }
  81.             }
  82.             System.out.println(vs.size()); //debug
  83.             if(rs != null){
  84.                 try{
  85.                     rs.close();
  86.                 }catch(SQLException e){ }
  87.             }
  88.            
  89.             if(conn != null){
  90.                 try{
  91.                     conn.close();
  92.                 }catch(SQLException e){ }
  93.             }
  94.        
  95.     }
  96.    
  97.     public void queryString(String q, ArrayList<ArrayList<String>> vs)
  98.     throws SQLException {
  99.  
  100.         ArrayList<ArrayList<Integer>> vi = new ArrayList<ArrayList<Integer>>();
  101.  
  102.         query(q, vs, vi, new ArrayList<Integer>());
  103.        
  104.     }
  105.  
  106.     public void queryInt(String q, ArrayList<ArrayList<Integer>> vi)
  107.     throws SQLException{
  108.  
  109.         ArrayList<ArrayList<String>> vs = new ArrayList<ArrayList<String>>();
  110.  
  111.         query(q, vs, vi, new ArrayList<Integer>(), 1);
  112.        
  113.     }
  114.  
  115.     public void query(String q) throws SQLException
  116.     {
  117.  
  118.         Connection conn = connect();
  119.        
  120.         Statement stmt = null;
  121.  
  122.         stmt = conn.createStatement();
  123.         stmt.executeQuery(q);
  124.        
  125.         if(conn != null){
  126.                 try{
  127.                     conn.close();
  128.                 }catch(SQLException e){ }
  129.             }
  130.  
  131.     }
  132.  
  133.     public boolean isInt(int i, ArrayList<Integer> v)
  134.     {
  135.  
  136.         for (Iterator<Integer> it = v.iterator(); it.hasNext();){
  137.             if (it.next() == i)
  138.                 return true;
  139.         }
  140.  
  141.         return false;
  142.  
  143.     }
  144.    
  145.     public String fixDate(String d)
  146.     {
  147.  
  148.         String data = d.replaceAll("-", "/");
  149.  
  150.         return data;
  151.        
  152.     }
  153.  
  154.     public static String unfixDate(String d)
  155.     {
  156.  
  157.         String data = d.replaceAll("/", "-");
  158.  
  159.         return data;
  160.        
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement