java 操作实例
- 输入: "abcabcbb"
- 输出: 3
- 解释: 因为无重复字符的最长子串是
"abc",所以其
长度为 3。
- class Solution {
- public int lengthOfLongestSubstring(String s) {
- int max = 0;
- int curNoStrIndexStart = 0;
- Map<String,Integer> map = new HashMap<>();
- for (int i=0;i < s.length();i++) {
- String index = String.valueOf(s.charAt(i));
- if (map.containsKey(index)) {
- curNoStrIndexStart = Math.max(map.get(index),curNoStrIndexStart);
- }
- max = Math.max(max,i - curNoStrIndexStart +1);
- map.put(index,i + 1);
- }
- return max;
- }
- }
2、mysql获取表的元数据信息
- import java.io.*;
- import java.sql.*;
- import java.util.*;
- public class DBEntityInit {
- private static String propName = "db.properties";
- private static Connection connection = null;
- private static Map<String, List<Column>> tableData = new HashMap<>();
- static {
- connection = getConnection(propName);
- }
- public static Connection getConnection(String propName){
- Connection conn = null;
- try {
- InputStream in = new FileInputStream(new File(propName));
- Properties properties = new Properties();
- properties.load(in);
- String url = properties.getProperty("jdbc.url");
- String username = properties.getProperty("jdbc.username");
- String driver = properties.getProperty("jdbc.driver");
- String password = properties.getProperty("jdbc.password");
- Class.forName(driver);
- conn = DriverManager.getConnection(url,username,password);
- } catch (SQLException | ClassNotFoundException | IOException e) {
- e.printStackTrace();
- }
- return conn;
- }
- public static void initTableData() {
- ResultSet tables = null;
- try {
- DatabaseMetaData metaData = connection.getMetaData();
- tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
- while (tables.next()) {
- tableData.put(tables.getString(3),new ArrayList<>());
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (tables != null) {
- tables.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void initTableColumn(String tableName) {
- List<Column> columns = tableData.get(tableName);
- PreparedStatement pst = null;
- try {
- String sql = "select * from " + tableName;
- pst = connection.prepareStatement(sql);
- ResultSetMetaData metaData = pst.getMetaData();
- int columnCount = metaData.getColumnCount();
- for (int i=0 ; i < columnCount; i++) {
- Column column = new Column();
- column.setName(metaData.getColumnName(i+1));
- column.setType(metaData.getColumnTypeName(i+1));
- columns.add(column);
- }
- //tableData.put(tableName,columns);
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (pst != null) {
- pst.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void initComment(String tableName) {
- //与数据库的连接
- PreparedStatement pst = null;
- String tableSql = "select * from" + tableName;
- List<String> comments = new ArrayList<>();//列名注释集合
- ResultSet rs = null;
- List<Column> columns = tableData.get(tableName);
- try {
- pst = connection.prepareStatement(tableSql);
- rs = pst.executeQuery("show full columns from " + tableName);
- while (rs.next()) {
- String comment = rs.getString("Comment");
- comments.add(comment);
- }
- for (int i=0 ;i < columns.size() ;i++) {
- Column column = columns.get(i);
- column.setComment(comments.get(i));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void closeConnection(Connection conn) {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static Map<String, List<Column>> init() {
- initTableData();
- for (String key : tableData.keySet()) {
- initTableColumn(key);
- initComment(key);
- }
- closeConnection(connection);
- return tableData;
- }
- public static String getPropName() {
- return propName;
- }
- public static void setPropName(String propName) {
- DBEntityInit.propName = propName;
- }
- public static void main(String[] args) {
- init();
- for (Map.Entry<String,List<Column>> entry : tableData.entrySet()) {
- List<Column> value = entry.getValue();
- System.out.println("--------------表名" + entry.getKey());
- for (Column column : value) {
- System.out.print(column.getName() + "---");
- System.out.print(column.getType() + "---");
- System.out.print(column.getComment());
- System.out.println();
- }
- }
- }
- // public static void initColumnComment(String tableName) {
- //
- // try {
- // List<Column> cList = tableData.get(tableName);
- // DatabaseMetaData metaData = connection.getMetaData();
- // ResultSet columns = metaData.getColumns(null, null, tableName, "%");
- //
- // while (columns.next()) {
- // Column column = new Column();
- // String name = columns.getString("COLUMN_NAME");
- // String type = columns.getString("TYPE_NAME");
- // String comment = columns.getString("REMARKS");
- // column.setName(name);
- // column.setType(type);
- // column.setComment(comment);
- // cList.add(column);
- // }
- // } catch (SQLException e) {
- // e.printStackTrace();
- // }
- //
- // }
- }
相关实体
- public class Column {
- private String name;
- private String type;
- private String comment;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- name = name;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getComment() {
- return comment;
- }
- public void setComment(String comment) {
- this.comment = comment;
- }
- }
3、日期的加减操作
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class DateCalculate {
- private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- /**
- * 获取形如yyyy-MM-dd HH:mm:ss
- * @param date
- * @return
- */
- public static String datetimeToString(Date date) {
- return sdf.format(date);
- }
- /**
- * 根据时间字符串获取日期
- * @param dateString
- * @return
- * @throws ParseException
- */
- public static Date stringToDatetime(String dateString) throws ParseException {
- return sdf.parse(dateString);
- }
- /**
- * 获取本月最后一天
- * @return
- */
- public static Date getMonthStartDate(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(Calendar.DAY_OF_MONTH,1);
- return calendar.getTime();
- }
- /**
- * 获取本月最后一天
- * @return
- */
- public static Date getMonthEndDate(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
- return calendar.getTime();
- }
- /**
- * 获取指定日期所属周的开始时间
- * @param date
- * @return
- */
- public static Date getBeginWeekDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayOfWeek == 1) {
- dayOfWeek += 7;
- }
- cal.add(Calendar.DATE,2 - dayOfWeek);
- return cal.getTime();
- }
- /**
- * 距离指定日期所属周结束时间
- * @return
- */
- public static Date getEndWeekDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayOfWeek == 1) {
- dayOfWeek += 7;
- }
- cal.add(Calendar.DATE,8 - dayOfWeek);
- return cal.getTime();
- }
- /**
- * 对指定日期进行年份加减操作
- * @param date
- * @param num
- * @return
- */
- public static Date calculateDateOfYear(Date date,Integer num) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.YEAR,num);
- return calendar.getTime();
- }
- /**
- * 对指定日期月份进行加减操作
- * @param date
- * @param num
- * @return
- */
- public static Date calculateDateOfMonth(Date date,Integer num) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.MONTH,num);
- return calendar.getTime();
- }
- /**
- * 对指定日期天数进行加减操作
- * @param date
- * @param num 负整数 正整数
- * @return
- */
- public static Date calculateDateOfDay(Date date,Integer num) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.DAY_OF_MONTH,num);
- return calendar.getTime();
- }
- public static void main(String[] args) throws ParseException {
- System.out.println(datetimeToString(getMonthStartDate(sdf.parse("2019-12-04 12:09:52"))));
- System.out.println(datetimeToString(getEndWeekDate(sdf.parse("2019-12-04 12:09:52"))));
- System.out.println(datetimeToString(calculateDateOfYear(stringToDatetime("2019-12-04 12:09:52"),-2)));
- }
- }
java 操作实例的更多相关文章
- SysLog简介和java操作实例
什么是SysLog syslog协议属于一种主从式协议:syslog发送端会传送出一个小的文字讯息(小于1024字节)到syslog接收端.接收端通常名为“syslogd”.“syslog daemo ...
- memcached—Java操作Memcached实例
前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...
- java操作Hbase实例
所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...
- Java 正则表达式实例操作
Regular Expression正则表达式,简称RegExp,常规通用的表达式,在多个开发语言中都有它的实现,可以通过正则表达式来快速的检索.匹配.查找.替换字符串中的文本. 简单实例 匹配网址 ...
- JAVA操作ORACLE数据库的存储过程
一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- Mongodb快速入门之使用Java操作Mongodb
[IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...
随机推荐
- Java 之 JDK 1.8 新增日期时间类型
一.原来的日期时间 Java1.0中包含了一个Date类,但是它的大多数方法已经在Java 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: ① 可 ...
- FileReader生成图片dataurl的分析
目录 相关代码及html(来源:百度百科) File API及FileReader简介 结合补充知识进行代码分析 修改尝试: 拖曳图片到网页完成转换 相关代码及html(来源:百度百科) <!D ...
- windows下搭建vue+webpack的开发环境
1. 安装git其右键git bash here定位比cmd的命令行要准确,接下来的命令都是利用git bash here.2. 安装node.js一般利用vue创建项目是要搭配webpack项目构建 ...
- Java 8 Time Api 使用指南-珍藏限量版
前面写过了Stream和Lambda,最近正想写Java 8的Time Api,小胖哥这个文章写得很好,就偷懒转载了. 1.概述 Java 8为Date和Time引入了新的API,以解决旧java.u ...
- PHP如何创建文件夹(mkdir的用法,mkdirs的语法)【转】
(PHPmkdir如何创建多级目录) 在开始之前,我先说明一下,可能许多朋友与我一样认为只要给一个路径,mkdir就可以创建文件夹,其它不是那样,单个的MKDIR只能创建一级目录,对于多级的就不 ...
- MySQL truncate含有外键约束的条目报错
1.报错信息: Cannot truncate a table referenced in a foreign key constraint 2.出现错误操作: truncate table a1; ...
- Springboot环境搭建_第一个例子
首先创建一个maven项目 maven项目创建完成之后,找到pom.xml配置节点.需要springboot-starter-web ,springboot-starter-test,springbo ...
- Livy 安装教程
Livy 安装教程 本文原始地址:https://sitoi.cn/posts/16143.html 安装环境 Fedora 29 Spark PySpark 安装步骤 下载 Livy 安装包 解压 ...
- Apache源码编译安装脚本
Apache是开源的的.最流行的Web服务器软件之一,它快速.可靠并且可通过简单的API扩充,将Perl/Python/PHP等解释器编译到服务器中.Apache的模块超多,以及具有运行稳定,强大 ...
- 系统管理员必须知道的PHP安全实践
Apache web 服务器提供了这种便利 :通过 HTTP 或 HTTPS 协议,访问文件和内容.配置不当的服务器端脚本语言会带来各种各样的问题.所以,使用 PHP 时要小心.以下是 25 个 PH ...