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 船只停靠管理可视化(四)的更多相关文章
- JavaSwing 船只停靠管理可视化(五)
JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...
- JavaSwing 船只停靠管理可视化(三)
JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...
- JavaSwing 船只停靠管理可视化(二)
JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwin ...
- JavaSwing 船只停靠管理可视化(一)
最近抽空闲时间做了船只停靠管理系统,先看一下效果. 停靠泊位管理:实现泊位的 增删改查. JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing ...
- Java-Swing常用布局管理器
http://www.cnblogs.com/hthuang/p/3460234.html 5.Java-Swing常用布局管理器 应用布局管理器都属于相对布局,各组件位置可随界面大小 ...
- dock停靠管理器
DockManager停靠管理器可以对它所拥有的 停靠面板 的行为和外观设置进行集中控制.DockPanel停靠面板是停靠应用程序的主要构成部件. 常规面板 DockPanel.ParentPanel ...
- 使用Visual Studio Team Services敏捷规划和项目组合管理(四)——冲刺计划和任务板
使用Visual Studio Team Services敏捷规划和项目组合管理(四)--冲刺计划和任务板 团队在sprint计划会议期间创建冲刺积压工作项,通常在冲刺的第一天召开该会议.每个冲刺都对 ...
- Spring事务管理的四种方式(以银行转账为例)
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之五(四十一)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
随机推荐
- 面经手册 · 第20篇《Thread 线程,状态转换、方法使用、原理分析》
作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...
- 第十一章、Designer中主窗口QMainWindow类
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 主窗口对象是在新建窗口对象时,选择main window类型的模板时创建的窗口对象,如图: ...
- jdk源码之 hashmap 与hashtable 的区别
hashmap hashtable 线程安全 否,但jdk5之后,提供ConcurrentHashMap,可 替代HashTable. 是,synchronized value是否允许为空 是 否 ...
- Win10激活失败并提示错误代码0xC004C003的解决方法
亲测,可用. 步骤如下: 进入cmd,管理员权限登录 slmgr.vbs /upk slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX slmgr /skms zh.us ...
- pandas 由其中几列生成新的列
data是一个dataframe #data["x1"]=data[["a","b"]].apply(lambda x:x["a& ...
- THE BUG 队第一次团队项目作业
队名: THE BUG 队 2.队员学号: 杨梓琦 3118005115(队长) 温海源,3118005109 陈杰才,3118005089 李华,3118005097 钟明康,3118005123 ...
- react+redux项目搭建及示例
React + Redux示例,实现商品增删改 目录结构 1.项目搭建 1.1 使用create-react-app react_redux创建项目 1.2 安装使用redux需要的依赖 npm in ...
- NOIP2020 浙江 游记
day - ? 由于 CSP-S 的失利,感觉这一次 NOIP 的心态反而是非常的淡定,感觉反正已经炸过一次了,再炸一次好像也没什么,就抱着这样的心态去考试的. day 1 考试当天起晚了,到考场的时 ...
- 【Codeforces 715C】Digit Tree(点分治)
Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...
- 题解-洛谷P4724 【模板】三维凸包
洛谷P4724 [模板]三维凸包 给出空间中 \(n\) 个点 \(p_i\),求凸包表面积. 数据范围:\(1\le n\le 2000\). 这篇题解因为是世界上最逊的人写的,所以也会有求凸包体积 ...