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


    1. 本周学习总结

    2. 书面作业

    1. MySQL数据库基本操作

    建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)
    在自己建立的数据库上执行常见SQL语句(截图)

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

    数据库简单操作
    1.显示数据库列表: show databases;

    2.删除数据库:drop database 数据库名;

    3.创建数据库 :create database 数据库名;

    4.建表:
    use 数据库名;
    create table 表名(字段设定列表);

    5.显示数据表的表结构: describe 表名;

    6.往表中插入数据:insert into 表名 values (”hyq”,”M”);

    7.显示表中的记录:select * from 表名;


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

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


    我认为最有价值的就是对命令的处理上:

    public static void displayAll() throws ClassNotFoundException //显示所有学生的学号、名字和出生时间,select
    {    ///201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String sql = "select * from student";
    	String userName = "root";//root
    	String password = "wujiantong";//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
    		System.out.println("ID	学号		姓名		年龄	生日		");
    		while(resultSet.next()){
    			System.out.print(resultSet.getInt("id")+"	");
    			System.out.print(resultSet.getString("stuno")+"	");
    			System.out.print(resultSet.getString("name")+"		");
    			System.out.print(resultSet.getInt("age")+"	");
    			System.out.print(resultSet.getDate("birthdate"));
    			System.out.println();
    		}
    	} catch (SQLException e) {
    		// TODO Auto-generated catch block
            	e.printStackTrace();
    	}
    }
    public static int insert(Student stu) throws ClassNotFoundException       //插入学生,insert
    {///201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String sql = "insert into student (stuno,name,age) values ('"+stu.getStuno()+"','"+stu.getName()+"','"+stu.getAge()+"')";
    	String userName = "root";//root
    	String password = "wujiantong";//123456
    	Connection conn = null;
    	int resultNum = 0;
    	Class.forName(driverName);///jdbc4.0 后无需使用这句进行驱动注册操作
    	try {
    		conn = DriverManager.getConnection(URL,userName,password);
    		Statement statement = conn.createStatement();
    		resultNum = statement.executeUpdate(sql);
    	} catch (SQLException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    public static void displayAllOrderByIdDesc() throws ClassNotFoundException //打印出学生的ID号、姓名和出生年日期,并按id降序排列。
    {///201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String sql = "select * from student order by id desc";
    	String userName = "root";//root
    	String password = "wujiantong";//123456
    	Class.forName(driverName);///jdbc4.0 后无需使用这句进行驱动注册操作
    	Connection conn = null;
    	try {
    		conn = DriverManager.getConnection(URL,userName,password);
    		Statement statement = conn.createStatement();
    		ResultSet resultSet = statement.executeQuery(sql);
    		// id | stuno     | name | gender | birthdate  | major
    		System.out.println("ID	学号		姓名		年龄	生日		");
    		while(resultSet.next()){
    			System.out.print(resultSet.getInt("id")+"	");
    			System.out.print(resultSet.getString("stuno")+"	");
    			System.out.print(resultSet.getString("name")+"		");
    			System.out.print(resultSet.getInt("age")+"	");
    			System.out.print(resultSet.getDate("birthdate"));
    			System.out.println();
    		}
    	} catch (SQLException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    public static int deleteStudent(int id) throws ClassNotFoundException      //按id删除某个学生,返回值为删除学生的个数,delete
    {///201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String sql = "delete from students where id='"+id+"'";
    	String userName = "root";//root
    	String password = "wujiantong";//123456
    	Connection conn = null;
    	int resultNum = 0;
    	Class.forName(driverName);///jdbc4.0 后无需使用这句进行驱动注册操作
    	try {
    		conn = DriverManager.getConnection(URL,userName,password);
    		Statement statement = conn.createStatement();
    		resultNum = statement.executeUpdate(sql);
    	} catch (SQLException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    public static int updateStudentAge() throws ClassNotFoundException//将每个学生的年龄+1,update语句
    {//201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String sql = "select * from student";
    	String userName = "root";//root
    	String password = "wujiantong";//123456
    	Connection conn = null;
    	int Num = 0;
    	Class.forName(driverName);///jdbc4.0 后无需使用这句进行驱动注册操作
    	try {
    		conn = DriverManager.getConnection(URL,userName,password);
    		Statement statement = conn.createStatement();
    		ResultSet resultSet = statement.executeQuery(sql);
    		int age = 0;
    		while (resultSet.next()) {
    			age = resultSet.getInt("age");
    			age ++;
    			String uesql = "update student set age="+age+" where id="+resultSet.getInt("id");
    			Class.forName(driverName);
    			conn = DriverManager.getConnection(URL,userName,password);
    			Statement statement1 = conn.createStatement();
    			int resultSet1 = statement1.executeUpdate(uesql);
    			Num++;
    		}
    	} catch (SQLException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    

    功能区:

    显示显示所有学生的信息:

    插入学生:

    按id降序排列:

    等等功能。


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

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

    (1)、装载驱动;
    (2)、与数据库建立连接;
    (3)、向数据库发送SQL语句;
    (4)、获得结果集;
    (5)、关闭连接,释放资源;


    3. PreparedStatement与参数化查询

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


    //201521123056 查找指定姓名的学生信息
    String sql = "select * from student where name=?";
    Class.forName(driverName);
    conn = DriverManager.getConnection(URL, userName,password);
    pStatement = conn.prepareStatement(sql);
    pStatement.setInt(1, age);
    ResultSet resultSet = pStatement.executeQuery(sql);
    System.out.println("ID	学号		姓名		年龄	生日		");
    System.out.print(resultSet.getInt("id")+"	");
    System.out.print(resultSet.getString("stuno")+"	");
    System.out.print(resultSet.getString("name")+"		");
    System.out.print(resultSet.getInt("age")+"	");
    System.out.print(resultSet.getDate("birthdate"));
    System.out.println();
    

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

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

    public void test() {///201521123056
    	String URL = "jdbc:mysql://localhost:3306/test";
    	String driverName = "com.mysql.jdbc.Driver";
    	String userName = "root";//root
    	String password = "wujiantong";//123456
    	PreparedStatement pStatement = null;
    	Connection conn = null;
    	try {
    		conn = DriverManager.getConnection(URL, userName, password);
    		for (int i = 0; i < 1000; i++) {
    			String strSql = "insert into students(stuno,name) values(?,?)";
    			pStatement = conn.prepareStatement(strSql);
    			pStatement.setString(1, "201521123056");
    			pStatement.setString(2, "wujiantong");
    			pStatement.executeUpdate();//添加到同一个批处理中
    		}
    	} catch (SQLException e) {
    		// TODO: handle exception
    	}finally{
    		if(conn!=null)
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		conn = null;
    	}
    }
    

    运行时间是335ms:


    4. JDBCUtil与DAO

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



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

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


    4.2 答:DAO模式指的是将连接与释放连接的操作封装起来,提供访问数据库的同一入口。避免代码的重复使用,将其封装起来也方便出错的时候进行修改。


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

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



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



    选做:6. 批量更新测试

    数据库课程上,需要测试索引对查找的加速作用。然而在几百或几千的数据量上进行操作无法直观地体验到索引的加速作用。现希望编写一个程序,批量插入1000万条数据,且该数据中的某些字段的内容可以随机生成。
    6.1 截图你的代码(出现学号)、统计运行时间
    6.2 计算插入的速度到底有多快?(以条/秒、KB/秒两种方式计算)

    选做:7. 事务处理

    7.1 使用代码与运行结果证明你确实实现了事务处理功能。(粘贴一段你认为比较有价值的代码,出现学号)
    7.2 你觉得什么时候需要使用事务处理?

    • 参考:实验任务书-题目4
      选做 8. 数据库连接池

    使用数据库连接池改写题目5

    • 参考:实验任务书-题目4
    • 数据连接池参考资料

    3. 码云

    3.1. 码云代码提交记录

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

  • 相关阅读:
    iOS 开发学习-类的创建与实现,与java语言的对比
    iOS 开发学习-import和include的区别
    iOS开发学习-nonatomic和atomic的区别
    用户调查报告
    第一阶段冲刺的总结报告
    数1 游戏
    用户体验
    找水王2
    场景调研 persona
    返回一个二维整数数组中最大联通子数组的和
  • 原文地址:https://www.cnblogs.com/wjt960310/p/6916318.html
Copyright © 2020-2023  润新知