Advertisement
weydile228

ToDoList

Nov 1st, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class ToDo {
  6.  
  7.     private static final String URL = "jdbc:mysql://localhost:3306/ToDo?useUnicode=true&characterEncoding=utf8 ";
  8.     private static final String USERNAME = "root";
  9.     private static final String PASSWORD = "";
  10.  
  11.     private static ResultSet result;
  12.     private static Connection con;
  13.     private static PreparedStatement prstmt;
  14.  
  15.     private static void add(String task) throws SQLException {
  16.         prstmt = con.prepareStatement("insert into Tasks (Task) values(?)");
  17.         prstmt.setString(1, task);
  18.         prstmt.executeUpdate();
  19.  
  20.         prstmt = con.prepareStatement("select id from Tasks where Task = ?");
  21.         prstmt.setString(1, task);
  22.         result = prstmt.executeQuery();
  23.         result.next();
  24.         System.out.println("Task - " + task + " added by id " + result.getInt("id"));
  25.     }
  26.  
  27.     private static void show(String buf) throws SQLException {
  28.         if (buf.equals("")) {
  29.             prstmt = con.prepareStatement("select id from Tasks");
  30.             ResultSet result1 = prstmt.executeQuery();
  31.             int tmp;
  32.             ArrayList<Integer> id = new ArrayList<>();
  33.             while (result1.next()){
  34.                 tmp = result1.getInt("id");
  35.                 id.add(tmp);
  36.                 prstmt = con.prepareStatement("select Task from Tasks where id = ?");
  37.                 prstmt.setInt(1, tmp);
  38.                 result = prstmt.executeQuery();
  39.                 result.next();
  40.                 System.out.println(tmp + ": " + result.getString("Task"));
  41.  
  42.             }
  43.             if (id.size() == 0){
  44.                 System.out.println("You have not added any tasks");
  45.                 return;
  46.             }
  47.         }else{
  48.             try{
  49.                 prstmt = con.prepareStatement("select task from Tasks where id = ?");
  50.                 prstmt.setInt(1, Integer.parseInt(buf));
  51.                 result = prstmt.executeQuery();
  52.                 result.next();
  53.                 System.out.println(result.getString("Task"));
  54.             }catch (SQLException e){
  55.                 System.out.println("Task № " + buf + " does not exist");
  56.             }
  57.         }
  58.     }
  59.  
  60.     private static void del(String buf) {
  61.         try {
  62.             prstmt = con.prepareStatement("select task from Tasks where id = ?");
  63.             prstmt.setInt(1, Integer.parseInt(buf));
  64.             result = prstmt.executeQuery();
  65.             result.next();
  66.             result.getString("Task");
  67.             prstmt = con.prepareStatement("delete from Tasks where id = ?");
  68.             prstmt.setInt(1, Integer.parseInt(buf));
  69.             prstmt.executeUpdate();
  70.             System.out.println("Task № " + buf + " deleted");
  71.         }catch (SQLException e) {
  72.             System.out.println("Task № " + buf + " does not exist");
  73.         }catch (NumberFormatException e){
  74.             System.out.println("Incorrect task number");
  75.         }
  76.     }
  77.  
  78.     private static void edit(String buf){
  79.         Scanner sc = new Scanner(System.in);
  80.         try {
  81.             prstmt = con.prepareStatement("select task from Tasks where id = ?");
  82.             prstmt.setInt(1, Integer.parseInt(buf));
  83.             result = prstmt.executeQuery();
  84.             result.next();
  85.             System.out.println("task to change - " + result.getString("Task"));
  86.             prstmt = con.prepareStatement("update Tasks set task  = ? where id = ?");
  87.             String tmp = sc.nextLine();
  88.             prstmt.setString(1, tmp);
  89.             prstmt.setInt(2, Integer.parseInt(buf));
  90.             prstmt.executeUpdate();
  91.             System.out.println("Task " + Integer.parseInt(buf) + " changed to " + tmp);
  92.         } catch (SQLException e) {
  93.             System.out.println("Task № " + buf + " does not exist");
  94.         }catch (NumberFormatException e){
  95.             System.out.println("Incorrect task number");
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) throws SQLException {
  100.         Driver driver;
  101.  
  102.         try   {
  103.             driver = new com.mysql.cj.jdbc.Driver();
  104.             DriverManager.registerDriver(driver);
  105.         }
  106.         catch (SQLException e1) {
  107.             System.out.println("Oops");
  108.         }
  109.  
  110.         try{
  111.             con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  112.         } catch (SQLException e) {
  113.             e.printStackTrace();
  114.         }
  115.  
  116.         Scanner sc = new Scanner(System.in);
  117.         while (true){
  118.             StringBuilder buf = new StringBuilder();
  119.             String[] command = sc.nextLine().split(" ");
  120.             for (int i = 1; i < command.length; i++){
  121.                 if (i + 1 == command.length){
  122.                     buf.append(command[i]);
  123.                 }else{
  124.                     buf.append(command[i]).append(" ");
  125.                 }
  126.             }
  127.             switch (command[0].toUpperCase()){
  128.                 case "ADD":
  129.                     add(buf.toString());
  130.                     break;
  131.                 case "SHOW":
  132.                     show(buf.toString());
  133.                     break;
  134.                 case "DEL":
  135.                     del(buf.toString());
  136.                     break;
  137.                 case "EDIT":
  138.                     edit(buf.toString());
  139.                     break;
  140.                 case "EXIT":
  141.                     return;
  142.                 default:
  143.                     System.out.println("incorrect input");
  144.                     break;
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement