如果 一个程序员要记录自己上班工作时间的话 ,还需要靠手动去记录, 那就有点 不够范了, 程序员自然要有自己的极客范儿 , 下面就跟我一起来(zhuangbi);

先列一下整体的步骤:

1: 先做一个jar文件, 通过执行这个jar文件可以将上下班时间 写入数据库中;

2: 再写一个批处理脚本,能通过批处理命令来执行这个jar文件;

3: 将批处理文件做开机启动执行处理;

1: 先写一个可执行jar:

原理: 通过jdbc操作数据库,

那就上代码吧:

项目目录结构:

pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.etoak</groupId>
<artifactId>daka</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties> <dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<dependencies>
<!-- 解决sun base64编译报错 -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--主类入口的路径-->
<mainClass>com.etoak.Daka</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
PropertiesUtil 类 :

package com.etoak.util;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties; public class PropertiesUtil { /**
* 根据属性文件路径获取所有的属性键值对
* @param propertiesFilePath
* @return
* @throws IOException
*/
public static Properties getProperties(String propertiesFilePath) throws IOException {
Properties props = new Properties();
InputStream in = null;
try {
//第一种,通过类加载器进行获取properties文件流
in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFilePath);
//第二种,通过类进行获取properties文件流
//in = PropertiesFileUtil.class.getResourceAsStream("/"+propertiesFilePath);
props.load(new InputStreamReader(in, "utf-8"));
} finally {
if (null != in) {
in.close();
}
}
return props;
} /**
* 根据属性文件路径和属性的键获取属性的值
* @param propertiesFilePath
* @param key
* @return
* @throws IOException
*/
public static String getProperty(String propertiesFilePath, String key) throws IOException {
Properties props = getProperties(propertiesFilePath);
return props.getProperty(key);
} /**
* 写Properties文件
*/
public static void writePropertiesFile(Map<String,String> propsMap , String outputPropertiesFilePath) throws Exception { if(propsMap.isEmpty() || propsMap.containsKey("") ){
throw new Exception("传入的键值对不能为空且键值对的主键不能包含空!");
} if( !outputPropertiesFilePath.endsWith(".properties")){
throw new Exception("输出文件路径需要以.properties为结尾!");
} //文件路径的文件夹不存在则创建
String fileSeparator = File.separator;
String dirPath = outputPropertiesFilePath.substring(0,outputPropertiesFilePath.lastIndexOf(fileSeparator)) ;
if( !new File(dirPath).exists()){
new File(dirPath).mkdirs();
} Properties prop = new Properties() ;
Iterator<Map.Entry<String, String>> it = propsMap.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
prop.setProperty( entry.getKey(),entry.getValue());
} FileOutputStream oFile = null ;
try {
//保存属性到b.properties文件
oFile = new FileOutputStream(outputPropertiesFilePath, false);//true表示追加打开,false每次都是清空再重写
//prop.store(oFile, "此参数是保存生成properties文件中第一行的注释说明文字");//这个会两个地方乱码
//prop.store(new OutputStreamWriter(oFile, "utf-8"), "汉字乱码");//这个就是生成的properties文件中第一行的注释文字乱码
prop.store(new OutputStreamWriter(oFile, "utf-8"), null);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oFile != null ) {
oFile.close();
}
}
} public static void main(String[] args) throws Exception {
String propertiesFilePath = "properties/userinfo.properties";
Properties props = getProperties(propertiesFilePath);
Iterator<String> it = props.stringPropertyNames().iterator();
Map<String,String> propsMap = new HashMap<String,String>();
while (it.hasNext()) {
String key = it.next();
String value = props.getProperty(key) ;
System.out.println(key + ":" + value);
propsMap.put(key,value);
}
writePropertiesFile(propsMap,"C:\\Users\\Administrator\\Desktop\\properties\\userinfo2.properties"); }
}
 
ConnectionUtil 类:
package com.etoak.util;

import java.io.IOException;
import java.sql.*;
import java.util.Properties; public class ConnectionUtil { //获得链接
public static Connection getConn(String propertiesFilePath) throws ClassNotFoundException, SQLException, IOException {
Properties props = PropertiesUtil.getProperties(propertiesFilePath);
Class.forName(props.getProperty("driver"));
return DriverManager.getConnection(props.getProperty("url"), props.getProperty("user"), props.getProperty("password"));
} //释放链接
public static void release(Connection conn, Statement st, PreparedStatement pst, ResultSet rs) throws SQLException {
if (rs != null) {
try {
rs.close();
} finally {
try {
if (st != null) {
st.close();
} else if (pst != null) {
pst.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
}
}

Daka类:

package com.etoak;

import com.etoak.util.ConnectionUtil;
import com.etoak.util.PropertiesUtil; import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar; public class Daka { public static void daka(String jdbcPropertiesFilePath, String userinfoPropertiesFilePath) throws SQLException, ClassNotFoundException, IOException { //用户名
String name = PropertiesUtil.getProperty(userinfoPropertiesFilePath, "username");
String[] xqArr = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar calendar = Calendar.getInstance();//可以对每个时间域单独修改
int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
String xq = xqArr[day];
String flag = "0";
int hour = calendar.get(Calendar.HOUR_OF_DAY); if (day == 0 || day == 6) {
flag = "3"; //周末加班
} else {
if (hour <= 19) {
flag = "1"; // 平时正常点上下班
} else {
flag = "2"; // 平时加班
}
} /* 持久化到数据库中*/
Connection conn = ConnectionUtil.getConn(jdbcPropertiesFilePath);
String insertSql = "insert into WORKTIMERECODE (recordId , name , rq , xq , flag , workstartTime , workendTime ) select " +
" sys_guid() , '" + name + "' , trunc(sysdate,'dd') , '" + xq + "' , '" + flag + "' , to_char(trunc(sysdate,'mi'),'hh24:mi') , to_char(trunc(sysdate,'mi'),'hh24:mi') from dual ";
String updateSql = "update WORKTIMERECODE set (flag, workendtime ) = ( select '" + flag + "' , to_char(trunc(sysdate,'mi'),'hh24:mi') from dual ) where rq = trunc(sysdate,'dd') "; Statement st = conn.createStatement();
int un = st.executeUpdate(updateSql);
if (un == 0) {
st.executeUpdate(insertSql);
}
ConnectionUtil.release(conn, st, null, null);
} public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
String jdbcPropertiesFilePath = "properties/jdbc.properties";
String userinfoPropertiesFilePath = "properties/userinfo.properties";
daka(jdbcPropertiesFilePath,userinfoPropertiesFilePath);
System.out.println(Calendar.getInstance().getTime() +" SUCCESSED");
} }

执行Maven clean  >> Maven install >> Maven package ;

使用这个带依赖关系的jar包,并更名为daka.jar ;

先使用 cmd 命令行执行一下 ;

执行结果如下, 没有报错说明执行成功了;

2: 写批处理脚本;

echo off
cd/d "C:\Users\Administrator\Desktop"
java -jar daka.jar
pause

上面脚本的注释:

1: echo off  :关闭打印输出;

2: cd/d 打开目录 "C:\Users\Administrator\Desktop"  目录文件路径;

3: java -jar daka.jar  执行 daka.jar 的 cmd命令 ;

4: pause 暂停 ; 不然的话 就会一闪而过 ;

OK  执行结果如下:

3: 再下面一步: 设置成为开机启动时执行:

将脚本放到开机启动的文件夹下即可开机启动:

如果找不到路径 有肯能是隐藏了; 需要显示隐藏文件;

下面是源代码:

https://pan.baidu.com/s/1dE3OGUX

jjs9

上班打卡--- 通过批处理命令执行jar文件来记录上班时间的更多相关文章

  1. java命令执行jar文件

    如果java -jar target/hbase-demo-1.0-SNAPSHOT.jar HBaseDemo 提示如下 no main manifest attribute, in target/ ...

  2. Java:执行jar文件命令

    Java:执行jar文件命令 执行jar文件命令: java -jar test.jar win7系统切换目录命令: cd /d d:/test

  3. SQL Server数据库备份:通过Windows批处理命令执行

    通过Windows批处理命令执行SQL Server数据库备份 建立mybackup.bat ,输入以下内容直接运行该脚本,即可开始自动备份数据库也可把该脚本加入windows任务计划里执行. --- ...

  4. cmd中执行jar文件命令(待参数)

    cmd中执行jar文件命令(待参数) 1,jar文件路径:F:\products 2,cmd命令: --两个日期参数(空格隔开) java -jar F:\products\analysis.jar ...

  5. Java应用程序可执行jar文件与服务器交互中文乱码

    生成可执行jar文件后,直接双击打开应用,发送Http请求带有中文时,服务器接收到的中文乱码! 解决方式: 1.在cmd命令中执行javaw命令打开jar可执行应用: 打开cmd命令框,输入: jav ...

  6. maven打包可执行jar文件运行报错

    起因 项目中同时依赖了Spring和MyBatis,并使用mybatis-spring集成MyBatis和Spring. 使用maven打包为可执行jar文件运行,打包插件为:maven-shade- ...

  7. linux怎么执行jar文件 怎么打可执行的jar包

    Linux下执行jar文件方法:命令行下进入文件目录,执行java -jar file.jar即可,也可在桌面创建一个启动器,在命令栏填写相关的命令:java -jar /file路径/file.ja ...

  8. 记录自己在 cmd 中执行 jar 文件遇到的一些错误

    记录自己在 cmd 中执行 jar 文件遇到的一些错误 场景: 请求接口,解析接口返回的 JSON 字符串并插入到我们的数据库里面. 情况: 项目在 eclipse 中正常运行,打成 jar 包后在 ...

  9. 利用osql/ocmd批处理批量执行sql文件

    原文:利用osql/ocmd批处理批量执行sql文件 上周在测试环境建了几十张表,保存了.sql文件,准备在正式环境重建的时候懒得一个个打开建了,做一在网上搜寻了一下,果然有简单点的方法. 利用osq ...

随机推荐

  1. LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. [译]ASP.NET Core 2.0 路由引擎之网址生成

    问题 如何在ASP.NET Core 2.0中由路由引擎来生成网址? 答案 新建一个空项目,修改Startup.cs文件,添加MVC服务和中间件: public void ConfigureServi ...

  3. Linux系列教程(十)——Linux文本编辑器vim

    通过前面几篇博客我们终于结束了Linux常用命令的介绍,Linux常用命令主要包括以下: ①.Linux文件和目录处理命令 ②.Linux链接命令和权限管理命令 ③.Linux文件搜索命令 ④.Lin ...

  4. HTTP服务简介

    第1章 HTTP服务介绍 1.1 简述用户访网站流程 a 进行域名信息的DNS解析   dig +trace 获得www.oldboyedu.com  ip地址信息 b 进行与网站服务器建立连接,tc ...

  5. Unity3D手机斗地主游戏开发实战(03)_地主牌显示和出牌逻辑(不定期更新中~~~)

    Hi,之前有同学说要我把源码发出来,那我就把半成品源码的链接放在每篇文件的最后,有兴趣的话可以查阅参考,有问题可以跟我私信,也可以关注我的个人公众号,互相交流嘛.当然,代码也是在不断的持续改进中~ 上 ...

  6. JavaScript系列----一切皆是对象

    1.判断对象类型 1.1.typeof 运算符 首先要认识到,typepof是一个运算符,其运算需要一个参数,返回值是参数的类型. typeof使用方法 typeof parameter //使用方法 ...

  7. 【机器学习实战】第15章 大数据与MapReduce

    第15章 大数据与MapReduce 大数据 概述 大数据: 收集到的数据已经远远超出了我们的处理能力. 大数据 场景 假如你为一家网络购物商店工作,很多用户访问该网站,其中有些人会购买商品,有些人则 ...

  8. 如何优雅的设计React组件

    如何优雅的设计 React 组件 如今的 web 前端已被 React.Vue 和 Angular 三分天下,一统江山十几年的 jQuery 显然已经很难满足现在的开发模式.那么,为什么大家会觉得 j ...

  9. 《Python数据分析常用手册》一、NumPy和Pandas篇

    一.常用链接: 1.Python官网:https://www.python.org/ 2.各种库的whl离线安装包:http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...

  10. php 常用 常量集合

    DIRECTORY_SEPARATOR 常量 DIRECTORY_SEPARATOR  目录分割符