做个简单的Java学生考勤系统02--工具类
2021/10/30 1:10:58
本文主要是介绍做个简单的Java学生考勤系统02--工具类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
工欲善其事,必先利其器。完成Java学生考勤的下一步是要有其相关的工具类作为考勤系统的辅助工具,下来列出有哪些相关的工具类。
工具类:
1、DB工具类
是连接数据库的工具类,数据库的用户名,密码,数据库驱动名称,url(JDBC+数据库库名的地址)。将数据保存到数据库里,也可以从数据库读取数据
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DB { private Connection con; private PreparedStatement pstm; private String user = "root"; private String password = "123456"; private String className = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/db_kaoqin"; public DB() { try { Class.forName(className); } catch (ClassNotFoundException e) { System.out.println("加载数据库驱动失败!"); e.printStackTrace(); } } /** 创建数据库连接 */ public Connection getCon() { try { con = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("创建数据库连接失败!"); con = null; e.printStackTrace(); } return con; } public void doPstm(String sql, Object[] params) { if (sql != null && !sql.equals("")) { if (params == null) params = new Object[0]; getCon(); if (con != null) { try { System.out.println(sql); pstm = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int i = 0; i < params.length; i++) { pstm.setObject(i + 1, params[i]); } pstm.execute(); } catch (SQLException e) { System.out.println("doPstm()方法出错!"); e.printStackTrace(); } } } } public ResultSet getRs() throws SQLException { return pstm.getResultSet(); } public int getCount() throws SQLException { return pstm.getUpdateCount(); } public void closed() { try { if (pstm != null) pstm.close(); } catch (SQLException e) { System.out.println("关闭pstm对象失败!"); e.printStackTrace(); } try { if (con != null) { con.close(); } } catch (SQLException e) { System.out.println("关闭con对象失败!"); e.printStackTrace(); } } }
2、配置filter
配置EncodingFilter过滤器,用来处理请求编码问题,解决Java中文乱码问题。
package util; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class EncodingFilter implements Filter { protected String encoding = ""; protected FilterConfig filterConfig = null; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Select and set (if needed) the character encoding to be used String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); } // Pass control on to the next filter chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }
3、其他工具类
包含了时间转换,截取时间,将字符串截短等方法
package util; import java.text.ParseException; import java.util.Date; import java.util.GregorianCalendar; public class Util { public static Date newDate(String s) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "yyyy-MM-dd"); Date date = new Date(); date = sdf.parse(s); return date; } public static Date newDate1(String s) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm"); Date date = new Date(); date = sdf.parse(s); return date; } public static Date FormatFullDate(String s) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = new Date(); date = sdf.parse(s); return date; } public static String splitDate(Date d) { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "yyyy-MM-dd"); return sdf.format(d); } public static String splitDate1(Date d) { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "yyyy年MM月dd日"); return sdf.format(d); } /** * 将字符串截短,取前n个字符,英文算半个字符。 * * @param orignalString * 原字符串 * @param length * 长度 * @param chopedString * 超过部分的表示字符串 * @return 截取的字符串 */ public static String chop(String orignalString, double length, String chopedString) { if (orignalString == null || orignalString.length() == 0) { return orignalString; } orignalString = orignalString.replaceAll(" ", " "); if (orignalString.length() < length) { return orignalString; } StringBuffer buffer = new StringBuffer((int) length); length = length * 2; int count = 0; int stringLength = orignalString.length(); int i = 0; for (; count < length && i < stringLength; i++) { char c = orignalString.charAt(i); if (c < '\u00ff') { count++; } else { count += 2; } buffer.append(c); } if (i < stringLength) { buffer.append(chopedString); } return buffer.toString(); } public static long getPrimeKey() { GregorianCalendar calendar = new GregorianCalendar(); return calendar.getTimeInMillis(); } public static long stringToLong(String source) { return Long.parseLong(source); } }
这篇关于做个简单的Java学生考勤系统02--工具类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-04敏捷管理与看板工具:提升研发、设计、电商团队工作效率的利器
- 2025-01-04智慧养老管理工具如何重塑养老生态?
- 2025-01-04如何打造高绩效销售团队:工具与管理方法的结合
- 2025-01-04解决电商团队协作难题,在线文档工具助力高效沟通
- 2025-01-04春节超市管理工具:解锁高效运营与顾客满意度的双重密码
- 2025-01-046种主流销售预测模型:如何根据场景选用最佳方案
- 2025-01-04外贸服务透明化:增强客户信任与合作的最佳实践
- 2025-01-04重新定义电商团队协作:在线文档工具的战略作用
- 2025-01-04Easysearch Java SDK 2.0.x 使用指南(三)
- 2025-01-04百万架构师第八课:设计模式:设计模式容易混淆的几个对比|JavaGuide