201521123013 《Java程序设计》第14周学习总结

1. 本周学习总结

2. 书面做业

Q1. MySQL数据库基本操做

1.1 创建数据库,将本身的姓名、学号做为一条记录插入。(截图,需出现本身的学号、姓名)

1.2 在本身创建的数据库上执行常见SQL语句(截图)

Q2. 使用JDBC链接数据库与Statement

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

//2015013
public static int insert(Student stu) throws SQLException{
         conn = DriverManager
                    .getConnection(url,userName,password);
                    //sql语句使用拼接的方式
            String strsql="insert into student(stuno,name,age,birthdate)"
                    + " values('"+stu.getStuno()+"','"+stu.getName()+"',"+stu.getAge()+",'"+stu.getBirthdate()+"')";
            System.out.println(strsql);
            st=conn.createStatement();
            resultNum=st.executeUpdate(strsql);
        return resultNum;
    }

运行结果:
java

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

  1. 加载jdbc驱动
  2. 得到数据库链接,URL用来标识数据库
  3. 建立Statement,执行sql语句
  4. 处理结果
  5. 释放资源

Q3. PreparedStatement与参数化查询

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

//2015013
String sql="select * from student where name=?";
        pst=conn.prepareStatement(sql);
        pst.setString(1, name);
        rs=pst.executeQuery();  
        Student student=null;
        //while(rs.next())必须写,不然出现Before start of result set,由于rs是链式存储,一开始指针不在第一个数据位置,因此必须next()才能取得数据
        while(rs.next()){
            student=new Student(rs.getString("stuno"),rs.getString("name"),rs.getInt("age"),rs.getDate("birthdate"));
        }
    ...
    String sql="select * from student where birthdate between ? and ?";
        pst=conn.prepareStatement(sql);
        pst.setDate(1, java.sql.Date.valueOf(begin));
        pst.setDate(2, java.sql.Date.valueOf(end));
     ...

运行结果:

mysql

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

//2015013
public void batchTest() throws ClassNotFoundException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, userName, password);
            con.setAutoCommit(false);
            pstmt = con.prepareStatement(strSql);
            for (int i = 0; i < 1000; i++) {
                pstmt.setString(1, "2015022");
                pstmt.setString(2, "郭雅清");
                pstmt.setInt(3, 11);
                pstmt.setDate(4, java.sql.Date.valueOf("2000-09-02"));
                pstmt.addBatch();
            }
            pstmt.executeBatch();
            con.commit();

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } finally {
          realeaseAll(rs,pst,conn);
    }

Q4. JDBCUtil与DAO

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

//2015013
@Override
    public int add(Student stu) {
        // TODO Auto-generated method stub
        Connection conn=null;
        PreparedStatement pst=null;
        String sql="insert into students(name) values(?)";
        int result=1;
        try {
            conn=JDBCUtil.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setString(1, stu.getName());
            result=pst.executeUpdate();
            if(result<0){
                result=-1;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBCUtil.realeaseAll(null, pst, conn);
        }
        return result;
    }
            ...
        public Map<String,Student> fun(List<Student> stulist){
        Map<String,Student> map=new HashMap<String,Student>();
        for (int i = 0; i < stulist.size(); i++) {
            map.put(stulist.get(i).getName(),stulist.get(i));
        }
        return map;
    }

运行结果:
sql

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

  • 数据访问和业务逻辑分离,便于数据维护,业务逻辑不须要了解访问细节,如数据源是数据库、文件、数组、List等。

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

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

界面:

管理员(插入、删除):


用户(读取):

数据库

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

  • 使用文件,数据冗余度答,浪费存储空间,而使用数据库存储,数据是面向整个系统的,数据能够被多个用户、应用共享,减小冗余。
  • 当使用文件时,当文件变大时,使用文件访问速度会变得很慢,而使用数据库访问速度比使用文件访问更快。

选作:6. 批量更新测试

数据库课程上,须要测试索引对查找的加速做用。然而在几百或几千的数据量上进行操做没法直观地体验到索引的加速做用。现但愿编写一个程序,批量插入1000万条数据,且该数据中的某些字段的内容能够随机生成。

6.1 截图你的代码(出现学号)、统计运行时间


6.2 计算插入的速度到底有多快?(以条/秒、KB/秒两种方式计算)

  • 约5747条/秒
  • ibd文件(数据+索引)

选作:7. 事务处理

7.1 使用代码与运行结果证实你确实实现了事务处理功能。(粘贴一段你认为比较有价值的代码,出现学号)

//2015013
    public static void transfer(Account a, Account b, double x) throws Exception
    {
        String sql1="update account set balance=? where name=?";
        String sql2="update account set balance=? where name=?";
        try {
            conn=DriverManager.getConnection(url, userName, password);
            conn.setAutoCommit(false);
            if(a.getBalance()>=x){
                pst = conn.prepareStatement(sql1);
                pst.setDouble(1, a.getBalance()-x);
                pst.setString(2, a.getName());
                pst.executeUpdate();
                pst = conn.prepareStatement(sql2);
                pst.setDouble(1, b.getBalance()+x);
                pst.setString(2, b.getName());
                pst.executeUpdate();
                conn.commit();
                System.out.println("转帐成功");
            }else{
                throw new SQLException("余额不足");
            }
        }catch (SQLException e){
            e.printStackTrace();
            if (conn != null) {
                try{
                    System.err.print("事务正在回滚");
                    conn.rollback();
                } catch(SQLException excep) {
                    excep.printStackTrace();
                }
            }
        }
        finally {
            pst.close();
            conn.close();
        }
    }

7.2 你以为何时须要使用事务处理?

事务是并发控制的单位,是用户自定义的一个操做序列,这些操做要么都作,要么都不作,是一个不可分割的工做单位。当须要对数据库进行统一的提交和回滚时,好比当进行转帐时,必须帐户两边更改数据,要么成功要么失败,若是某一操做出错,则回滚以前全部的操做。数组

3. 码云

3.1. 码云代码提交记录

相关文章
相关标签/搜索