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. 3D网页小实验——将txt配置文本转化为3D陈列室

    设计目标:借鉴前辈编程者的经验将简单的配置文本转化为3D场景,并根据配置文件在场景中加入图片和可播放的视频,最终形成可浏览的3D陈列室. 一.使用效果 1.txt配置文件: (博客园的富文本编辑器会改 ...

  2. MacBook键盘锁定、按键失效、无反应等问题

    目录 MacBook按键无反应 u,i,o,j,k,l等键失灵 capslock灯不亮 原文链接:joselynzhao·CSDN·MacBook键盘锁定.按键失效.无反应等问题 MacBook按键无 ...

  3. UnityEditorWindow做一个TimeLine的滑动块

    UnityEditorWindow做一个TimeLine的滑动块 最近在做一个基于TimeLine的动画编辑器,在制作TineLine滑动条时遇到问题,网上查了好久,试了好多GUI组件都不满意.最后在 ...

  4. Cys_Control(二) MButton

    一.添加自定义Button 二.Xaml文件自动关联 Custom Control 取名与资源文件相同加.cs文件将自动关联 Themes文件下Generic.xaml引入该控件,用于对外公布样式 & ...

  5. CAD插件

    CAD插件使用: 1.首先得有插件,插件解压,放那个盘都可以,只要自己觉得放得下,注:(每次打开CAD想要用插件都要的步骤)打开CAD---AP回车----找到插件所在文件夹-------Ctrl+A ...

  6. 安装rose遇到的问题

    将rose文件夹里面的rose.rar直接解压到rose安装目录里面的common文件夹中 安装教程与问题解决连接https://blog.csdn.net/Farewell_w/article/de ...

  7. 第7.22节 Python中使用super调用父类的方法

    第7.22节 Python中使用super调用父类的方法 前面章节很多地方都引入了super方法,这个方法就是访问超类这个类对象的.由于super方法的特殊性,本节单独谈一谈super方法. 一.su ...

  8. 转:Python考核试题及答案

    Python测试(总分:120) 选择题(每题2分,共20分) 1.下列哪个语句在Python中是非法的? (B) A.x = y = z = 1 B.x = (y = z + 1) C.x, y = ...

  9. python基本案例实现

    案例一:test.txt文件中与输入的用户进行认证,超过3次用户被锁定,且把用户加入锁定的lock.txt文件中. # 需求点: # 1.输入用户名.密码 # 2.认证成功后显示欢迎信息 # 3.输错 ...

  10. 5、Spring Cloud Ribbon

    1.Ribbon简介 (1).Ribbon介绍 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. Ribbon是Netflix发布的开源项目,主 ...