ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day13

2022-01-17 21:31:16  阅读:146  来源: 互联网

标签:qr String throw book Day13 new public


Dao包BookDao类

public interface BookDao {

    void insertBook(Book book);

    int findAllBookNumber();

    
    List<Book> findAllBookRecords(int startIndex,int pageSize);

    void remove(String bookId);

    Book getBookById(String bookId);

    void updateBook(Book book);

    int findPageBookNumber(String categoryId);

    List<Book> findPageBooks(int startIndex, int pageSize, String categoryId);
     
    
    
}
View Code

BookDaoImpl类

public class BookDaoImpl implements BookDao {
    QueryRunner qr = new QueryRunner(DbcpUtils.getDataSource());
        public void insertBook(Book book) {
        try {
            qr.update("insert into book(id,name,description,author,publish,price,path,photoname,categoryId) values (?,?,?,?,?,?,?,?,?)",
                    book.getId(),
                    book.getName(),
                    book.getDescription(),
                    book.getAuthor(),
                    book.getPublish(),
                    book.getPrice(),
                    book.getPath(),
                    book.getPhotoName(),
                    book.getCategoryId()
                    );
        } catch (SQLException e) {
            throw new AddBookException(e);
/*            throw new RuntimeException(e);
*/        }
    }
    
    public int findAllBookNumber() {
        try {
            Object obj = qr.query("select count(*) from book", new ScalarHandler(1));
            Long num = (Long) obj;
            return num.intValue();
        } catch (SQLException e) {
            throw new QueryBookException(e);
/*            throw new RuntimeException(e);
*/        }
    }
    
    public List<Book> findAllBookRecords(int startIndex, int pageSize) {
        try {
            List<Book> book  = qr.query("select * from book  limit ?,?", new BeanListHandler<Book>(Book.class),startIndex,pageSize);
            System.out.println("book:"+book);
            return book;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
    
    public void remove(String bookId) {
        try {
            qr.update("delete from book where id = ?",bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public Book getBookById(String bookId) {
        try {
            Book book = qr.query("select * from book where id = ?", new BeanHandler<Book>(Book.class),bookId);
            return book;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
    public void updateBook(Book book) {
        try {
            qr.update("update book set name=?,description=?,author=?,publish=?,price=?,path=?,photoname=?,categoryId=? where id =?",
                    
                    book.getName(),
                    book.getDescription(),
                    book.getAuthor(),
                    book.getPublish(),
                    book.getPrice(),
                    book.getPath(),
                    book.getPhotoName(),
                    book.getCategoryId(),
                    book.getId()
                    );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
    
    @Override
    public int findPageBookNumber(String categoryId) {
        try {
         Object obj = qr.query("select count(*)  from book where categoryId = ?",new ScalarHandler(1),categoryId);
         Long num =  (Long) obj;
         return num.intValue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Book> findPageBooks(int startIndex, int pageSize,
            String categoryId) {
        try {
            return qr.query("select *  from book where categoryId = ? limit ?,?",new BeanListHandler<Book>(Book.class),categoryId,startIndex,pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
View Code

CategoryDao类

public interface CategoryDao {
    void save(Category category);

    List<Category> getAllCategory();

    Category getCategoryById(String id);

    void removeCategory(String c);
    
    void updateCategory(Category category);
}
View Code

CategoryDaoImpl类

public class CategoryDaoImpl implements CategoryDao {
    QueryRunner qr  = new QueryRunner(DbcpUtils.getDataSource());

    public void save(Category category) {
        try {
            qr.update("insert into category(id,name,description) values (?,?,?) ", 
                    category.getId(),
                    category.getName(),
                    category.getDescription());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public List<Category> getAllCategory() {
        try {
            return qr.query("select * from category", new BeanListHandler<Category>(Category.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Category getCategoryById(String id) {
    
        try {
             Category c = qr.query("select * from category where id = ?",new BeanHandler<Category>(Category.class),id);
             return c;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void removeCategory(String categoryId) {
        try {
            System.out.println("dao实现的id: " +categoryId);
            
            qr.update("delete from category where id = ? ",categoryId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }

    public void updateCategory(Category category) {
        try {
            qr.update("update category set name = ?,description= ? where id = ? ", 
                    category.getName(),
                    category.getDescription(),
                    category.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
}
View Code

UserDao类

public interface UserDao {

    void save(User user);

    User login(String username, String password, String type);

}
View Code

UserDaoImpl类

public class UserDaoImpl implements UserDao {

    QueryRunner qr = new QueryRunner(DbcpUtils.getDataSource());
    
    public void save(User user) {
        
        try {
            qr.update("insert into user(id,username,password,type,sex) values (?,?,?,?,?)",
                    user.getId(),
                    user.getUsername(),
                    user.getPassword(),
                    user.getType(),
                    user.getSex()
                    );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public User login(String username, String password,String type) {
        try {
        System.out.println("..."+username+password+type);
            User user =  qr.query("select * from user where username = ? and password = ? and type = ?", 
                    new BeanHandler<User>(User.class),username,password,type);
            System.out.println("dao'user"+user);
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
View Code

DbcpUtils类

public class DbcpUtils {

        public static DataSource dataSource;

        static{
            try {
                String myFile = "dbcp.properties";
                InputStream in = DbcpUtils.class.getClassLoader().getResourceAsStream(myFile);
                Properties p = new Properties();
                p.load(in);
                dataSource = BasicDataSourceFactory.createDataSource(p);
            }  catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        
        public static DataSource getDataSource(){
            return dataSource;
        }
       
        public static Connection getConnection() throws SQLException {
            return dataSource.getConnection();
        }
}
View Code

 

标签:qr,String,throw,book,Day13,new,public
来源: https://www.cnblogs.com/headl-voi/p/15815421.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有