zoukankan      html  css  js  c++  java
  • JavaSwing 船只停靠管理可视化(四)

    JavaSwing 船只停靠管理可视化(一) 

    JavaSwing 船只停靠管理可视化(二) 

    JavaSwing 船只停靠管理可视化(三) 

    JavaSwing 船只停靠管理可视化(四) 

    JavaSwing 船只停靠管理可视化(五) 

    项目源码 :https://github.com/Wo-com/ShipPort

    如果觉得不错的话就在GitHub里面给个Star吧

    JavaSwing 船只停靠管理可视化

    项目目录:

    工具类主要是

    consql包里面的 Dao类,用于连接数据库。

    tool包里面的Appoint类用于船只 指定港口。

    tool包里面的DateFormat类用于时间计算。

     

    引入连接mysql数据库 jar包

    下载地址:https://dev.mysql.com/downloads/connector/j/

     建议下载zip版    

    解压后导入里面的jia包

    Dao类源码:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    
    public class Dao {
        private Connection conn = null;
        PreparedStatement statement = null;
    
        // connect to MySQL
        public void connSQL() {
            String url = "jdbc:mysql://localhost:3306/ShipPort?useUnicode=true&characterEncoding=utf-8&useSSL=false";
            String username = "root";
            String password = "zgx1734475W"; // 加载驱动程序以连接数据库 
            try { 
                Class.forName("com.mysql.cj.jdbc.Driver" ); 
                conn = DriverManager.getConnection( url,username, password ); 
                }
            //捕获加载驱动程序异常
             catch ( ClassNotFoundException cnfex ) {
                 System.err.println(
                 "装载 JDBC/ODBC 驱动程序失败。" );
                 cnfex.printStackTrace(); 
             } 
             //捕获连接数据库异常
             catch ( SQLException sqlex ) {
                 System.err.println( "无法连接数据库" );
                 sqlex.printStackTrace(); 
             }
        }
    
        // 断开连接 MySQL
        public void deconnSQL() {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                System.out.println("关闭数据库问题 :");
                e.printStackTrace();
            }
        }
    
        // 查询语句
        public ResultSet selectSQL(String sql) {
            ResultSet rs = null;
            try {
                statement = conn.prepareStatement(sql);
                rs = statement.executeQuery(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }
    
        // 插入语句
        public boolean insertSQL(String sql) {
            try {
                statement = conn.prepareStatement(sql);
                statement.executeUpdate();
                return true;
            } catch (SQLException e) {
                System.out.println("插入数据库时出错:");
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println("插入时出错:");
                e.printStackTrace();
            }
            return false;
        }
        //删除语句
        public boolean deleteSQL(String sql) {
            try {
                statement = conn.prepareStatement(sql);
                statement.executeUpdate();
                return true;
            } catch (SQLException e) {
                System.out.println("删除数据库时出错:");
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println("删除时出错:");
                e.printStackTrace();
            }
            return false;
        }
        //更新语句
        public boolean updateSQL(String sql) {
            try {
                statement = conn.prepareStatement(sql);
                statement.executeUpdate();
                return true;
            } catch (SQLException e) {
                System.out.println("更新数据库时出错:");
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println("更新时出错:");
                e.printStackTrace();
            }
            return false;
        }
        
        
        public static void main(String args[]) {
    
            Dao db = new Dao();
            db.connSQL();
    //        String sql1 = "select * from port";
     //       String sql2 = "insert into port(id,name) values("+2222222+",'mmmmm')";
     //       String sql3 = "delete from port where id='20190101'";
    //        String sql4 = "update ship set name='bbbb' where id='3453';";
            
           // String sql5="insert into portship(shipid,arrive,leaves,portid) values('222','2012-12-12 01:12:11','2012-12-12 01:12:11','5')";
        //    String sql5="select * from ship where id='2222';";
    //        String sql6="select recent form port where id='333';";
            
            String min_time="select * from portship where leaves<='2019-06-15 01:12:11';";
            ResultSet rs_min=db.selectSQL(min_time);    
            try{
            while(rs_min.next()) {                                //第一行 获取最近停靠时间
                System.out.println(rs_min.getString(1));
            }
            }catch(Exception e){
                System.out.println("查询出现max min");
            }
            System.out.println("查询完成");
        
           
            db.deconnSQL();//关闭连接
        }
    }

    Appoint类源码:

    import java.sql.ResultSet;
    import consql.Dao;
    
    public class Appoint {
        Dao db = new Dao();
        
        public Appoint(){
            System.out.println("--开始指定位置--");
        }
    
        public boolean human_appoint(String shipid,String portid){
            try{    
                db.connSQL();        //连接数据库
    
                //获取船只到达离开时间
                String sql1="select * from ship where id='"+shipid+"';";
                System.out.println(sql1);
                ResultSet rs2=db.selectSQL(sql1);
                String ar = null;
                String lv = null;
                if(rs2.first()) {                                //第一行 获取最近停靠时间
                    System.out.println(rs2.getString("leaves"));
                    ar=rs2.getString("arrive");
                    lv=rs2.getString("leaves");
                }
            //    String sql4="insert into portship(shipid,arrive,leaves,portid) values('"+shipid+"','"+ar+"','"+lv+"','"+portid+"')";
                int ptid=Integer.parseInt(portid);
                update(shipid,ar,lv,ptid);
                
                   System.out.print("手动指定完成");
            }catch(Exception e1){
                System.out.print("手动指定,出错");
                db.deconnSQL();        //断开连接
                return false;
            }
            db.deconnSQL();
            return true;            //断开连接
        }
        
        public boolean auto_appoint(String shipid){
            try{    
                db.connSQL();        //连接数据库      
                //获取船只 偏好  到达时间  离开时间
                String sql1="select * from ship where id='"+shipid+"';";
                ResultSet rs1=db.selectSQL(sql1);
                String per = null;
                
                String one_ar = null;
                String one_lv = null;
                if(rs1.first()) {                                //只有一行 
                    per=rs1.getString("perfer");
                    one_ar=rs1.getString("arrive");
                    one_lv=rs1.getString("leaves");
                }    
                int one_per=Integer.parseInt(per);
                //查询偏好处有没有 时间冲突
                String sql2="select * from portship where portid='"+one_per+"';";//查询已经停靠在 偏好位置的船只    
                System.out.println("查看偏好处有没有空"); 
                boolean add_one=conflict(shipid,one_ar,one_lv,one_per,sql2);//在偏好位置添加成功则退出
                if(!add_one){
                    System.out.println("查看偏好处没有空 查看其他位置"); 
                    String sql3="select * from port;";
                    ResultSet rs3=db.selectSQL(sql3);
                    rs3.last() ; int port_row = rs3.getRow(); rs3.beforeFirst();//光标回滚  获取行数  光标回滚
                    int portid=0;
                    int i=0;
                    boolean add_two=false;                    //如果在其他位置 可以添加则 添加之后退出
                    while(rs3.next()&&(!add_two)){
                        i++;
                        portid=rs3.getInt("num_add");
                        String sql4="select * from portship where portid='"+portid+"';";//查询已经停靠在 偏好位置的船只    
                        add_two=conflict(shipid,one_ar,one_lv,portid,sql4);
                        if(i==port_row&&(!add_two)){                //查找所有位置没空位时
                            db.deconnSQL();        //断开连接
                            return false;
                        }
                    }
                }
     
                   System.out.println("自动分配结束");
            }catch(Exception e1){
                db.deconnSQL();        //断开连接
                System.out.println("自动分配,出错");
            }
            db.deconnSQL();        //断开连接
            return true;
        }
        boolean conflict(String shipid,String one_ar,String one_lv,int one_per,String sql){
            try{
                DateFormat da=new DateFormat();
                String all_ar = null;
                String all_lv = null;
                ResultSet rs2=db.selectSQL(sql);
                if(rs2.next()){ 
                    System.out.println("港口不为空"); 
                }else{
                    update(shipid,one_ar,one_lv,one_per);
                    System.out.println("港口"+one_per+"为空可以添加");
                }
                rs2.last() ;
                int row = rs2.getRow();                        //获取行数
                int i=0;                                    //i每比较一次增加1 当等于row时那么 执行到最后一条数据了
                rs2.beforeFirst();//游标归位
                while(rs2.next()) {    //为空不执行  不为空执行
                    all_ar=rs2.getString("arrive");
                    all_lv=rs2.getString("leaves");
                    int num=i+1;
                    System.out.println("港口"+one_per+"不为空 判定时间是否冲突"+num+"次");//时间段(a-b)时间段(c-d)  不相交条件: (b<c)||(d<a)
                    int dtime1=da.dateDiff(all_lv,one_ar);    //船只到达时间 与  已经停靠离开时间做时间差
                    int dtime2=da.dateDiff(one_lv,all_ar);    //已经停靠到达时间做时间差 与 船只离开时间
                    if((dtime1>0)||(dtime2>0)){
                        i++;
                        if(i==row){                            //直到最后一条数据都不冲突时  那么可以添加
                            update(shipid,one_ar,one_lv,one_per);
                            System.out.println("港口"+one_per+"不为空 时间不冲突");
                        }
                    }else{
                        
                        System.out.println("港口"+one_per+"不为空 时间冲突");    
                        return false;
                    }
                    
                }
                
            }catch(Exception e){
                db.deconnSQL();        //断开连接
                System.out.println("confict判定错误xxxx");
            }
            return true;
        }
        
        void update(String shipid,String ar,String lv,int per){//执行插入操作(为船只安排泊位)
                    
            //    String sql5 = "select * from portship where shipid='"+shipid+"';";
    
            //把港口编号存入船只表中
            String sql6 = "update ship set site='"+per+"' where id='"+shipid+"';";
            db.updateSQL(sql6);
                
            //插入数据到portship中,作为服务记录
            //String sql4="insert into portship(shipid,arrive,leaves,portid) values('222','2012-12-12 01:12:11','2012-12-12 01:12:11','5')";
            String sql7="insert into portship(shipid,arrive,leaves,portid) values('"+shipid+"','"+ar+"','"+lv+"','"+per+"')";
            db.insertSQL(sql7);
            
    
        }
        
    }

    DateFormat类源码:

    import java.text.SimpleDateFormat;
    
    
    /**
     * 用于计算 时间间隔
     */
    public class DateFormat {
    
        public static void main(String[] args) {
     
            
            
            try {
          //      long min = dateDiff("2014-05-27 13:30:00","2014-05-27 13:00:00");
          //      System.out.println("---------相隔分钟数: "+min);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        
        
        public int dateDiff(String startTime, String endTime) throws Exception {
            String format="yyyy-MM-dd HH:mm:ss";
            //按照传入的格式生成一个simpledateformate对象
            SimpleDateFormat sd = new SimpleDateFormat(format);
            long nd = 1000*24*60*60;//一天的毫秒数
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 1000*60;//一分钟的毫秒数
      //      long ns = 1000;//一秒钟的毫秒数
            long diff;
            //获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
           long day = diff/nd;//计算差多少天
            long hour = diff%nd/nh;//计算差多少小时
            long min = diff%nd%nh/nm;//计算差多少分钟
      //      long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
      //     System.out.println("时间相差:"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
           int bet=(int) (day*24+hour+min/60);
           
            return bet ;
        }
      
    }

    至此项目源码已经介绍完成了,接下来介绍数据库设计;JavaSwing 船只停靠管理可视化(五) 

  • 相关阅读:
    《java并发编程实战》读书笔记9--并发程序的测试
    《java并发编程实战》读书笔记8--死锁,性能与可伸缩性,锁粒度锁分解锁分段
    笔试算法题记录1
    _stdcall调用
    通信设备硬件工程师应该具备的基本能力和知识
    PCB产业链、材料、工艺流程详解(1)
    PCB中加入任意LOGO图文说明 精心制作
    开关电源基础知识(一)
    六个框架,一百多条检查项目,保证PCB设计不再出错
    开关电源PCB排版,基本要点分析
  • 原文地址:https://www.cnblogs.com/easyidea/p/11025569.html
Copyright © 2011-2022 走看看