Update TrainDaoTest.java

pull/1/head
pw6qtp7hv 8 months ago
parent 4bfb244888
commit fbc2530905

@ -1,6 +1,5 @@
package com.cn.test; package com.cn.test;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Date; import java.util.Date;
@ -15,132 +14,241 @@ import com.cn.domain.Train;
/** /**
* *
* @ClassName: TrainDaoTest * @ClassName: TrainDaoTest
* @Description: TrainDao * @Description: TrainDao访TrainDao
*
* TrainDao
* @author: ljy * @author: ljy
* @date: 2019916 10:53:22 * @date: 2019916 10:53:22
*/ */
public class TrainDaoTest { public class TrainDaoTest {
private TrainDao trainDao = new TrainDaoImpl(); // 创建TrainDao接口的具体实现类TrainDaoImpl的实例后续将通过该实例调用TrainDao中定义的各个数据库操作方法
// 来测试不同的列车信息相关的数据库操作功能。
@Test private TrainDao trainDao = new TrainDaoImpl();
public void testAdd() {
Train train = new Train();
train.setTrainNumber("K322");
train.setStartStation("北京");
train.setEndStation("天津");
train.setStartTime("2019-09-01 15:30:00");
train.setEndTime("2019-09-01 16:50:00");
train.setPrice(200);
train.setSeatNumber(1000);
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Timestamp addTime = Timestamp.valueOf(sdf.format(new Date()));
Timestamp addTime = new Timestamp(new Date().getTime());
train.setAddTime(addTime);
int recordNumber = 0;
try {
recordNumber = trainDao.add(train);
System.out.println(recordNumber);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test /**
public void testDelete() { * @TestJUnit
try { * TraintrainDaoadd
int recordNumber = trainDao.delete(1); * 便
System.out.println(recordNumber); */
} catch (SQLException e) { @Test
e.printStackTrace(); public void testAdd() {
} // 创建一个Train对象该对象用于封装要添加到数据库中的列车信息其各个属性对应数据库中列车信息表的不同字段。
} Train train = new Train();
// 设置列车的车次编号,这里将其设置为"K322",这是一个示例车次,实际应用中应符合实际的车次命名规则和业务要求。
train.setTrainNumber("K322");
// 设置列车的起始站名称,将其设置为"北京",表示列车出发的站点。
train.setStartStation("北京");
// 设置列车的终点站名称,此处设为"天津",代表列车最终要到达的站点。
train.setEndStation("天津");
// 设置列车的出发时间,采用"yyyy-MM-dd HH:mm:00"格式的字符串来表示时间这里设置的具体时间为2019-09-01 15:30:00
// 该格式需与数据库中对应时间字段的存储格式相匹配,以确保能正确插入数据。
train.setStartTime("2019-09-01 15:30:00");
// 设置列车的到达时间,同样遵循"yyyy-MM-dd HH:mm:00"格式设置为2019-09-01 16:50:00用于表示列车预计到达终点站的时间。
train.setEndTime("2019-09-01 16:50:00");
// 设置列车的票价单位通常为某种货币单位比如人民币元这里设置为200仅作为测试用途的示例价格数值。
train.setPrice(200);
// 设置列车的座位数量此处设为1000个座位同样是用于测试的一个示例数量值。
train.setSeatNumber(1000);
@Test // 获取当前系统时间并将其转换为Timestamp类型用于设置列车信息的添加时间即记录这条列车信息是什么时候被录入到系统中的。
public void testUpdate() { // 以下是两种获取Timestamp的方式这里采用了直接利用Date类的getTime方法获取时间戳再通过该时间戳构造Timestamp对象的方式。
Train train = new Train(); // 注释掉的代码是先使用SimpleDateFormat将Date对象格式化为指定的日期时间字符串格式"yyyy-MM-dd HH:mm:ss"
train.setTrainId(2); // 再通过Timestamp的valueOf方法将格式化后的字符串转换为Timestamp类型的方式两种方式都能达到目的只是实现途径略有不同。
train.setTrainNumber("XXX"); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
train.setStartStation("北京"); // Timestamp addTime = Timestamp.valueOf(sdf.format(new Date()));
train.setEndStation("天津"); Timestamp addTime = new Timestamp(new Date().getTime());
train.setStartTime("2019-09-01 15:30:00"); train.setAddTime(addTime);
train.setEndTime("2019-09-01 16:50:00");
train.setPrice(200);
train.setSeatNumber(1000);
Timestamp addTime = new Timestamp(new Date().getTime());
train.setAddTime(addTime);
try {
int recordNumber = trainDao.update(train);
System.out.println(recordNumber);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test int recordNumber = 0;
public void testGetAll() { try {
try { // 调用trainDao的add方法该方法在TrainDao接口中定义并由TrainDaoImpl类实现具体逻辑。
List<Train> list = trainDao.getAll(); // 其内部会构建合适的SQL插入语句与数据库建立连接然后将Train对象所包含的列车信息插入到数据库对应的列车信息表中
for(Train train : list) { // 返回值recordNumber通常表示受影响的数据库记录行数在成功插入一条记录的情况下一般返回值为1这里将其打印输出
System.out.println(train.toString()); // 以便查看添加操作是否按预期成功执行如果出现SQLException异常比如数据库连接问题、SQL语法错误等情况则会进入catch块进行处理。
} recordNumber = trainDao.add(train);
} catch (SQLException e) { System.out.println(recordNumber);
e.printStackTrace(); } catch (SQLException e) {
} // 如果在执行添加列车信息的操作过程中出现了SQLException异常
} // 通过e.printStackTrace()方法打印异常的栈追踪信息,方便开发人员排查问题所在,确定是哪部分代码或者数据库相关配置导致了异常的出现。
e.printStackTrace();
}
}
@Test /**
public void testGetById() { * @TestJUnitID
try { * trainDaodeleteID
Integer trainId = 2; * 便
Train train = trainDao.getById(trainId); */
System.out.println(train.toString()); @Test
} catch (SQLException e) { public void testDelete() {
e.printStackTrace(); try {
} // 调用trainDao的delete方法该方法的具体实现位于TrainDaoImpl类中其内部会构建相应的SQL删除语句
} // 根据传入的列车ID这里传入的硬编码值为1仅作为测试示例实际应用中应根据具体业务需求获取要删除的列车ID
// 在数据库中查找并删除对应的列车记录,处理数据库连接以及执行删除操作等相关事宜。
// 返回值recordNumber通常表示受影响的数据库记录行数若成功删除对应的列车记录返回值一般为1若数据库中不存在该ID对应的记录则返回值为0。
// 将返回的recordNumber打印输出便于查看删除操作是否按预期成功执行。
int recordNumber = trainDao.delete(1);
System.out.println(recordNumber);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test /**
public void testGetByTrainNumber() { * @TestJUnit
try { * TrainIDtrainDaoupdate
String trainNumber="XXX"; * 便
List<Train> list = trainDao.getByTrainNumber(trainNumber); */
for(Train train : list) { @Test
System.out.println(train.toString()); public void testUpdate() {
} Train train = new Train();
} catch (SQLException e) { // 设置要更新的列车记录在数据库中的唯一标识ID这里将其设置为2代表要更新的是数据库中ID为2的列车记录
e.printStackTrace(); // 通过这个ID可以准确找到对应的列车信息行进行更新操作实际应用中该值应根据具体业务需求来确定比如通过用户输入或者其他业务逻辑获取。
} train.setTrainId(2);
} train.setTrainNumber("XXX");
train.setStartStation("北京");
train.setEndStation("天津");
train.setStartTime("2019-09-01 15:30:00");
train.setEndTime("2019-09-01 16:50:00");
train.setPrice(200);
train.setSeatNumber(1000);
// 获取当前系统时间并转换为Timestamp类型用于设置列车信息的更新时间这里假设更新操作需要记录更新时间具体看业务需求
// 其原理与前面设置添加时间类似,都是获取当前时间并转换为合适的数据库时间类型。
Timestamp addTime = new Timestamp(new Date().getTime());
train.setAddTime(addTime);
@Test try {
public void testGetByStartEndStation() { // 调用trainDao的update方法该方法在TrainDao接口中定义并由TrainDaoImpl类实现具体更新逻辑
try { // 其内部会根据传入的Train对象所携带的更新信息在数据库中查找并更新对应ID的列车记录例如构建合适的SQL更新语句、
List<Train> list = trainDao.getByStartEndStation("上海", "广州", "2019-09-01"); // 处理数据库连接以及执行更新操作等返回值recordNumber通常表示受影响的数据库记录行数若成功更新对应的列车记录返回值一般为1
for(Train train : list) { // 若数据库中不存在该ID对应的记录则返回值为0将返回的recordNumber打印输出便于查看更新操作是否按预期成功执行。
System.out.println(train.toString()); int recordNumber = trainDao.update(train);
} System.out.println(recordNumber);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test /**
public void testGetAllStartStation() { * @TestJUnit
try { * trainDaogetAll
List<Train> list = trainDao.getAllStartStation(); * 便
for(Train train : list) { */
System.out.println(train.getStartStation()); @Test
} public void testGetAll() {
} catch (SQLException e) { try {
e.printStackTrace(); // 调用trainDao的getAll方法该方法的具体实现位于TrainDaoImpl类中其内部会构建相应的SQL查询语句
} // 执行查询操作从数据库的列车信息表中获取所有记录并将这些记录封装为Train对象列表返回以便后续进行遍历和处理。
} List<Train> list = trainDao.getAll();
@Test for (Train train : list) {
public void testGetAllEndStation() { // 遍历获取到的列车信息列表通过调用train对象的toString方法将每条列车信息以字符串的形式打印输出
try { // toString方法通常会按照一定的格式展示列车对象的各个属性信息方便查看查询结果的具体内容了解所有列车的详细情况。
List<Train> list = trainDao.getAllEndStation(); System.out.println(train.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @TestJUnitID
* IDtrainDaogetById
* IDID便
*/
@Test
public void testGetById() {
try {
// 定义要查询的列车记录在数据库中的唯一标识ID这里将其设置为2代表要获取ID为2的列车信息仅作为测试示例
// 实际应用中可根据具体业务场景通过合适的方式获取相应的列车ID值比如从用户输入、其他关联数据等途径获取。
Integer trainId = 2;
// 调用trainDao的getById方法该方法的具体实现位于TrainDaoImpl类中其内部会构建合适的SQL查询语句
// 根据传入的列车ID在数据库中查找并获取对应列车记录将查询到的列车信息封装为Train对象返回以便后续进行展示或其他处理。
Train train = trainDao.getById(trainId);
// 打印输出获取到的列车信息通过调用train对象的toString方法将列车对象的各个属性以字符串形式展示出来
// 方便查看查询结果,确认获取到的列车信息是否符合预期。
System.out.println(train.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @TestJUnit
* trainDaogetByTrainNumber
* 便
*/
@Test
public void testGetByTrainNumber() {
try {
// 定义要查询的列车车次编号,这里将其设置为"XXX",代表要获取车次编号为"XXX"的列车信息,仅作为测试示例,
// 实际应用中根据具体业务场景传入相应的车次编号值,比如用户输入想要查询的车次信息等情况。
String trainNumber = "XXX";
// 调用trainDao的getByTrainNumber方法该方法的具体实现位于TrainDaoImpl类中其内部可能会构建类似模糊查询如果支持模糊查询的话
// 或精确查询的SQL语句根据传入的列车车次编号在数据库中查找并获取对应列车记录将查询到的列车信息封装为Train对象列表返回以便后续遍历展示。
List<Train> list = trainDao.getByTrainNumber(trainNumber);
for (Train train : list) {
// 遍历获取到的列车信息列表通过调用train对象的toString方法将每条列车信息以字符串形式打印输出
// 方便查看查询到的符合条件的列车记录内容,确认是否获取到了期望的列车信息。
System.out.println(train.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @TestJUnit
* trainDaogetByStartEndStation
* 便
*/
@Test
public void testGetByStartEndStation() {
try {
// 调用trainDao的getByStartEndStation方法传入列车的起始站名称这里设置为"上海")、终点站名称(设置为"广州")以及出发日期(设置为"2019-09-01"
// 该方法的具体实现位于TrainDaoImpl类中其内部会构建相应的SQL查询语句根据这些条件在数据库中筛选出符合要求的列车记录
// 执行查询操作并将查询到的列车信息封装为Train对象列表返回以便后续进行遍历展示等操作。
List<Train> list = trainDao.getByStartEndStation("上海", "广州", "2019-09-01");
for (Train train : list) {
// 遍历获取到的列车信息列表通过调用train对象的toString方法将每条列车信息以字符串形式打印输出
// 方便查看查询到的符合条件的
System.out.println(train.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @TestJUnit
* trainDaogetAllStartStation
* 便
*/
@Test
public void testGetAllStartStation() {
try {
// 调用trainDao的getAllStartStation方法该方法的具体实现位于TrainDaoImpl类中其内部会构建相应的SQL查询语句
// 执行查询操作从数据库的列车信息表中获取所有列车记录的起始站名称,并将这些名称封装为列表返回,以便后续进行遍历和打印输出操作。
List<Train> list = trainDao.getAllStartStation();
for (Train train : list) {
// 遍历获取到的起始站名称列表,直接打印输出每个起始站名称,方便查看查询结果,了解数据库中所有列车的出发站点情况。
System.out.println(train.getStartStation());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @TestJUnit
* trainDaogetAllEndStation
* 便
*/
@Test
public void testGetAllEndStation() {
try {
// 调用trainDao的getAllEndStationList<Train> list = trainDao.getAllEndStation();
for(Train train : list) { for(Train train : list) {
System.out.println(train.getEndStation()); System.out.println(train.getEndStation());
} }

Loading…
Cancel
Save