1、无重复字符的最长子串

  1. 输入: "abcabcbb"
  2. 输出: 3
  3. 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3
  1. class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. int max = 0;
  4. int curNoStrIndexStart = 0;
  5. Map<String,Integer> map = new HashMap<>();
  6. for (int i=0;i < s.length();i++) {
  7. String index = String.valueOf(s.charAt(i));
  8. if (map.containsKey(index)) {
  9. curNoStrIndexStart = Math.max(map.get(index),curNoStrIndexStart);
  10. }
  11. max = Math.max(max,i - curNoStrIndexStart +1);
  12. map.put(index,i + 1);
  13. }
  14. return max;
  15. }
  16. }

2、mysql获取表的元数据信息

  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5. public class DBEntityInit {
  6.  
  7. private static String propName = "db.properties";
  8.  
  9. private static Connection connection = null;
  10.  
  11. private static Map<String, List<Column>> tableData = new HashMap<>();
  12.  
  13. static {
  14. connection = getConnection(propName);
  15. }
  16.  
  17. public static Connection getConnection(String propName){
  18. Connection conn = null;
  19. try {
  20. InputStream in = new FileInputStream(new File(propName));
  21. Properties properties = new Properties();
  22. properties.load(in);
  23. String url = properties.getProperty("jdbc.url");
  24. String username = properties.getProperty("jdbc.username");
  25. String driver = properties.getProperty("jdbc.driver");
  26. String password = properties.getProperty("jdbc.password");
  27. Class.forName(driver);
  28. conn = DriverManager.getConnection(url,username,password);
  29. } catch (SQLException | ClassNotFoundException | IOException e) {
  30. e.printStackTrace();
  31. }
  32. return conn;
  33. }
  34.  
  35. public static void initTableData() {
  36. ResultSet tables = null;
  37. try {
  38. DatabaseMetaData metaData = connection.getMetaData();
  39. tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
  40. while (tables.next()) {
  41. tableData.put(tables.getString(3),new ArrayList<>());
  42. }
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. } finally {
  46. try {
  47. if (tables != null) {
  48. tables.close();
  49. }
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55.  
  56. public static void initTableColumn(String tableName) {
  57. List<Column> columns = tableData.get(tableName);
  58. PreparedStatement pst = null;
  59. try {
  60. String sql = "select * from " + tableName;
  61. pst = connection.prepareStatement(sql);
  62. ResultSetMetaData metaData = pst.getMetaData();
  63. int columnCount = metaData.getColumnCount();
  64. for (int i=0 ; i < columnCount; i++) {
  65. Column column = new Column();
  66. column.setName(metaData.getColumnName(i+1));
  67. column.setType(metaData.getColumnTypeName(i+1));
  68. columns.add(column);
  69. }
  70. //tableData.put(tableName,columns);
  71.  
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. } finally {
  75. try {
  76. if (pst != null) {
  77. pst.close();
  78. }
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84.  
  85. public static void initComment(String tableName) {
  86. //与数据库的连接
  87. PreparedStatement pst = null;
  88. String tableSql = "select * from" + tableName;
  89. List<String> comments = new ArrayList<>();//列名注释集合
  90. ResultSet rs = null;
  91. List<Column> columns = tableData.get(tableName);
  92. try {
  93. pst = connection.prepareStatement(tableSql);
  94. rs = pst.executeQuery("show full columns from " + tableName);
  95. while (rs.next()) {
  96. String comment = rs.getString("Comment");
  97. comments.add(comment);
  98. }
  99.  
  100. for (int i=0 ;i < columns.size() ;i++) {
  101. Column column = columns.get(i);
  102. column.setComment(comments.get(i));
  103. }
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. } finally {
  107. try {
  108. if (rs != null) {
  109. rs.close();
  110. }
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. }
  114.  
  115. }
  116. }
  117.  
  118. public static void closeConnection(Connection conn) {
  119. if (conn != null) {
  120. try {
  121. conn.close();
  122. } catch (SQLException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. }
  127.  
  128. public static Map<String, List<Column>> init() {
  129. initTableData();
  130. for (String key : tableData.keySet()) {
  131. initTableColumn(key);
  132. initComment(key);
  133. }
  134. closeConnection(connection);
  135. return tableData;
  136. }
  137.  
  138. public static String getPropName() {
  139. return propName;
  140. }
  141.  
  142. public static void setPropName(String propName) {
  143. DBEntityInit.propName = propName;
  144. }
  145.  
  146. public static void main(String[] args) {
  147.  
  148. init();
  149. for (Map.Entry<String,List<Column>> entry : tableData.entrySet()) {
  150. List<Column> value = entry.getValue();
  151. System.out.println("--------------表名" + entry.getKey());
  152. for (Column column : value) {
  153. System.out.print(column.getName() + "---");
  154. System.out.print(column.getType() + "---");
  155. System.out.print(column.getComment());
  156. System.out.println();
  157. }
  158. }
  159.  
  160. }
  161.  
  162. // public static void initColumnComment(String tableName) {
  163. //
  164. // try {
  165. // List<Column> cList = tableData.get(tableName);
  166. // DatabaseMetaData metaData = connection.getMetaData();
  167. // ResultSet columns = metaData.getColumns(null, null, tableName, "%");
  168. //
  169. // while (columns.next()) {
  170. // Column column = new Column();
  171. // String name = columns.getString("COLUMN_NAME");
  172. // String type = columns.getString("TYPE_NAME");
  173. // String comment = columns.getString("REMARKS");
  174. // column.setName(name);
  175. // column.setType(type);
  176. // column.setComment(comment);
  177. // cList.add(column);
  178. // }
  179. // } catch (SQLException e) {
  180. // e.printStackTrace();
  181. // }
  182. //
  183. // }
  184.  
  185. }

相关实体

  1. public class Column {
  2.  
  3. private String name;
  4. private String type;
  5. private String comment;
  6.  
  7. public String getName() {
  8. return name;
  9. }
  10.  
  11. public void setName(String name) {
  12. name = name;
  13. }
  14.  
  15. public String getType() {
  16. return type;
  17. }
  18.  
  19. public void setType(String type) {
  20. this.type = type;
  21. }
  22.  
  23. public String getComment() {
  24. return comment;
  25. }
  26.  
  27. public void setComment(String comment) {
  28. this.comment = comment;
  29. }
  30. }

3、日期的加减操作

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5.  
  6. public class DateCalculate {
  7.  
  8. private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  9.  
  10. /**
  11. * 获取形如yyyy-MM-dd HH:mm:ss
  12. * @param date
  13. * @return
  14. */
  15. public static String datetimeToString(Date date) {
  16. return sdf.format(date);
  17. }
  18.  
  19. /**
  20. * 根据时间字符串获取日期
  21. * @param dateString
  22. * @return
  23. * @throws ParseException
  24. */
  25. public static Date stringToDatetime(String dateString) throws ParseException {
  26. return sdf.parse(dateString);
  27. }
  28.  
  29. /**
  30. * 获取本月最后一天
  31. * @return
  32. */
  33. public static Date getMonthStartDate(Date date) {
  34. Calendar calendar = Calendar.getInstance();
  35. calendar.setTime(date);
  36. calendar.set(Calendar.DAY_OF_MONTH,1);
  37. return calendar.getTime();
  38. }
  39.  
  40. /**
  41. * 获取本月最后一天
  42. * @return
  43. */
  44. public static Date getMonthEndDate(Date date) {
  45. Calendar calendar = Calendar.getInstance();
  46. calendar.setTime(date);
  47. calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  48. return calendar.getTime();
  49. }
  50.  
  51. /**
  52. * 获取指定日期所属周的开始时间
  53. * @param date
  54. * @return
  55. */
  56. public static Date getBeginWeekDate(Date date) {
  57. Calendar cal = Calendar.getInstance();
  58. cal.setTime(date);
  59. int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  60. if (dayOfWeek == 1) {
  61. dayOfWeek += 7;
  62. }
  63. cal.add(Calendar.DATE,2 - dayOfWeek);
  64. return cal.getTime();
  65. }
  66.  
  67. /**
  68. * 距离指定日期所属周结束时间
  69. * @return
  70. */
  71. public static Date getEndWeekDate(Date date) {
  72. Calendar cal = Calendar.getInstance();
  73. cal.setTime(date);
  74. int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  75. if (dayOfWeek == 1) {
  76. dayOfWeek += 7;
  77. }
  78. cal.add(Calendar.DATE,8 - dayOfWeek);
  79. return cal.getTime();
  80. }
  81.  
  82. /**
  83. * 对指定日期进行年份加减操作
  84. * @param date
  85. * @param num
  86. * @return
  87. */
  88. public static Date calculateDateOfYear(Date date,Integer num) {
  89. Calendar calendar = Calendar.getInstance();
  90. calendar.setTime(date);
  91. calendar.add(Calendar.YEAR,num);
  92. return calendar.getTime();
  93. }
  94.  
  95. /**
  96. * 对指定日期月份进行加减操作
  97. * @param date
  98. * @param num
  99. * @return
  100. */
  101. public static Date calculateDateOfMonth(Date date,Integer num) {
  102. Calendar calendar = Calendar.getInstance();
  103. calendar.setTime(date);
  104. calendar.add(Calendar.MONTH,num);
  105. return calendar.getTime();
  106. }
  107.  
  108. /**
  109. * 对指定日期天数进行加减操作
  110. * @param date
  111. * @param num 负整数 正整数
  112. * @return
  113. */
  114. public static Date calculateDateOfDay(Date date,Integer num) {
  115. Calendar calendar = Calendar.getInstance();
  116. calendar.setTime(date);
  117. calendar.add(Calendar.DAY_OF_MONTH,num);
  118. return calendar.getTime();
  119. }
  120.  
  121. public static void main(String[] args) throws ParseException {
  122.  
  123. System.out.println(datetimeToString(getMonthStartDate(sdf.parse("2019-12-04 12:09:52"))));
  124. System.out.println(datetimeToString(getEndWeekDate(sdf.parse("2019-12-04 12:09:52"))));
  125. System.out.println(datetimeToString(calculateDateOfYear(stringToDatetime("2019-12-04 12:09:52"),-2)));
  126. }
  127. }  

java 操作实例的更多相关文章

  1. SysLog简介和java操作实例

    什么是SysLog syslog协议属于一种主从式协议:syslog发送端会传送出一个小的文字讯息(小于1024字节)到syslog接收端.接收端通常名为“syslogd”.“syslog daemo ...

  2. memcached—Java操作Memcached实例

    前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...

  3. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  4. java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】

    java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...

  5. Java 正则表达式实例操作

    Regular Expression正则表达式,简称RegExp,常规通用的表达式,在多个开发语言中都有它的实现,可以通过正则表达式来快速的检索.匹配.查找.替换字符串中的文本. 简单实例 匹配网址 ...

  6. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  7. 安卓 SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  8. 在安卓开发中使用SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  9. Mongodb快速入门之使用Java操作Mongodb

    [IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...

随机推荐

  1. Java 之 JDK 1.8 新增日期时间类型

    一.原来的日期时间 Java1.0中包含了一个Date类,但是它的大多数方法已经在Java 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: ① 可 ...

  2. FileReader生成图片dataurl的分析

    目录 相关代码及html(来源:百度百科) File API及FileReader简介 结合补充知识进行代码分析 修改尝试: 拖曳图片到网页完成转换 相关代码及html(来源:百度百科) <!D ...

  3. windows下搭建vue+webpack的开发环境

    1. 安装git其右键git bash here定位比cmd的命令行要准确,接下来的命令都是利用git bash here.2. 安装node.js一般利用vue创建项目是要搭配webpack项目构建 ...

  4. Java 8 Time Api 使用指南-珍藏限量版

    前面写过了Stream和Lambda,最近正想写Java 8的Time Api,小胖哥这个文章写得很好,就偷懒转载了. 1.概述 Java 8为Date和Time引入了新的API,以解决旧java.u ...

  5. PHP如何创建文件夹(mkdir的用法,mkdirs的语法)【转】

    (PHPmkdir如何创建多级目录)    在开始之前,我先说明一下,可能许多朋友与我一样认为只要给一个路径,mkdir就可以创建文件夹,其它不是那样,单个的MKDIR只能创建一级目录,对于多级的就不 ...

  6. MySQL truncate含有外键约束的条目报错

    1.报错信息: Cannot truncate a table referenced in a foreign key constraint 2.出现错误操作: truncate table a1; ...

  7. Springboot环境搭建_第一个例子

    首先创建一个maven项目 maven项目创建完成之后,找到pom.xml配置节点.需要springboot-starter-web ,springboot-starter-test,springbo ...

  8. Livy 安装教程

    Livy 安装教程 本文原始地址:https://sitoi.cn/posts/16143.html 安装环境 Fedora 29 Spark PySpark 安装步骤 下载 Livy 安装包 解压 ...

  9. Apache源码编译安装脚本

      Apache是开源的的.最流行的Web服务器软件之一,它快速.可靠并且可通过简单的API扩充,将Perl/Python/PHP等解释器编译到服务器中.Apache的模块超多,以及具有运行稳定,强大 ...

  10. 系统管理员必须知道的PHP安全实践

    Apache web 服务器提供了这种便利 :通过 HTTP 或 HTTPS 协议,访问文件和内容.配置不当的服务器端脚本语言会带来各种各样的问题.所以,使用 PHP 时要小心.以下是 25 个 PH ...