每日总结13
2023-04-18 15:51:17 时间
今天学写了如何做地铁线路图,了解到了怎么从文档中把表数据导入进去,很方便
![](https://img2023.cnblogs.com/blog/2913343/202303/2913343-20230313202124107-400523341.jpg)
package bean; import java.util.ArrayList; import java.util.List; public class station { private int id; private String station; private String line; //该站点是否支持换乘 private Boolean isTransferStation; //可换成线路 private List<String> lines = new ArrayList<>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStation() { return station; } public void setStation(String station) { this.station = station; } public String getLine() { return line; } public void setLine(String line) { this.line = line; } public Boolean getTransferStation() { return isTransferStation; } public void setTransferStation(Boolean transferStation) { isTransferStation = transferStation; } public List<String> getLines() { return lines; } public void setLines(List<String> lines) { this.lines = lines; } }
package dao; import bean.station; import util.util; import java.sql.*; import java.util.ArrayList; import java.util.List; public class dao { public List<station> readStation(String a) { List<station> list = new ArrayList<station>(); Connection con=util.getConnection(); ResultSet rs=null; String sql=null; switch(a){ case "1": sql="select * from line1";break; case "2": sql="select * from line2";break; case "4": sql="select * from line4";break; case "5": sql="select * from line5";break; case "6": sql="select * from line6";break; case "7": sql="select * from line7";break; case "8": sql="select * from line8";break; case "9": sql="select * from line9";break; case "10": sql="select * from line10";break; case "13": sql="select * from line13";break; case "14": sql="select * from line14";break; case "15": sql="select * from line15";break; } try {//连接数据库的操作 Statement statement=con.createStatement(); rs=statement.executeQuery(sql); while (rs.next()) { station s = new station(); s.setId(rs.getInt("id")); s.setStation(rs.getString("station")); list.add(s); } util.close(rs, statement, con); }catch(Exception e){ e.printStackTrace(); } return list; } public static int search_bukeline(String station){ int line =0; try { Connection conn = util.getConnection(); Statement state = null; String sql="select * from buke where station=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,station); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ line= Integer.parseInt((rs.getString("line"))); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } return line; } public static int search_keline1(String station){ int line1 =0; try { Connection conn = util.getConnection(); Statement state = null; String sql="select * from ke where station=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,station); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ line1= Integer.parseInt((rs.getString("line1"))); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } return line1; } public static int search_keline2(String station){ int line2 =0; try { Connection conn = util.getConnection(); Statement state = null; String sql="select * from ke where station=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,station); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ line2= Integer.parseInt((rs.getString("line2"))); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } return line2; } //判断站点是否可以换乘 public static boolean judgestation(String station){ boolean flag=true; int line =0; try { Connection conn = util.getConnection(); Statement state = null; String sql="select * from buke where station=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,station); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ line= Integer.parseInt((rs.getString("line"))); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } if(line==0){ flag=true; } else{ flag=false; } return flag; } //判断两站点的线路是否相同 public static boolean judgeline(String start, String stop, boolean f1, boolean f2) { boolean flag = true; if (!f1 && !f2) { int line1 = search_bukeline(start); int line2 = search_bukeline(stop); if (line1 == line2) { flag=true; } else { flag=false; } } else if(!f1&&f2){ int line = search_bukeline(start); int line1 = search_keline1(stop); int line2 = search_keline2(stop); if(line==line1||line==line2){ flag=true; } else{ flag=false; } } else if(f1&&!f2){ int line = search_bukeline(stop); int line1 = search_keline1(start); int line2 = search_keline2(start); if(line==line1||line==line2){ flag=true; } else{ flag=false; } } else if(f1&&f2){ int line1 = search_keline1(start); int line2 = search_keline2(start); int line3 = search_keline1(start); int line4 = search_keline2(start); if(line1==line3||line1==line4||line2==line3||line2==line4){ flag=true; } else{ flag=false; } } return flag; } //查询站点所对应线路的id public static int readId(int a,String station) { int id =0; Connection conn = util.getConnection(); String sql = null; switch (a) { case 1: sql = "select * from line1 where station=?"; break; case 2: sql = "select * from line2 where station=?"; break; case 4: sql = "select * from line4 where station=?"; break; case 5: sql = "select * from line5 where station=?"; break; case 6: sql = "select * from line6 where station=?"; break; case 7: sql = "select * from line7 where station=?"; break; case 8: sql = "select * from line8 where station=?"; break; case 9: sql = "select * from line9 where station=?"; break; case 10: sql = "select * from line10 where station=?"; break; case 13: sql = "select * from line13 where station=?"; break; case 14: sql = "select * from line14 where station=?"; break; case 15: sql = "select * from line15 where station=?"; break; } try {//连接数据库的操作 PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, station); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { id = Integer.parseInt((rs.getString("id"))); } rs.close(); pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return id; } //查询同一条线路的站点 public static List<station> readStations(int a, int b, int c) { List<station> list = new ArrayList<station>(); Connection con=util.getConnection(); ResultSet rs=null; String sql=null; switch(a){ case 1: sql = "select * from line1 limit "+b+","+c; break; case 2: sql = "select * from line2 limit "+b+","+c; break; case 4: sql = "select * from line4 limit "+b+","+c; break; case 5: sql = "select * from line5 limit "+b+","+c; break; case 6: sql = "select * from line6 limit "+b+","+c; break; case 7: sql = "select * from line7 limit "+b+","+c; break; case 8: sql = "select * from line8 limit "+b+","+c; break; case 9: sql = "select * from line9 limit "+b+","+c; break; case 10: sql = "select * from line10 limit "+b+","+c; break; case 13: sql = "select * from line13 limit "+b+","+c; break; case 14: sql = "select * from line14 limit "+b+","+c; break; case 15: sql = "select * from line15 limit "+b+","+c; break; } try {//连接数据库的操作 Statement statement=con.createStatement(); rs=statement.executeQuery(sql); while(rs.next()) { station s = new station(); s.setId(rs.getInt("id")); s.setStation(rs.getString("station")); list.add(s); } util.close(rs, statement, con); }catch(Exception e){ e.printStackTrace(); } return list; }
package util; import java.sql.*; public class util { private static String mysqlname = "subway"; private static Connection con; private static Statement sta; private static ResultSet re; private static String coursename = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/" + mysqlname + " ?serverTimezone=GMT%2B8"; public static Connection getConnection() { try { Class.forName(coursename); System.out.println("连接成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url, "root", "123456"); System.out.println("LIANJIE"); } catch (Exception e) { e.printStackTrace(); con = null; } return con; } public static void close(Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
相关文章
- Sybase批量操作的实现
- AI 大战 AI,一个深度强化学习多智能体竞赛系统
- 自己动手从零写桌面操作系统GrapeOS系列教程——10.NASM汇编
- 简易的工厂设计模式
- 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(10)-Charles如何修改请求参数和响应数据-下篇
- JavaMail 邮件发送,有意思的附件名乱码 → 客户端正常,web端乱码
- 「降本」有可能,「增效」不确定
- 配运基础数据缓存瘦身实践
- 我的十年编程路 2018篇
- new bing功能使用
- 【数据结构与算法学习】线性表(顺序表、单链表、双向链表、循环链表)
- C++11 thread_local关键字
- 有关Docker的八个令人难以置信的事实
- 快递大数据让包裹实现全程可视化跟踪
- Nginx如何配置HTTPS详解
- 被骗好多年:原来这才是大数据
- VUE+.NET应用系统的国际化-整体设计思路
- 减少80%存储-风控名单服务重构剖析
- 为什么基于机器学习的产品很难见到?
- 我的十年编程路 2017年篇