一、無重複字符的最長子串java
輸入: "abcabcbb" 輸出: 3 解釋: 由於無重複字符的最長子串是 長度爲 3。"abc",因此其
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; } }
二、mysql獲取表的元數據信息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(); // } // // } }
相關實體sql
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; } }
三、日期的加減操做數據庫
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))); } }