03_java基础(九)之综合练习与考核评估

25.综合练习之车站业务分析

  

      完成步骤:

  需求: 以车站业务对车票做增删改查操作

  1.建立数据库

  2.建立车票表

  3.建立java项目结构(model\dao\service\test)

  4.创建model

  5.创建dao(接口和实现)并实现测试

  6.创建service(接口实现)

26.综合练习之数据库与表建立

  

27.项目标准结构创建

  

28\29\30\31.综合练习之代码实现

  1.model

 1 package com.day03.station.model;
 2 
 3 /**
 4  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
 5  * 疑问咨询wx:851298348
 6  */
 7 public class Ticket {
 8     private Integer id;
 9     private String startStation;
10     private String stopStation;
11     private String startTime;
12     private Integer ticketPrice;
13 
14     public Integer getId() {
15         return id;
16     }
17 
18     public void setId(Integer id) {
19         this.id = id;
20     }
21 
22     public String getStartStation() {
23         return startStation;
24     }
25 
26     public void setStartStation(String startStation) {
27         this.startStation = startStation;
28     }
29 
30     public String getStopStation() {
31         return stopStation;
32     }
33 
34     public void setStopStation(String stopStation) {
35         this.stopStation = stopStation;
36     }
37 
38     public String getStartTime() {
39         return startTime;
40     }
41 
42     public void setStartTime(String startTime) {
43         this.startTime = startTime;
44     }
45 
46     public Integer getTicketPrice() {
47         return ticketPrice;
48     }
49 
50     public void setTicketPrice(Integer ticketPrice) {
51         this.ticketPrice = ticketPrice;
52     }
53 }
Ticket

  2.dao接口

 1 package com.day03.station.dao;
 2 
 3 import com.day03.station.model.Ticket;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
 9  * 疑问咨询wx:851298348
10  */
11 public interface ITicketDao {
12     //增加
13     public void save(Ticket ticket);
14     //删除  根据id删除
15     public void delete(Integer id);
16 
17     //修改
18     public void update(Ticket ticket);
19     //查找   查单个  根据id查询
20     public Ticket queryById(Integer id);
21     //查找多个  (集合装)List
22     public List<Ticket> queryAll();
23 }
ITicketDao

  3.dao实现

  1 package com.day03.station.dao.impl;
  2 
  3 import com.day03.station.dao.ITicketDao;
  4 import com.day03.station.model.Ticket;
  5 
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.PreparedStatement;
  9 import java.sql.ResultSet;
 10 import java.util.ArrayList;
 11 import java.util.List;
 12 
 13 /**
 14  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
 15  * 疑问咨询wx:851298348
 16  */
 17 public class TicketDao implements ITicketDao {
 18     @Override
 19     public void save(Ticket ticket) {
 20 
 21         try {
 22 
 23             //1.加载
 24             Class.forName("com.mysql.jdbc.Driver");
 25             //2.链接
 26             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
 27             //3.创建编译语句
 28             String sql="INSERT INTO ticket (start_station,stop_station,start_time,ticket_price) VALUES (?,?,?,?)";
 29             PreparedStatement preparedStatement = connection.prepareStatement(sql);
 30                 //设置参数
 31             preparedStatement.setString(1,ticket.getStartStation());
 32             preparedStatement.setString(2,ticket.getStopStation());
 33             preparedStatement.setString(3,ticket.getStartTime());
 34             preparedStatement.setInt(4,ticket.getTicketPrice());
 35             //4.执行
 36             preparedStatement.executeUpdate();
 37             //5.释放资源
 38             preparedStatement.close();
 39             connection.close();
 40         } catch (Exception e) {
 41             e.printStackTrace();
 42         }
 43 
 44     }
 45 
 46     @Override
 47     public void delete(Integer id) {
 48         try {
 49 
 50             //1.加载
 51             Class.forName("com.mysql.jdbc.Driver");
 52             //2.链接
 53             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
 54             //3.创建编译语句
 55             String sql="DELETE FROM ticket WHERE id=?";
 56             PreparedStatement preparedStatement = connection.prepareStatement(sql);
 57             //设置参数
 58             preparedStatement.setInt(1,id);
 59             //4.执行
 60             preparedStatement.executeUpdate();
 61             //5.释放资源
 62             preparedStatement.close();
 63             connection.close();
 64         } catch (Exception e) {
 65             e.printStackTrace();
 66         }
 67     }
 68 
 69     @Override
 70     public void update(Ticket ticket) {
 71         try {
 72 
 73             //1.加载
 74             Class.forName("com.mysql.jdbc.Driver");
 75             //2.链接
 76             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
 77             //3.创建编译语句
 78             String sql="UPDATE ticket SET ticket_price=? WHERE id=?";
 79             PreparedStatement preparedStatement = connection.prepareStatement(sql);
 80             //设置参数
 81             preparedStatement.setInt(1,ticket.getTicketPrice());
 82             preparedStatement.setInt(2,ticket.getId());
 83 
 84             //4.执行
 85             preparedStatement.executeUpdate();
 86             //5.释放资源
 87             preparedStatement.close();
 88             connection.close();
 89         } catch (Exception e) {
 90             e.printStackTrace();
 91         }
 92     }
 93 
 94     @Override
 95     public Ticket queryById(Integer id) {
 96         //准备一个对象,装获取到的结果数据
 97         Ticket ticket = new Ticket();
 98         try {
 99 
100             //1.加载
101             Class.forName("com.mysql.jdbc.Driver");
102             //2.链接
103             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
104             //3.创建编译语句
105             String sql="SELECT id,start_station,stop_station,start_time,ticket_price FROM ticket  WHERE id=?";
106             PreparedStatement preparedStatement = connection.prepareStatement(sql);
107             //设置参数
108             preparedStatement.setInt(1,id);
109             //4.执行
110             ResultSet resultSet = preparedStatement.executeQuery();
111              //解析结果集
112             while (resultSet.next()){
113                 //取id
114                 int id1 = resultSet.getInt("id");
115                 //取开始车站
116                 String startStation = resultSet.getString("start_station");
117                 //取到达车站
118                 String stopStation = resultSet.getString("stop_station");
119                 //取发车时间
120                 String startTime = resultSet.getString("start_time");
121                 //取车票价格
122                 int ticketPrice = resultSet.getInt("ticket_price");
123                   //封装到对象里面去
124                 ticket.setId(id1);
125                 ticket.setStartStation(startStation);
126                 ticket.setStopStation(stopStation);
127                 ticket.setStartTime(startTime);
128                 ticket.setTicketPrice(ticketPrice);
129 
130             }
131 
132             //5.释放资源
133             preparedStatement.close();
134             connection.close();
135         } catch (Exception e) {
136             e.printStackTrace();
137         }
138         return ticket;
139     }
140 
141     @Override
142     public List<Ticket> queryAll() {
143         //准备一个对象,装获取到的结果数据
144         //Ticket ticket = new Ticket();
145         //准备一个可以装多个对象的对象来装多个车票对象==>集合对象List
146         List<Ticket> list=new ArrayList<>();
147         //list.add(); 添加
148         //list.get(); 取
149         try {
150 
151             //1.加载
152             Class.forName("com.mysql.jdbc.Driver");
153             //2.链接
154             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
155             //3.创建编译语句
156             String sql="SELECT id,start_station,stop_station,start_time,ticket_price FROM ticket";
157             PreparedStatement preparedStatement = connection.prepareStatement(sql);
158             //设置参数
159 
160             //4.执行
161             ResultSet resultSet = preparedStatement.executeQuery();
162             //解析结果集
163             while (resultSet.next()){
164                 //取id
165                 int id1 = resultSet.getInt("id");
166                 //取开始车站
167                 String startStation = resultSet.getString("start_station");
168                 //取到达车站
169                 String stopStation = resultSet.getString("stop_station");
170                 //取发车时间
171                 String startTime = resultSet.getString("start_time");
172                 //取车票价格
173                 int ticketPrice = resultSet.getInt("ticket_price");
174                 //封装到对象里面去
175                 Ticket ticket = new Ticket();
176                 ticket.setId(id1);
177                 ticket.setStartStation(startStation);
178                 ticket.setStopStation(stopStation);
179                 ticket.setStartTime(startTime);
180                 ticket.setTicketPrice(ticketPrice);
181 
182                 //将车票对象装入集合对象里面
183                 list.add(ticket);
184 
185             }
186 
187             //5.释放资源
188             preparedStatement.close();
189             connection.close();
190         } catch (Exception e) {
191             e.printStackTrace();
192         }
193         return list;
194     }
195 }
TicketDao

  4.dao测试

 1 package com.day03.station.testDao;
 2 
 3 import com.day03.station.dao.impl.TicketDao;
 4 import com.day03.station.model.Ticket;
 5 import org.junit.Test;
 6 
 7 import java.util.List;
 8 
 9 /**
10  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
11  * 疑问咨询wx:851298348
12  */
13 public class TestTicketDao {
14     TicketDao ticketDao = new TicketDao();
15     //测试车票保存
16     @Test
17     public void testSave(){
18         //1.创建dao对象
19         //TicketDao ticketDao = new TicketDao();
20         //2.调用dao方法
21         Ticket ticket = new Ticket();
22             //赋值
23         ticket.setStartStation("南充");
24         ticket.setStopStation("成都");
25         ticket.setStartTime("2018-05-20 13:14:00");
26         ticket.setTicketPrice(99);
27         ticketDao.save(ticket);
28     }
29     @Test
30     public void testDelete(){
31         ticketDao.delete(5);
32     }
33     @Test
34     public void testUpdate(){
35         Ticket ticket = new Ticket();
36            ticket.setTicketPrice(120);
37             ticket.setId(2);
38         ticketDao.update(ticket);
39     }
40     @Test
41     public void testQueryById(){
42         Ticket ticket = ticketDao.queryById(2);
43         System.out.println("ticket="+ticket);
44     }
45     @Test
46     public void testQuery(){
47         List<Ticket> tickets = ticketDao.queryAll();
48         System.out.println("tickets="+tickets);
49 
50     }
51 
52 }
TestTicketDao

  5.service接口

 1 package com.day03.station.service;
 2 
 3 import com.day03.station.model.Ticket;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
 9  * 疑问咨询wx:851298348
10  */
11 public interface ITicketService {
12     //增加
13     public void save(Ticket ticket);
14     //删除  根据id删除
15     public void delete(Integer id);
16 
17     //修改
18     public void update(Ticket ticket);
19     //查找   查单个  根据id查询
20     public Ticket queryById(Integer id);
21     //查找多个  (集合装)List
22     public List<Ticket> queryAll();
23 }
ITicketService

  6.service实现

 1 package com.day03.station.service.impl;
 2 
 3 import com.day03.station.dao.impl.TicketDao;
 4 import com.day03.station.model.Ticket;
 5 import com.day03.station.service.ITicketService;
 6 
 7 import java.util.List;
 8 
 9 /**
10  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
11  * 疑问咨询wx:851298348
12  */
13 public class TicketService implements ITicketService {
14     TicketDao ticketDao = new TicketDao();
15 
16     @Override
17     public void save(Ticket ticket) {
18         //1.创建dao对象
19         //TicketDao ticketDao = new TicketDao();
20         //2.调用dao方法
21         ticketDao.save(ticket);
22     }
23 
24     @Override
25     public void delete(Integer id) {
26         ticketDao.delete(id);
27     }
28 
29     @Override
30     public void update(Ticket ticket) {
31      ticketDao.update(ticket);
32     }
33 
34     @Override
35     public Ticket queryById(Integer id) {
36         Ticket ticket = ticketDao.queryById(id);
37 
38         return ticket;
39     }
40 
41     @Override
42     public List<Ticket> queryAll() {
43         List<Ticket> tickets = ticketDao.queryAll();
44 
45         return tickets;
46     }
47 }
TicketService

   7.项目完成后的结构

  

posted @ 2018-05-02 17:47  李东平|一线码农  阅读(670)  评论(0编辑  收藏  举报