hyw7012

导航

201521123065《java程序设计》第14周学习总结

1. 本周学习总结

1、大部分情况下使用的数据库是关系型的数据库,使用表存储数据;
2、关系型数据库可以通过唯一的主键查找记录,也可以通过多个信息确定主键;
3、Mysql操作:显示-show databases(库);
                   show tables(表);
              创建-create database **;
              删除-drop **;
              连接-use **;
              查询-select ** from **;
              删除表数据-delete from ** where **;

   语句完后一定要加分号;
4、进行表操作之前一定要连接表(use **);
5、进行数据的插入:insert into ** (**,**,**) values (**,**,**);
   不用中文进行插入,除整型数据不用引号包装外,其他均用单引号;
6、JDBC:JavaDataBaseConnection,建立java程序与数据库的连接,这是除用Mysql语句外的方式;
7、JDBC四大步骤:DriverManage->Connection->Statement->ResultSet

2. 书面作业

1. MySQL数据库基本操作

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

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

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

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


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

1、建立连接:Connection con=DriverManager.getConnection(url, userName, password);
2、执行语句:Statement statement = con.createStatement();
3、保存结果:ResultSet resultNum = statement.executeUpdate(strSql);

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

3. PreparedStatement与参数化查询

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

//201521123065
Scanner sc=new Scanner(System.in);
		Connection conn = null;
		ResultSet rs=null;
		PreparedStatement st = null;
		String name=sc.nextLine();
		try {
			System.out.println(getStudentByName(name,rs,st,conn).toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
public static Student getStudentByName(String name, ResultSet rs, PreparedStatement st, Connection conn) throws SQLException {
		String sql = "select * from stuinfor where name = ?";

		conn = JDBCUtil.getConnection();
		st = conn.prepareStatement(sql);
		st.setString(1, name);
		rs = st.executeQuery(sql);

		int id = rs.getInt("id");
		String stuname = rs.getString("name");
		String gender = rs.getString("gender");
		Date birthday = rs.getDate("birthday");
		String major = rs.getString("major");
		int age = rs.getInt("age");
		Student student = new Student(id, stuname, gender, birthday, major, age);
		JDBCUtil.closeConnection(conn);
		return student;

	}

3.2 批量更新-批量插入1000个学生,统计整个操作所消耗的时间。对比普通方法插入与使用executeBatch方法所消耗的时间。(使用JUint4测试,需要出现时间对比截图)

BatchUpdate

普通方法

由上面比较可知
executeBatch的方法明显快于普通方法

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

4. JDBCUtil与DAO

4.1 粘贴一段你认为比较有价值的代码,并说明为什么要摘取这段代码。出现学号

//201521123065
public boolean writeStudent(Student student) {
		Connection conn = null;
		PreparedStatement pstat = null;
		String sql = "insert into student(name) values(?) ";//表中有id和name这列
		int result = -1;
		
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, student.getName());
			result = pstat.executeUpdate();
			
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JDBCUtil.realeaseAll(null,pstat, conn);
		}
		return result>0?true:false;
	}

	@Override
	public Student readStudent(String name) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from student where name like ?";//表中有id和name这列
		Student student = null;
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, name);
			rs = pstat.executeQuery();
			while(rs.next()){
				int stuId = rs.getInt("id");
				String stuName = rs.getString("name");
				student = new Student(stuId, stuName);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JDBCUtil.realeaseAll(rs,pstat, conn);
		}
		return student;
	}

	@Override
	public void diplayAllStudent() {
		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");
				System.out.format("该学生的id=%d,姓名=%s\n",id,name);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JDBCUtil.realeaseAll(rs,stat, conn);
		}
	}
这段代码可以向数据库写入学生信息,通过姓名查找该生的全部信息,
并且可以查询全部学生信息的功能
查询来的全部学生信息可以放入List,后返回;

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

通过DAO模式,可以将数据库的入口与出口进行封装,实现统一性;
StudenDaoListImpl、StudentDaoArrayImpl、StudentDaoJDBCImpl都实现了StudentDao的接口,关系明确;
TestMain类作为主要的入口,主要是一些版面与格式的优化。

参考:实验任务书-题目5
5. 使用数据库改造购物车系统

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

public static Student getStudentByName(String name, ResultSet rs, PreparedStatement st, Connection conn) throws SQLException {//根据姓名查找学生信息
		String sql = "select * from stuinfor where name = ?";

		conn = JDBCUtil.getConnection();
		st = conn.prepareStatement(sql);
		st.setString(1, name);
		rs = st.executeQuery(sql);

		int id = rs.getInt("id");
		String stuname = rs.getString("name");
		String gender = rs.getString("gender");
		Date birthday = rs.getDate("birthday");
		int age = rs.getInt("age");
		Student student = new Student(id, stuname, gender, birthday, age);
		JDBCUtil.closeConnection(conn);
		return student;
                
	}
ublic static void displayAll() {//显示所有学生信息
		// TODO Auto-generated method stub
		try {
			con = DriverManager.getConnection(url, userName, password);
			statement = con.createStatement();
			String strSql = "select * from stuinfor;";
			rs = statement.executeQuery(strSql);
			while (rs.next()) {
				System.out.println("abc");
                System.out.println(rs.getInt("id")+"\t");
				System.out.print(rs.getString("name") + "\t");
				System.out.print(rs.getString(3) + "\t");
				System.out.print(rs.getDate("birthday") + "\t");
				System.out.print(rs.getString(5) + "\t");
				System.out.println(rs.getInt("age")+"\t\n");
			}
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
		} finally {
			try {

				if (con != null)
					con.close();
				con = null;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			con = null;
		}
	}

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

数据库与文件一样是存储信息的容器
但相比之下,文件仅仅是用于存储,并不能很明显的体现文件之间的层次关系,
而庞大的数据库,不仅在存储的优化上或是在综合效率方面都优于文件的形式
比方说,记录号码的通讯本和手机上的通讯录,就能很好的体会其强势之处

3. 码云

3.1. 码云代码提交记录

posted on 2017-05-28 20:32  hyw7012  阅读(117)  评论(0编辑  收藏  举报