Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Application {
- public static void main(String[] args) {
- UserDao uDao = new UserDao();
- BookDao bDao = new BookDao();
- RentDao rDao = new RentDao();
- Scanner sc = new Scanner(System.in);
- System.out.println("Witamy w Bibliotece!");
- System.out.println("Wybierz co chcesz zrobić:");
- System.out.println(" ____UŻYTKOWNICY____");
- System.out.println("1. Dodaj nowego użytkownika");
- System.out.println("2. Edytuj dane użytkownika");
- System.out.println("3. Usuń użytkownika");
- System.out.println("4. Wyświetl wszystkich użytkowników");
- System.out.println(" ____KSIĄŻKI____");
- System.out.println("5. Dodaj nową książkę");
- System.out.println("6. Wyświetl wszystkie książki");
- System.out.println(" ____WYPOŻYCZENIA____");
- System.out.println("7. Dodaj nowe wypożyczenie");
- System.out.println("8. Wyświetl wszystkie wypożyczenia");
- int choose = sc.nextInt();
- switch (choose){
- case 1:{
- //addUser
- User u = new User();
- System.out.print("Podaj imię: ");
- u.setName(sc.next());
- System.out.print("Podaj nazwisko: ");
- u.setLastname(sc.next());
- System.out.print("Podaj numer telefonu: ");
- u.setTelephoneNumber(sc.nextInt());
- uDao.addUser(u);
- break;
- }
- case 2: {
- //editUser
- System.out.print("Podaj ID użytkownika: ");
- uDao.editUser(sc.nextInt());
- break;
- }
- case 3: {
- //removeUser
- System.out.print("Podaj ID użytkownika: ");
- uDao.removeUser(sc.nextInt());
- break;
- }
- case 4: {
- //showAllUsers
- uDao.showAll();
- }
- case 5: {
- //addBook
- Book b = new Book();
- System.out.print("Podaj tytuł: ");
- b.setTitle(sc.next());
- System.out.print("Podaj autora: ");
- b.setAuthor(sc.next());
- System.out.print("Podaj rok publikacji: ");
- b.setPublishedYear(sc.nextInt());
- bDao.addBook(b);
- break;
- }
- case 6: {
- //showAllBooks
- bDao.showAll();
- break;
- }
- case 7: {
- //addRent
- System.out.print("Podaj id użytkownika: ");
- int userId = sc.nextInt();
- System.out.print("Podaj id książki: ");
- int bookId = sc.nextInt();
- rDao.addRent(userId, bookId);
- break;
- }
- case 8: {
- //showAllRents
- rDao.showAll();
- break;
- }
- default: {
- System.out.println("Nieprawidłowy wybór!");
- }
- }
- sc.close();
- }
- }
- public class Book {
- private String title;
- private String author;
- private int publishedYear;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public int getPublishedYear() {
- return publishedYear;
- }
- public void setPublishedYear(int publishedYear) {
- this.publishedYear = publishedYear;
- }
- public Book(String title, String author, int publishedYear) {
- super();
- this.title = title;
- this.author = author;
- this.publishedYear = publishedYear;
- }
- public Book() {
- super();
- }
- }
- public class BookDao {
- private MySQLConnector connect;
- public BookDao() {
- connect = MySQLConnector.getINSTANCE();
- }
- public void addBook(Book book) {
- String sql = "INSERT INTO book VALUES(?,?,?,?)";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.setNull(1, 0);
- state.setString(2, book.getTitle());
- state.setString(3, book.getAuthor());
- state.setInt(4, book.getPublishedYear());
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Książka została dodana.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void showAll(){
- Statement state = connect.getStatement();
- ResultSet rs;
- try {
- rs = state.executeQuery("SELECT * FROM book");
- while(rs.next()){
- System.out.println("");
- System.out.println("ID: " + rs.getInt("id"));
- System.out.println("Tytuł: " + rs.getString("title"));
- System.out.println("Autor: " + rs.getString("author"));
- System.out.println("Rok wydania: " + rs.getInt("publishedYear"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public class Rent {
- private User user;
- private Book book;
- public Rent(User user, Book book) {
- super();
- this.user = user;
- this.book = book;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public Book getBook() {
- return book;
- }
- public void setBook(Book book) {
- this.book = book;
- }
- }
- public class RentDao {
- private MySQLConnector connect;
- public RentDao() {
- connect = MySQLConnector.getINSTANCE();
- }
- public void addRent(int userId, int bookId) {
- String sql = "INSERT INTO rent VALUES(?,?,?,?)";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.setNull(1, 0);
- state.setInt(2, userId);
- state.setInt(3, bookId);
- state.setNull(4, 0);
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Wypożyczenie zostało dodane.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void showAll(){
- Statement state = connect.getStatement();
- ResultSet rs;
- try {
- rs = state.executeQuery("SELECT * FROM rent");
- while(rs.next()){
- System.out.println("");
- System.out.println("Id użytkownika: " + rs.getInt("user"));
- System.out.println("Id książki: " + rs.getInt("book"));
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public class User {
- private String name;
- private String lastname;
- private int telephoneNumber;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = lastname;
- }
- public int getTelephoneNumber() {
- return telephoneNumber;
- }
- public void setTelephoneNumber(int telephoneNumber) {
- this.telephoneNumber = telephoneNumber;
- }
- public User(String name, String lastname, int telephoneNumber) {
- super();
- this.name = name;
- this.lastname = lastname;
- this.telephoneNumber = telephoneNumber;
- }
- public User() {
- super();
- }
- }
- public class UserDao {
- private MySQLConnector connect;
- public UserDao() {
- connect = MySQLConnector.getINSTANCE();
- }
- public void addUser(User user) {
- String sql = "INSERT INTO user VALUES(?,?,?,?)";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.setNull(1, 0);
- state.setString(2, user.getName());
- state.setString(3, user.getLastname());
- state.setInt(4, user.getTelephoneNumber());
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Użytkownik został dodany.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void removeUser(int id) {
- String sql = "DELETE FROM user WHERE id = ?";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.setInt(1, id);
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Użytkownik został usunięty.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void editUser(int id) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Wybierz co chcesz edytować:");
- System.out.println("1. Imię");
- System.out.println("2. Nazwisko");
- System.out.println("3. Numer telefonu");
- int choice = sc.nextInt();
- switch(choice){
- case 1:
- {
- System.out.print("Podaj nowe imię: ");
- String name = sc.next();
- String sql = "UPDATE user SET name = '" + name + "' WHERE id=" + id + "";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Imię zostało zmienione.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- break;
- }
- case 2:
- {
- System.out.print("Podaj nowe nazwisko: ");
- String lastname = sc.next();
- String sql = "UPDATE user SET lastname = '" + lastname + "' WHERE id=" + id + "";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Nazwisko zostało zmienione.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- break;
- }
- case 3:
- {
- System.out.print("Podaj nowy numer telefonu: ");
- int telephoneNumber = sc.nextInt();
- String sql = "UPDATE user SET telephoneNumber = '" + telephoneNumber + "' WHERE id=" + id + "";
- PreparedStatement state = connect.getPreparedStatement(sql);
- try {
- state.execute();
- state.isCloseOnCompletion();
- System.out.println("Numer telefonu został zmieniony.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- break;
- }
- default:
- {
- System.out.println("Nieprawidłowy wybór!");
- }
- }
- sc.close();
- }
- public void showAll(){
- Statement state = connect.getStatement();
- ResultSet rs;
- try {
- rs = state.executeQuery("SELECT * FROM user");
- while(rs.next()){
- System.out.println("");
- System.out.println("ID: " + rs.getInt("id"));
- System.out.println("Imię i nazwisko: " + rs.getString("name") + " " + rs.getString("lastname"));
- System.out.println("Numer telefonu: " + rs.getInt("telephoneNumber"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public class MySQLConnector {
- private static MySQLConnector instance;
- private final static String DBURL = "jdbc:mysql://5.135.218.27/Dorota?characterEncoding=utf8";
- private final static String DBLOGIN = "root";
- private final static String DBPASSWORD = "10135886";
- private Connection conn;
- private MySQLConnector(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println("Połączono!");
- }
- public static MySQLConnector getINSTANCE(){
- if (instance == null){
- instance = new MySQLConnector();
- }
- return instance;
- }
- public Statement getStatement(){
- try {
- return conn.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public PreparedStatement getPreparedStatement(String sql){
- try {
- return conn.prepareStatement(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment