Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class ToDo {
- private static final String URL = "jdbc:mysql://localhost:3306/ToDo?useUnicode=true&characterEncoding=utf8 ";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "";
- private static ResultSet result;
- private static Connection con;
- private static PreparedStatement prstmt;
- private static void add(String task) throws SQLException {
- prstmt = con.prepareStatement("insert into Tasks (Task) values(?)");
- prstmt.setString(1, task);
- prstmt.executeUpdate();
- prstmt = con.prepareStatement("select id from Tasks where Task = ?");
- prstmt.setString(1, task);
- result = prstmt.executeQuery();
- result.next();
- System.out.println("Task - " + task + " added by id " + result.getInt("id"));
- }
- private static void show(String buf) throws SQLException {
- if (buf.equals("")) {
- prstmt = con.prepareStatement("select id from Tasks");
- ResultSet result1 = prstmt.executeQuery();
- int tmp;
- ArrayList<Integer> id = new ArrayList<>();
- while (result1.next()){
- tmp = result1.getInt("id");
- id.add(tmp);
- prstmt = con.prepareStatement("select Task from Tasks where id = ?");
- prstmt.setInt(1, tmp);
- result = prstmt.executeQuery();
- result.next();
- System.out.println(tmp + ": " + result.getString("Task"));
- }
- if (id.size() == 0){
- System.out.println("You have not added any tasks");
- return;
- }
- }else{
- try{
- prstmt = con.prepareStatement("select task from Tasks where id = ?");
- prstmt.setInt(1, Integer.parseInt(buf));
- result = prstmt.executeQuery();
- result.next();
- System.out.println(result.getString("Task"));
- }catch (SQLException e){
- System.out.println("Task № " + buf + " does not exist");
- }
- }
- }
- private static void del(String buf) {
- try {
- prstmt = con.prepareStatement("select task from Tasks where id = ?");
- prstmt.setInt(1, Integer.parseInt(buf));
- result = prstmt.executeQuery();
- result.next();
- result.getString("Task");
- prstmt = con.prepareStatement("delete from Tasks where id = ?");
- prstmt.setInt(1, Integer.parseInt(buf));
- prstmt.executeUpdate();
- System.out.println("Task № " + buf + " deleted");
- }catch (SQLException e) {
- System.out.println("Task № " + buf + " does not exist");
- }catch (NumberFormatException e){
- System.out.println("Incorrect task number");
- }
- }
- private static void edit(String buf){
- Scanner sc = new Scanner(System.in);
- try {
- prstmt = con.prepareStatement("select task from Tasks where id = ?");
- prstmt.setInt(1, Integer.parseInt(buf));
- result = prstmt.executeQuery();
- result.next();
- System.out.println("task to change - " + result.getString("Task"));
- prstmt = con.prepareStatement("update Tasks set task = ? where id = ?");
- String tmp = sc.nextLine();
- prstmt.setString(1, tmp);
- prstmt.setInt(2, Integer.parseInt(buf));
- prstmt.executeUpdate();
- System.out.println("Task " + Integer.parseInt(buf) + " changed to " + tmp);
- } catch (SQLException e) {
- System.out.println("Task № " + buf + " does not exist");
- }catch (NumberFormatException e){
- System.out.println("Incorrect task number");
- }
- }
- public static void main(String[] args) throws SQLException {
- Driver driver;
- try {
- driver = new com.mysql.cj.jdbc.Driver();
- DriverManager.registerDriver(driver);
- }
- catch (SQLException e1) {
- System.out.println("Oops");
- }
- try{
- con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Scanner sc = new Scanner(System.in);
- while (true){
- StringBuilder buf = new StringBuilder();
- String[] command = sc.nextLine().split(" ");
- for (int i = 1; i < command.length; i++){
- if (i + 1 == command.length){
- buf.append(command[i]);
- }else{
- buf.append(command[i]).append(" ");
- }
- }
- switch (command[0].toUpperCase()){
- case "ADD":
- add(buf.toString());
- break;
- case "SHOW":
- show(buf.toString());
- break;
- case "DEL":
- del(buf.toString());
- break;
- case "EDIT":
- edit(buf.toString());
- break;
- case "EXIT":
- return;
- default:
- System.out.println("incorrect input");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement