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

    1. 本周学习总结

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


    2. 书面作业

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

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

    1、建立数据库语句:

    2、数据库建立成功:

    3、添加学生信息语句:

    4、查询学生信息语句:


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

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

    使用Statement操作数据库,展现数据库中学生的id/stuno/birthdate信息,如下图:

    2.2 使用JDBC操作数据库主要包含哪几个步骤?
    -参考:实验任务书-题目2

    1、安装驱动;
    2、建立连接、connection;
    3、statement语句;
    4、RresultSet结果集;
    5、Release关闭释放资源。


    Q3. PreparedStatement与参数化查询
    参考:实验任务书-题目3
    3.1 使用PreparedStatement根据用户指定的查询条件进行查询。(粘贴一段你认为比较有价值的代码,出现学号)

    //201521123074
    Connection con = null;
    		PreparedStatement pStatement = null;
    		ResultSet rs = null;
    		SimpleDateFormat hmFromat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    		String driverName = "com.mysql.jdbc.Driver";
    		String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
    		String userName = "root";
    		String password = "gsjy1998041";
    		try {
    			con = DriverManager.getConnection(url, userName, password);
    			String strSql = "select * from students where name = ?";
    			pStatement = con.prepareStatement(strSql);
    			pStatement.setString(1,name);
    			rs = pStatement.executeQuery();
    			while (rs.next()) {
    				System.out.println(rs.getString("stuno") + "	");
    				System.out.println(rs.getString("name") + "	");
    				System.out.println(rs.getString("gender") + "	");
    				System.out.println(rs.getInt("age") + "	");
    				System.out.println(rs.getDate("birthdate") + "	");
    				System.out.println(rs.getString("major") + "	");
    			}
    			pStatement.close();
    

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

    啊。。这题老是会出现这样的错误T _T运行不了所以测不了时间。。。


    Q4. JDBCUtil与DAO
    参考:实验任务书-题目5

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

    public class JDBCUtil {
    //201521123074
    	private static String driverName = "com.mysql.jdbc.Driver";// jdbc4.0以后不需要
    	private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
    	private static String userName = "root";
    	private static String password = "123456";
    
    	public static void registerDriver() {
    		try {
    			Class.forName(driverName);// jdbc4.0以前需要这句进行驱动注册
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    			System.out.println("找不到驱动");
    		}
    	}
    
    	public static Connection getConnection() throws SQLException {
    		Connection conn = null;
    		System.out.println("正在连接数据库...");
    		conn = DriverManager.getConnection(url, userName, password);
    		System.out.println("数据库已连接!");
    		return conn;
    
    	}
    
    	public static void closeConnection(Connection conn) {
    		System.out.println("正在释放所有资源...");
    		
    			if (conn != null) {
    				try {
    					conn.close();
    					conn = null;
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    		
    	}
    	
    	/*
    	 * 释放所有资源
    	 */
    	public static void realeaseAll(ResultSet rs,Statement st,Connection conn){
    		if(rs!=null){
    			try {
    				rs.close();
    				rs = null;
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		if (st!=null){
    			try {
    				st.close();
    				st = null;
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		closeConnection(conn);
    	}
    

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

    各个层的代码分开写,思路要清晰些,而且方便维护.DAO存在大部分是为了理清思路,代码简洁易懂。


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

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

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

    文件储存比较方便管理,可是安全性很低;数据库存储比较复杂点,安全性高。


    3. 码云

    3.1. 码云代码提交记录

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


  • 相关阅读:
    Eclipse / android studio 添加第三方jar包 步骤
    Android checkbox 自定义点击效果
    Android 程序打包和安装过程
    Android 基础
    (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法
    (转)eclipse 导入Android 项目 步骤
    微信开放平台注册 步骤
    Android Studio 初级安装
    数组
    作用域问题代码
  • 原文地址:https://www.cnblogs.com/guzhiling/p/6893400.html
Copyright © 2011-2022 走看看