发表于: 2017-08-06 17:17:19
3 1077
今天主要是改bug,然后对数据操作类的各功能进行测试,然后根据测试结果进一步完善代码,最后简单了解了下Junit。
错误原因:①数据库名是students,少了个s;
②是正斜杠而不是反斜杠
bug2:Access denied for user 'asus'@'localhost' (using password: YES)
错误定位:db.properties读取(<context:property-placeholder location="classpath:db.properties"/>)发生错误。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties"></property> </bean>即可正常读取。
bug3:Table 'students.student' doesn't exist
错误原因:各sql语句中插入的表名是student,应该为students
bug4:Column 'jnshuId' not found.
错误原因:mapRow中的rs.getString("")引号中应为数据库的列名,而不是Student类中的属性名 。
bug5:打印结果为数组地址
修改:将showStu函数中的sop(obj)改为foreach循环遍历。
Warn:Sat Aug 05 03:09:59 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
原因:未设置数据库的连接属性(是否使用SSL)。
Student stu = test.stuDAO.queryStuById(1); test.showStu(stu);查询结果:
Student stu = test.stuDAO.queryStuById(110); test.showStu(stu);结果:
Student stu = test.stuDAO.queryStuByName("胡凯博"); test.showStu(stu);结果:
原因:返回了大量同名的学生。这里应该修改代码:和通过id、专业+学号查询不同,确实可能存在同名的学生。所以,通过姓名查询时,应该返回List<Student>而不是Student。
Student stu = test.stuDAO.queryStuByJnshu("JAVA后端工程师", 1199); test.showStu(stu);结果:
Student stu = test.stuDAO.queryStuById(1); test.showStu(stu); //改-查看数据库得到结果 stu.setName("张三"); test.sop(test.stuDAO.updateStu(stu, 1));
Student stu = test.stuDAO.queryStuById(1); test.showStu(stu); stu.setName("李四"); stu.setJnshuId(7897); test.sop(test.stuDAO.addStu(stu));结果:
test.sop(test.stuDAO.delStuById(15));结果:
成功删除。通过专业+学号删除结果类似。
test.sop(test.stuDAO.delStuByName("胡凯博"));结果:
这里返回值是boolean,实际上通过姓名删除可能会删除多条记录,所以返回删除的记录数量比较好,应该修改。
3.完善代码并测试
- 各查询函数中,添加对要查询的学生不存在的情况的处理
- 通过name查询学生时,返回值改为List<Student>,因为可能有重名学生
- 通过name删除学生时,返回值改为int,因为可能删除多名学生
Student student = test.stuDAO.queryStuById(1111);
if (student!=null)
test.showStu(student);
结果:
List<Student> list = test.stuDAO.queryStuByName("李四");
if (list.size()!=0) {
int num = 0;
for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
Student student = (Student) iterator.next();
test.sop("Student"+num+++":");
test.showStu(student); test.sop("");
}
}
结果:③通过姓名删除学生:
test.sop(test.stuDAO.delStuByName("李四"));
打印结果为2。
4.Junit的学习
稍后会发一篇博客。
明天做任务中与Junit单元测试有关的部分。
评论