Advertisement
lemansky

Untitled

Nov 18th, 2020
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.89 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package app;
  7. import java.sql.*;
  8. import java.util.*;
  9. import java.util.logging.*;
  10.  
  11. /**
  12.  *
  13.  * @author Lemansky
  14.  */
  15. public class Connect {
  16.     public Connection conn;
  17.     public Statement stmt;
  18.     public ResultSet rs;
  19.    
  20.     public Connect(){
  21.         try {
  22.             conn = DriverManager.getConnection("jdbc:sqlite:chinook.db");
  23.         } catch (SQLException e) {
  24.             System.out.println(e.getMessage());
  25.         }
  26.     }
  27.    
  28.     public ArrayList<String> select(String[] columnsArray, String table){
  29.         ArrayList<String> data = new ArrayList<String>();
  30.         String columnsString = String.join(", ", columnsArray);
  31.         String sql = "SELECT " + columnsString + " FROM " + table;
  32.        
  33. //        System.out.println(sql);
  34.         try{
  35.             stmt = conn.createStatement();
  36.             rs = stmt.executeQuery(sql);
  37.             while(rs.next()){
  38.                 String row = "";
  39.                 for (int i = 0; i < columnsArray.length; i++) {
  40.                    row += rs.getString(columnsArray[i]) + "+";
  41.                }
  42. //                System.out.println(row);
  43.                data.add(row);
  44.            }
  45.        } catch (SQLException e) {
  46.            System.out.println(e.getMessage());
  47.        }
  48.        return data;
  49.    }
  50.    
  51.     public ArrayList<String> selectWhere(String[] columnsArray, int[] whereColIndex, String[] whereValue, String table){
  52.         ArrayList<String> data = new ArrayList<String>();
  53.         String columnsString = String.join(", ", columnsArray);
  54.         String sql = "SELECT " + columnsString + " FROM " + table + " WHERE ";
  55.         for (int i = 0; i < whereColIndex.length; i++) {
  56.             sql += columnsArray[whereColIndex[i]] + " LIKE '%" + whereValue[i] + "%' OR ";
  57.        }
  58.        
  59.        System.out.println(sql);
  60.        sql = sql.substring(0, sql.length()-4);
  61.        
  62.        System.out.println(sql);
  63.        try{
  64.            stmt = conn.createStatement();
  65.            rs = stmt.executeQuery(sql);
  66.            while(rs.next()){
  67.                String row = "";
  68.                for (int i = 0; i < columnsArray.length; i++) {
  69.                    row += rs.getString(columnsArray[i]) + " ";
  70.                }
  71.                data.add(row);
  72.            }
  73.        } catch (SQLException e) {
  74.            System.out.println(e.getMessage());
  75.        }
  76.        return data;
  77.    }
  78.    
  79.    public void close(){
  80.        try {
  81.            if (conn != null) {
  82.                conn.close();
  83.            }
  84.        } catch (SQLException ex) {
  85.            System.out.println(ex.getMessage());
  86.        } catch (Throwable ex) {
  87.            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
  88.        }
  89.  
  90.    }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement