Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package sql;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5.  
  6. class Category {
  7.     private int catId;
  8.     private String catName;
  9.     private ArrayList<Category> children = new ArrayList<Category>();
  10.  
  11.     Category(int id, String name) {
  12.         catId = id;
  13.         catName = name;
  14.     }
  15.    
  16.     public boolean add(Category cat, int parent) {
  17.         if (parent == catId) {
  18.             children.add(cat);
  19.             return true;
  20.         } else {
  21.             for (Category child : children) {
  22.                 if (child.add(cat, parent)) return true;
  23.             }
  24.             return false;
  25.         }
  26.        
  27.     }
  28.    
  29.     private String spaces(int parent) {
  30.         char[] chars = new char[parent*4];
  31.         Arrays.fill(chars, ' ');
  32.         return new String(chars);
  33.     }
  34.    
  35.     public void print(int ancestors) {
  36.         if (catId != 0) {
  37.             if (ancestors == 0)
  38.                 System.out.println(spaces(ancestors) + catName);
  39.             else
  40.                 System.out.println(spaces(ancestors) + "|-- " + catName);
  41.         }
  42.        
  43.         for (Category child : children) {
  44.             child.print(ancestors + 1);
  45.         }
  46.     }
  47.    
  48.     public boolean printFiltered(int search) {
  49.         /*
  50.         if (search == catId) {
  51.             return true;
  52.         } else {
  53.             for (Category child : children) {
  54.                 if (child.add(cat, parent)) return true;
  55.             }
  56.             return false;
  57.         }
  58.         */
  59.     }
  60.    
  61. }
  62.  
  63. public class SqlTask {
  64.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  65.     static final String DB_URL = "jdbc:mysql://localhost/task";
  66.     static final String USER = "root";
  67.     static final String PASS = "";
  68.  
  69.     public static void main(String[] args) {
  70.         Category root = new Category(0, "cat 0");
  71.         Connection conn = null;
  72.         Statement stmt = null;
  73.         int id;
  74.         int parent;
  75.         String name;
  76.         String sql;
  77.        
  78.         int search = 3;
  79.  
  80.         try {
  81.             Class.forName("com.mysql.jdbc.Driver");
  82.             System.out.println("Connecting to database...");
  83.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  84.  
  85.             System.out.println("Creating statement...");
  86.             stmt = conn.createStatement();
  87.  
  88.             sql = "SELECT id, name, parent FROM category ORDER BY name ASC";
  89.             ResultSet rs = stmt.executeQuery(sql);
  90.  
  91.             while(rs.next()) {
  92.                 Category cat = null;
  93.                 id  = rs.getInt("id");
  94.                 name = rs.getString("name");
  95.                 parent = rs.getInt("parent");
  96.                 cat = new Category(id, name);
  97.  
  98.                 root.add(cat, parent);
  99.             }
  100.  
  101.             root.print(-1);
  102.  
  103.             rs.close();
  104.             stmt.close();
  105.             conn.close();
  106.         } catch(SQLException se) {
  107.             se.printStackTrace();
  108.         } catch(Exception e) {
  109.             e.printStackTrace();
  110.         } finally {
  111.             try {
  112.                 if (stmt!=null)
  113.                     stmt.close();
  114.             } catch(SQLException se2) {
  115.             }
  116.             try {
  117.                 if (conn!=null)
  118.                     conn.close();
  119.             } catch(SQLException se) {
  120.                 se.printStackTrace();
  121.             }
  122.         }
  123.         System.out.println("Task finished!");
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement