接口测试第二天

第二天的课还是在周六进行,但是在周五的时候晚上通宵加班,第二天没法去上课,后面自己听了老师录制的视频,还是不如直接听老师讲课来的实在,同样是留了两个作业:

第一个

package com.mytesting.jdbc;

//jdbc:
//手动创建表:
//blog
//字段:
//title
//content
//username
//createTime
//
//用jdbc实现:
//博客内容新增、修改、查询、删除
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Homework2JDBC {
    public static void main(String args[]) throws Exception {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new Exception("没有发现驱动类。");
        }
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";
        String insertSql = "insert into blog(title,username,content,createTime) values "
                + "('博客标题','名字','内容','2015-8-20 23:22:11')";
        String updateSql = "update blog set title = '博客标题修改' where title = '博客标题'";
        String querySql = "select title from blog where username = '名字'";
        String delSql = "delete from blog where username = '名字'";
        try {
            Connection conn = DriverManager.getConnection(url, username,
                    password);
            // 增加
            updateData(conn, insertSql);
            // 查询
            String title = getTitle(conn, querySql);
            System.out.println("博客的标题是:" + title);
            // 修改
            updateData(conn, updateSql);
            // 删除
            updateData(conn, delSql);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //所有数据变更操作
    public static void updateData(Connection conn, String sql)
            throws SQLException {
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.executeUpdate();
    }

    //查询标题
    public static String getTitle(Connection conn, String sql)
            throws SQLException {
        String title = null;
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            title = rs.getString(1);
        }
        return title;
    }

}

第二个

package com.mytesting;
//commons-io-2.4.jar
//用IOUtils或FileUtils实现:
//遍历一个目录下的所有文件,打印文件名称,并将其拷贝到另外一个目标目录。

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class Homework2IO {

    public static void main(String[] args) throws IOException {
        String oldURI = "D:/test";
        String newURI = "D:/test1";

        File oldFile = new File(oldURI);
        File newFile = new File(newURI);
        
        System.out.println("在未拷贝之前,原路径下的文件为:");
        prtFiles(oldFile);
        System.out.println("在未拷贝之前,新路径下的文件为:");
        prtFiles(newFile);
        // 按目录拷贝
        FileUtils.copyDirectory(oldFile, newFile);

        System.out.println("在拷贝之后,原路径下的文件为:");
        prtFiles(oldFile);
        System.out.println("在拷贝之后,新路径下的文件为:");
        prtFiles(newFile);
    }

    // 打印文件夹下的文件列表
    public static void prtFiles(File file) {
        for (File singleFile : file.listFiles()) {
            System.out.println(singleFile.getName());
        }
    }

}

在第一个作业里面稍微有点儿偷懒,但是总体没啥,主要是要了解具体的使用方法,熟悉对数据库的连接建立及使用。

第二节课老师主要讲了异常、maven使用、JDBC、IO、servlet

原文地址:https://www.cnblogs.com/yunmenzhe/p/4747301.html