上班打卡--- 通过批处理命令执行jar文件来记录上班时间
如果 一个程序员要记录自己上班工作时间的话 ,还需要靠手动去记录, 那就有点 不够范了, 程序员自然要有自己的极客范儿 , 下面就跟我一起来(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