Advertisement
lemansky

Untitled

Nov 13th, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 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.         try{
  34.             stmt = conn.createStatement();
  35.             rs = stmt.executeQuery(sql);
  36.             while(rs.next()){
  37.                 String row = "";
  38.                 for (int i = 0; i < columnsArray.length; i++) {
  39.                     row += rs.getString(columnsArray[i]) + " ";
  40.                     System.out.println(row);
  41.                 }
  42.                 data.add(row);
  43.             }
  44.         } catch (SQLException e) {
  45.             System.out.println(e.getMessage());
  46.         }
  47.         return data;
  48.     }
  49.    
  50.      public ArrayList<String> selectWhere(String[] columnsArray, int[] whereColIndex, String[] whereValue, String table){
  51.         ArrayList<String> data = new ArrayList<String>();
  52.         String sql = "SELECT ";
  53. //        for (int i = 0; i < columnsArray.length; i++) {
  54. //            sql += columnsArray[i] + ", ";
  55. //        }
  56. //        sql = sql.substring(0, sql.length() - 2);
  57.         String columnsString = String.join(", ", columnsArray);
  58.        
  59.         sql += " " + columnsString + " FROM " + table + " WHERE ";
  60.         for (int i = 0; i < whereColIndex.length; i++) {
  61.             sql += columnsArray[whereColIndex[i]] + " LIKE '%" + whereValue[i] + "%' OR ";
  62.         }
  63.         sql = sql.substring(0, sql.length() - 4);
  64.        
  65.         System.out.println(sql);
  66.         try{
  67.             stmt = conn.createStatement();
  68.             rs = stmt.executeQuery(sql);
  69.             while(rs.next()){
  70.                 String row = "";
  71.                 for (int i = 0; i < columnsArray.length; i++) {
  72.                     row += rs.getString(columnsArray[i]) + " ";
  73.                 }
  74.                 data.add(row);
  75.             }
  76.         } catch (SQLException e) {
  77.             System.out.println(e.getMessage());
  78.         }
  79.         return data;
  80.     }
  81.    
  82.     public void close(){
  83.         try {
  84.             if (conn != null) {
  85.                 conn.close();
  86.             }
  87.         } catch (SQLException ex) {
  88.             System.out.println(ex.getMessage());
  89.         } catch (Throwable ex) {
  90.             Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
  91.         }
  92.  
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement