Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package portalestudente;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- import java.sql.SQLException;
- import java.util.*;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Isa
- */
- public class DataBase {
- public Connection connect() throws SQLException {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "root");
- connectionProps.put("password", "vincenzo92");
- //DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/portale?autoReconnect=true&useSSL=false", connectionProps);
- return conn;
- }
- public void query(String q, ArrayList<ArrayList<String>> vs, ArrayList<ArrayList<Integer>> vi, ArrayList<Integer> pi)
- throws SQLException {
- query(q, vs, vi, pi, 0);
- }
- public void query(String q, ArrayList<ArrayList<String>> vs, ArrayList<ArrayList<Integer>> vi, ArrayList<Integer> pi, int allInt)
- throws SQLException {
- Connection conn = connect();
- Statement stmt = null;
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(q);
- ResultSetMetaData metaData = rs.getMetaData();
- int numfields = metaData.getColumnCount();
- /////// start debug
- while (rs.next()) {
- for (int i = 2; i <= numfields; i++) {
- if (i > 1) System.out.print(", ");
- String columnValue = rs.getString(i);
- System.out.print(columnValue + " " + metaData.getColumnName(i));
- }
- System.out.println("");
- }
- ////// end debug
- while(rs.next()){ //aggiunto per debug. Non dà più le eccezioni ma il login fallisce sempre
- for (int i = 0; rs.next(); ++i) {
- // Dovremmo controllare se il primo elemento della riga è null per quando la query non da risultati
- if (allInt != 0 || pi.size() != 0)
- vi.add(new ArrayList<Integer>());
- if (allInt == 0)
- vs.add(new ArrayList<String>());
- for (int j = 0; j < numfields; ++j){
- if (allInt != 0 || isInt(j, pi) != false)
- vi.get(i).add(rs.getInt(j));
- else
- vs.get(i).add(rs.getString(j));
- }
- }
- }
- System.out.println(vs.size()); //debug
- if(rs != null){
- try{
- rs.close();
- }catch(SQLException e){ }
- }
- if(conn != null){
- try{
- conn.close();
- }catch(SQLException e){ }
- }
- }
- public void queryString(String q, ArrayList<ArrayList<String>> vs)
- throws SQLException {
- ArrayList<ArrayList<Integer>> vi = new ArrayList<ArrayList<Integer>>();
- query(q, vs, vi, new ArrayList<Integer>());
- }
- public void queryInt(String q, ArrayList<ArrayList<Integer>> vi)
- throws SQLException{
- ArrayList<ArrayList<String>> vs = new ArrayList<ArrayList<String>>();
- query(q, vs, vi, new ArrayList<Integer>(), 1);
- }
- public void query(String q) throws SQLException
- {
- Connection conn = connect();
- Statement stmt = null;
- stmt = conn.createStatement();
- stmt.executeQuery(q);
- if(conn != null){
- try{
- conn.close();
- }catch(SQLException e){ }
- }
- }
- public boolean isInt(int i, ArrayList<Integer> v)
- {
- for (Iterator<Integer> it = v.iterator(); it.hasNext();){
- if (it.next() == i)
- return true;
- }
- return false;
- }
- public String fixDate(String d)
- {
- String data = d.replaceAll("-", "/");
- return data;
- }
- public static String unfixDate(String d)
- {
- String data = d.replaceAll("/", "-");
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement