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 船只停靠管理可视化(五)

JavaSwing 船只停靠管理可视化(四)的更多相关文章

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

    JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...

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

    JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...

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

    JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...

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

    最近抽空闲时间做了船只停靠管理系统,先看一下效果. 停靠泊位管理:实现泊位的 增删改查. JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing ...

  5. Java-Swing常用布局管理器

    http://www.cnblogs.com/hthuang/p/3460234.html   5.Java-Swing常用布局管理器       应用布局管理器都属于相对布局,各组件位置可随界面大小 ...

  6. dock停靠管理器

    DockManager停靠管理器可以对它所拥有的 停靠面板 的行为和外观设置进行集中控制.DockPanel停靠面板是停靠应用程序的主要构成部件. 常规面板 DockPanel.ParentPanel ...

  7. 使用Visual Studio Team Services敏捷规划和项目组合管理(四)——冲刺计划和任务板

    使用Visual Studio Team Services敏捷规划和项目组合管理(四)--冲刺计划和任务板 团队在sprint计划会议期间创建冲刺积压工作项,通常在冲刺的第一天召开该会议.每个冲刺都对 ...

  8. Spring事务管理的四种方式(以银行转账为例)

    Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败.   原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...

  9. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之五(四十一)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

随机推荐

  1. SQL优化之SELECT COUNT(*)

    前言 SQL优化之SQL 进阶技巧(上) SQL优化之SQL 进阶技巧(下)中提到使用以下 sql 会导致慢查询 SELECT COUNT(*) FROM SomeTable SELECT COUNT ...

  2. Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析

    Python使用property函数和使用@property装饰器都能定义属性的get.set及delete的访问方法,他们的相同点主要如下三点: 1.定义这些方法后,代码中对相关属性的访问实际上都会 ...

  3. 第10.9节 Python子包的导入方式介绍

    在<第10.8节 Python包的导入方式详解>详细介绍了包的导入方式,子包也是包,子包的导入与包的导入方法上没有本质区别,但二者还是有所不同.本节对照二者的方式介绍子包与包导入的关系: ...

  4. 第15.17节 PyQt(Python+Qt)入门学习:PyQt图形界面应用程序的事件捕获方法大全及对比分析

    老猿Python博文目录 老猿Python博客地址 按照老猿规划的章节安排,信号和槽之后应该介绍事件,但事件在前面的随笔<PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法 ...

  5. HashMap稍微详细的理解

    此文章用来记录hashmap的一些特点(在学习中的所了解的,如有不足,请指正) 什么是hash表 概念 先来一段百度百科的的解释 散列表(Hash table,也叫哈希表),是根据关键码值(Key v ...

  6. 十. Axios网络请求封装

    1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...

  7. 题解-比赛CF1332

    题解-比赛CF1332 比赛CF1332 [A] [B] [C] [D] [E] [F] [G] [A]Exercising Walk Exercising Walk \(T\) 组测试数据,每次给定 ...

  8. Zabbix自定义模板监控多个url接口

    一.脚本配置 1.监控脚本 /etc/zabbix/zabbix_agent2.d/scripts/web_site_code_status.sh #!/bin/bash url_discovery( ...

  9. sql语句执行次序

    from→on→join→where→group by→having→select→distinct→order by→limit

  10. 七牛云SDKLinux环境下C SDK的编译(转)

    1.下载代码到本地 git clone https://github.com/qiniu/c-sdk.git 如果国外下载速度慢,可以用码云的镜像库 git clone https://gitee.c ...