zoukankan      html  css  js  c++  java
  • 201521123035《Java程序设计》第十四周学习总结

    1. 本周学习总结

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

    2. 书面作业

    1. MySQL数据库基本操作

    建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)

    在自己建立的数据库上执行常见SQL语句(截图)

    -参考:实验任务书-题目1

    2. 使用JDBC连接数据库与Statement

    2.1 使用Statement操作数据库。(粘贴一段你认为比较有价值的代码,出现学号)

    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class ConnectMySQL {
    
    	/**201521123035
    	 * @param args
    	 * @throws ClassNotFoundException 
    	 */
    	public static void main(String[] args) throws ClassNotFoundException {
    		//JDBC��һЩ����
    		String URL = "jdbc:mysql://localhost:3306/test";
    		String driverName = "com.mysql.jdbc.Driver";
    		String sql = "select * from student";
    		String userName = "root";//root
    		String password = "kfc123456";//123456
    		Connection conn = null;
    		
    		Class.forName(driverName);//jdbc4.0 ������ʹ������������ע�����
    		
    		
    		try {
    			conn = DriverManager.getConnection(URL,userName,password);
    			Statement statement = conn.createStatement();
    			ResultSet resultSet = statement.executeQuery(sql);
    			// id | stuno     | name | gender | birthdate  | major
    			while(resultSet.next()){
    				int id = resultSet.getInt("id");
    				String stuno = resultSet.getString("stuno");
    				Date date = resultSet.getDate("birthdate");
    				System.out.print("id="+id+" stuno="+stuno+" birthdate="+date);
    			}
    			
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}finally{
    			if(conn!=null)
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}//����ʹ��Try..with..resources�﷨��
    			conn = null;
    		}
    	}
    }
    

    2.2 使用JDBC操作数据库主要包含哪几个步骤?

    创建数据库——发送SQL语句——返回结果——关闭释放资源
    

    -参考:实验任务书-题目2

    3. PreparedStatement与参数化查询

    3.1 使用PreparedStatement根据用户指定的查询条件进行查询。(粘贴一段你认为比较有价值的代码,出现学号)

    //201521123035
    String strSql = "select * from students where Id < ?";
    pStatement = con.prepareStatement(strSql);
    pStatement.setInt(1, 10);
    rs = pStatement.executeQuery();
    while(rs.next()){   
        System.out.println(rs.getInt("id"));
        System.out.println(rs.getString("stuno"));
        System.out.println(rs.getString("name"));
        System.out.println(rs.getInt("age"));
    }
    pStatement.close();
    

    3.2 批量更新-批量插入1000个学生,统计整个操作所消耗的时间。(使用方法executeBatch)

    参考:实验任务书-题目3
    

    4. JDBCUtil与DAO

    4.1 粘贴一段你认为比较有价值的代码,出现学号

    //201521123035
    
    //函数,遍历该List,将学生的姓名与年龄输出
    public void TdiplayAllStudent(List<Student> t) {
        for(int i=0;i<t.size();i++){
            System.out.println(t.get(i).toString());
        }
    }
    
    //将List中的所有Student取出,放入Map中,其中key为学生的姓名,value为相应的学生对象
    public void MAPdiplayAllStudent(List<Student> t) {
        Map<String,Student> map=new HashMap<String,Student>();
        for(int i=0;i<t.size();i++){
            map.put(t.get(i).getName(), t.get(i));
        }
    }
    Map<String,Integer> map=new HashMap<String,Integer>();
    
    
    //将ResultSet中的学生信息,构造成一个个的Student对象,并放入List中
    @Override
    public List<Student> getAllStudents() {
        List<Student> t=new ArrayList<>();
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        String sql = "select * from student";//表中有id和name这列
        try {
            conn = JDBCUtil.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                Student student0=new Student(id,name);
                t.add(student0);
            }
        }catch (SQLException sqle) {
            sqle.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs,stat, conn);
        }
        return t;
    }
    

    4.2 使用DAO模式访问数据库有什么好处?

    数据访问和业务逻辑分离,便于数据维护,业务逻辑不需要了解访问细节.

    5. 使用数据库改造购物车系统

    5.1 使用数据库改造以前的购物车系统(应有图形界面)。如果以前为完成购物车系统,可编写基于数据库的学生管理系统。包括对学生的增删改查,要求使用。

    5.2 相比较使用文件,使用数据库存储与管理数据有何不一样?

    3. 码云

    3.1. 码云代码提交记录

    在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
    

  • 相关阅读:
    推荐一款idea 翻译插件 ECTranslation
    idea 执行maven 命令
    Future Clalback使用案例
    newCachedThreadPool使用案例
    线程池 原理学习笔记
    使用curator框架简单操作zookeeper 学习笔记
    mongo aggregate 用法记录
    ReentrantLock 学习笔记
    VUE:过渡&动画
    VUE:生命周期
  • 原文地址:https://www.cnblogs.com/wuling15/p/6897225.html
Copyright © 2011-2022 走看看