Advertisement
lemansky

Untitled

Dec 7th, 2021
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. package app;
  2. import java.sql.*;
  3. import java.util.*;
  4. import java.util.logging.*;
  5.  
  6. public class Connect {
  7.     public Connection conn;
  8.     public Statement stmt;
  9.     public ResultSet rs;
  10.    
  11.     public Connect(){
  12.         try {
  13.             conn = DriverManager.getConnection("jdbc:sqlite:chinook.db");
  14.         } catch (SQLException e) {
  15.             System.out.println(e.getMessage());
  16.         }
  17.     }
  18.    
  19.     public ArrayList<String> select(String[] columnsArray, String table){
  20.         ArrayList<String> data = new ArrayList<String>();
  21.        
  22.         String columnsString = String.join(", ", columnsArray);
  23.         String sql = "SELECT " + columnsString + " FROM " + table;
  24.        
  25.         System.out.println(sql);
  26.         try{
  27.             stmt = conn.createStatement();
  28.             rs = stmt.executeQuery(sql);
  29.             while(rs.next()){
  30.                 String row = "";
  31.                 for (int i = 0; i < columnsArray.length; i++) {
  32.                     row += rs.getString(columnsArray[i]) + "---";
  33.                 }
  34.                 data.add(row);
  35.                
  36.             }
  37.         } catch (SQLException e) {
  38.             System.out.println(e.getMessage());
  39.         }
  40.         return data;
  41.     }
  42.    
  43.      public ArrayList<String> selectWhere(String[] columnsArray, int[] whereColIndex, String[] whereValue, String table){
  44.         ArrayList<String> data = new ArrayList<String>();
  45.        
  46.         String columnsString = String.join(", ", columnsArray);
  47.        
  48.         String sql = "SELECT " + columnsString + " FROM " + table + " WHERE ";
  49.             for (int i = 0; i < whereColIndex.length; i++) {
  50.                 sql += columnsArray[whereColIndex[i]] + " LIKE '%" + whereValue[i] + "%' OR ";
  51.             }
  52.         sql = sql.substring(0, sql.length() - 4);
  53.         System.out.println(sql);
  54.         try{
  55.             stmt = conn.createStatement();
  56.             rs = stmt.executeQuery(sql);
  57.             while(rs.next()){
  58.                 String row = "";
  59.                 for (int i = 0; i < columnsArray.length; i++) {
  60.                     row += rs.getString(columnsArray[i]) + "---";
  61.                 }
  62.                 data.add(row);
  63.             }
  64.         } catch (SQLException e) {
  65.             System.out.println(e.getMessage());
  66.         }
  67.         return data;
  68.     }
  69.      
  70.     public void delete(String column, int id, String table){
  71.         String sql = "DELETE FROM " + table + " WHERE " + column + " = " + id;
  72.         try{
  73.             stmt = conn.createStatement();
  74.             rs = stmt.executeQuery(sql);
  75.         } catch (SQLException e) {
  76.             System.out.println(e.getMessage());
  77.         }
  78.     }    
  79.    
  80.     public void insert(String[] columnsArray, String[] valuesArray, String table){
  81.         String columnsString = String.join(", ", columnsArray);
  82.         String valuesString = "'" + String.join("', '", valuesArray) + "'";
  83.        
  84.         valuesString = valuesString.replace("''", "null");
  85.        
  86.         String sql = "INSERT INTO " + table + " (" + columnsString + ") VALUES ("
  87.                 + valuesString + ")";
  88.         System.out.println(sql);
  89.         try{
  90.             stmt = conn.createStatement();
  91.             stmt.executeQuery(sql);
  92.         } catch (SQLException e) {
  93.             System.out.println(e.getMessage());
  94.         }
  95.     }
  96.    
  97.     public void update(String[] columnsArray, String[] valuesArray, String whereColumn, int id, String table){
  98.         String sql =  "UPDATE " + table + " SET ";
  99.         for (int i = 0; i < columnsArray.length; i++) {
  100.             sql = sql + columnsArray[i] + " = " +
  101.                     (valuesArray[i].equals("") ? "null, " : "'" + valuesArray[i] + "', ");
  102.         }
  103.         sql = sql.substring(0, sql.length() - 2);
  104.  
  105.         sql = sql + " WHERE " + whereColumn + " = " + id;
  106.        
  107.         System.out.println(sql);
  108.         try{
  109.             stmt = conn.createStatement();
  110.             stmt.executeQuery(sql);
  111.         } catch (SQLException e) {
  112.             System.out.println(e.getMessage());
  113.         }
  114.     }
  115.    
  116.     public void close(){
  117.         try {
  118.             if (conn != null) {
  119.                 conn.close();
  120.             }
  121.         } catch (SQLException ex) {
  122.             System.out.println(ex.getMessage());
  123.         } catch (Throwable ex) {
  124.             Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
  125.         }
  126.  
  127.     }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement