Advertisement
lemansky

Untitled

Nov 17th, 2020
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.86 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.        
  31.         String columnsString = String.join(", ", columnsArray);
  32.         String sql = "SELECT " + columnsString + " FROM " + table;
  33.        
  34.         System.out.println(sql);
  35.         try{
  36.             stmt = conn.createStatement();
  37.             rs = stmt.executeQuery(sql);
  38.             while(rs.next()){
  39.                 String row = "";
  40.                 for (int i = 0; i < columnsArray.length; i++) {
  41.                    row += rs.getString(columnsArray[i]) + "---";
  42.                }
  43.                data.add(row);
  44.                
  45.            }
  46.        } catch (SQLException e) {
  47.            System.out.println(e.getMessage());
  48.        }
  49.        return data;
  50.    }
  51.    
  52.     public ArrayList<String> selectWhere(String[] columnsArray, int[] whereColIndex, String[] whereValue, String table){
  53.         ArrayList<String> data = new ArrayList<String>();
  54.        
  55.         String columnsString = String.join(", ", columnsArray);
  56.        
  57.         String sql = "SELECT " + columnsString + " FROM " + table + " WHERE ";
  58.             for (int i = 0; i < whereColIndex.length; i++) {
  59.                sql += columnsArray[whereColIndex[i]] + " LIKE '%" + whereValue[i] + "%' OR ";
  60.            }
  61.        sql = sql.substring(0, sql.length() - 4);
  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