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. python删除list中的空list

    list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText'] 如何删除空列表,以便我得到: list2 = ['text', 'text2 ...

  2. 冲刺随笔——Day_Three

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...

  3. MySQL对数据 对表 对数据操作

    ------------恢复内容开始------------ MySQL 数据库的操作 创建 create database 数据库名; 指定字符集 create database 数据库名 char ...

  4. 从JMM透析volatile与synchronized原理,图文并茂

    在面试.并发编程.一些开源框架中总是会遇到 volatile 与 synchronized .synchronized 如何保证并发安全?volatile 语义的内存可见性指的是什么?这其中又跟 JM ...

  5. python核心高级学习总结8------动态性、__slots__、生成器、迭代器、装饰、闭包

    python的动态性 什么是动态性呢,简单地来说就是可以在运行时可以改变其结构,如:新的函数.对象.代码都可以被引进或者修改,除了Python外,还有Ruby.PHP.javascript等也是动态语 ...

  6. moviepy音视频剪辑VideoClip类fl_image方法及参数image_func的功能介绍

    ☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...

  7. 第12.2节 Python sys模块导览

    sys模块包括一些用于系统处理的功能,常用的成员包括: sys.argv:当前执行进程的命令参数列表,不含执行程序本身的名字: sys.stdin .sys.stdout 和 stderr :分别对应 ...

  8. PyQt(Python+Qt)学习随笔:QTreeView树形视图的indentation属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的indentation属性用于控制视图中每级数据项之间的缩进,对于顶级项 ...

  9. vue 中 this.$options.data() 重置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. I am zhoukangyang!

    我是 \(\texttt{zhoukangyang}\),一名来自浙江省,杭州市的初二菜鸡 \(\texttt{oier}\) . 洛谷zhoukangyang 很多东西因为太垃圾所以 了,要开 请洛 ...