• java第14次作业


    1. 本周学习总结

    1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容。

    2. 使用数据库技术改造你的系统

    2.1 简述如何使用数据库技术改造你的系统。要建立什么表?截图你的表设计。


    两个dao接口

    • 管理用户模块,建立一个用户表users 用于存放用户,每个用户要拥有一个借阅表,表名以用户ID前加一个字母a (表名不能全为数字)命名。
    • 管理图书模块,建立一个图书表books用于存放书籍
      books表

    users表

    借阅表

    2.2 系统中使用到了JDBC中什么关键类?

    • DriverManager类:用于管理数据库中的所有驱动程序
    • Connection接口:用于与特定数据库进行连接
    • PreparedStatement接口:用于动态执行SQL语句
    • ResultSet接口:用于暂时存放数据库查询操作所获得的结果集

    2.3 截图数据库相关模块的关键代码。关键行需要加注释。

    图书dao接口

    public interface BookDao {
    	public void add(Book book,int num);	//添加书籍
    	public void remove(Book book);	//移除书籍
    	public void lendOrBack(Book book,LendOrBack action); //更改书籍变动
    	public Book peekBook(long ID);	//查询书籍
    
    }
    

    用户dao接口

    public interface UserDao {
    	public void add( User user );	//添加用户
    	public void remove( User user );	//删除用户
    	public User peekUser( long ID );	//查找用户
    	public void updateUser(User user,String name,String password);	//修改用户信息
    
    }
    

    判断数据库中是否存在指定表

        public static boolean HasTable(String name){
        	//用于判断数据库中是否存在指定表
        	Connection con = null;
        	DatabaseMetaData meta;
        	ResultSet rs = null;
    		try{
    			con = getConnection();
    			meta = con.getMetaData();
    			rs = meta.getTables(null, null, name, null);
    			while(rs.next()){
    				realeaseAll(rs,null,con);
    				return true;
    			}
    				
    		}catch(Exception e){
    			
    		}
    		realeaseAll(rs,null,con);
    		return false;
    	}
    

    创建book表并写入数据

    	private static void initializeBookStore() throws IOException, ClassNotFoundException{
    		Map<Book,Integer> books=new TreeMap<Book,Integer>();
    		try {if(JdbcUtil.HasTable("books")==false){
    			con = JdbcUtil.getConnection();	
    			strSql = "create table books(bookname char(20) not null,id long not null,category char(10),num int);";
    			pStatement = con.prepareStatement(strSql);
    			//当数据库中不存在book表时创建该表并写入数据
    			books.put(new Book("红",111,"文学类"), 30);
    			books.put(new Book("蓝",112,"文学类"), 50);
    			books.put(new Book("绿",113,"文学类"), 40);
    			books.put(new Book("黄",114,"经济类"), 10);
    			pStatement.executeUpdate(strSql);	//预编译
    			strSql = "insert into books(bookname,id,category,num) values(?,?,?,?)";
    			pStatement = con.prepareStatement(strSql);
    			con.setAutoCommit(false);
    			Iterator<Map.Entry<Book, Integer>> m = books.entrySet().iterator();	//将books中的数据写入数据库
    			while(m.hasNext()){
    				Map.Entry<Book, Integer> e = m.next();
    				if(e.getValue()>0)
    					pStatement.setString(1,e.getKey().getName());
    					pStatement.setLong(2,e.getKey().getID());
    					pStatement.setString(3,e.getKey().getCategory());
    					pStatement.setInt(4,e.getValue());
    					pStatement.addBatch();}
    			pStatement.executeBatch();
    			con.commit();
    			}
    		}catch(SQLException e){
    			
    		}finally{
    		
    		}
    				
    	}
    

    添加书籍

    	public void add(Book book,int num){	//添加书籍
    		strSql = "select * from books";
            try {
            	pStatement = con.prepareStatement(strSql);
    			rs = pStatement.executeQuery(strSql);
    			 while (rs.next()) {
    				 //当数据库中存在所要添加的书籍时,在原有书籍数量上加上要添加的数量
    				 if(rs.getString("bookname").equals(book.getName())){
    					 strSql = " update books set num=num+"+num+" where name="+book.getName()+";";
    					 rs = pStatement.executeQuery(strSql);
    					 return;
    				 }
    				 
    			 }
    			 //当数据库中不存在书籍时,插入一条新的书籍信息到数据库
    		strSql = "insert into books(bookname,id,category,num) values(?,?,?,?)";
    		pStatement = con.prepareStatement(strSql);
    		pStatement.setString(1,book.getName());
    		pStatement.setLong(2,book.getID());
    		pStatement.setString(3,book.getCategory());
    		pStatement.setInt(4,num);
    		pStatement.addBatch();
    		pStatement.executeUpdate();
            }catch(SQLException e){
    
            }
    }
    

    移除书籍

    	public void remove(Book book){
    		//移除书籍
    		strSql = "select * from books";
            try {
            	pStatement = con.prepareStatement(strSql);
    			rs = pStatement.executeQuery(strSql);
    			 while (rs.next()) {
    				 if(rs.getString("bookname").equals(book.getName())){
    					 strSql = "delete from books where bookname='"+book.getName()+"';";
    					 rs = pStatement.executeQuery(strSql);
    					 return;
    				 }
    				 
    			 }
    			 return;
            }catch(SQLException e){
            	
            }
    }
    

    根据ID查询书籍

    	public Book peekBook(long ID) {
    			//根据ID查询书籍
    		try {
    			con = JdbcUtil.getConnection();
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		}
            try {
            	strSql = "select * from books";
            	pStatement = con.prepareStatement(strSql);
    			rs = pStatement.executeQuery(strSql);
    			 while (rs.next()) {
    				 if(rs.getLong("id")==ID){
    		        	return new Book(rs.getString("bookname"),rs.getLong("id"),rs.getString("category"));
    		        	
    				 }
    		        }
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}finally{	
    		}
    		return null;
    	}
    

    3. 代码量统计

    3.1 统计本周完成的代码量

    需要将每周的代码统计情况融合到一张表中。

    周次 总行数 新增行数 总文件数 新增文件数
    1 115 115 17 17
    2 421 306 24 7
    3 698 277 30 6
    5 1085 387 38 8
    6 1497 412 48 10
    7 2033 536 57 9
    8 2265 232 60 3
    9 2728 522 65 5
    10 3360 632 73 8
    11 3958 598 83 10
    12 4435 477 90 7
    13 4802 367 96 6
    14 5244 442 101 5
    15 5876 632 108 7
  • 相关阅读:
    freemarker 遍历 hashmap 到select option
    三分钟跑起jsblocks
    hibernate_@GeneratedValue
    跑github上的Symfony项目遇到的问题2
    Activiti使用过程_1
    Symfony官方视频教程
    跑github上的Symfony项目遇到的问题
    剧本杀
    随遇而安
    开发者职场心得
  • 原文地址:https://www.cnblogs.com/yellower/p/8094243.html
Copyright © 2020-2023  润新知