# Java Miscellanea
# Text
String
— seeString
Character
— see also charpublic final class Character extends Object implements Serializable, Comparable<Character>
- general category in the Unicode specification (opens new window)
- defined in static fields as
byte
static int getType(char ch)
static int getType(int codePoint)
- defined in static fields as
- wrapping and outboxing
Character(char value)
static Character valueOf(char c)
char charValue()
- conversion between code points
- casts and
0 + 'a'
static char[] toChars(int codePoint)
static int toChars(int codePoint, char[] dst, int dstIndex)
static int toCodePoint(char high, char low)
codePointAt
codePointBefore
codePointCount
static char forDigit(int digit, int radix)
— 1 to'1'
, 10 to'a'
etc.static int getNumericValue(char ch)
— also for characters likeⅪ
(Roman number)
static int getNumericValue(int codePoint)
- casts and
Character.UnicodeScript
public static enum UnicodeScript
- script names — enum constants
static Character.UnicodeScript forName(String scriptName)
static Character.UnicodeScript of(int codePoint)
Character.UnicodeBlock
public static final class Character.UnicodeBlock extends Character.Subset
- Unicode character block names — static fields
static Character.UnicodeBlock forName(String blockName)
static Character.UnicodeBlock of(char c)
static Character.UnicodeBlock of(int codePoint)
Character.Subset
— for extending, represents particular subsets of the Unicode character set- more
- general category in the Unicode specification (opens new window)
java.nio.charset.Charset
— A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytepublic abstract class Charset extends Object implements Comparable<Charset>
static Charset defaultCharset()
java.text.StringCharacterIterator
# Regex
# Regex Syntax
# Characters and Character Classes
characters
\0n
,\0nn
,\0mnn
— ASCII (0~255 or 0~0o377) in octal\xhh
,\uhhhh
,\x{h...h}
— hexadecimal unicode\t
,\n
,\r
\f
— form feed\x0c
\a
— alert (bell),\x07
\e
— escape,\x1b
\cx
— control character,\ch
forctrl-h
(backspace,\x08
)\R
(matcher) — any Unicode line break sequence, is equivalent to\r\n|[\n\u000B\f\r\u0085\u2028\u2029]
character classes
[a-zA-Z]
— a through z or A through Z, inclusive (range)[a-d[m-p]]
— a through d, or m through p:[a-dm-p]
(union)[a-z&&[def]]
— d, e, or f (intersection)[a-z&&[^bc]]
— a through z, except for b and c:[ad-z]
(subtraction)[a-z&&[^m-p]]
— a through z, and not m through p:[a-lq-z]
(subtraction)[\p{L}&&[^\p{Lu}]]
— any letter except an uppercase letter (subtraction)- predefined non
\p
.
— any character, including line terminators ifDOTALL
\d
— a digit:[0-9]
\D
— a non-digit:[^0-9]
\h
— a horizontal whitespace character:[ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
\H
— a non-horizontal whitespace character:[^\h]
\s
— a whitespace character:[ \t\n\x0B\f\r]
\S
— a non-whitespace character:[^\s]
\v
— a vertical whitespace character:[\n\x0B\f\r\x85\u2028\u2029]
\V
— a non-vertical whitespace character:[^\v]
\w
— a word character:[a-zA-Z_0-9]
\W
— a non-word character:[^\w]
predefined character classes —
\p{prop}
,\P{prop}
for negation, can be\pX
ifprop
only takes one letter- Regex Tutorial - Unicode Characters and Properties (opens new window)
- predefined POSIX ASCII,
\p{ASCII}
— all ASCII:[\x00-\x7F]
\p{Print}
— a printable character:[\p{Graph}\x20]
\p{Graph}
— a visible character:[\p{Alnum}\p{Punct}]
\p{Punct}
— punctuation: One of!"#$%&'()*+,-./:;<=>?@[\]^_{|}~
and backtick\p{Alnum}
— an alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Digit}
— a decimal digit:[0-9]
or\d
\p{XDigit}
— a hexadecimal digit:[0-9a-fA-F]
\p{Alpha}
— an alphabetic character:[\p{Lower}\p{Upper}]
\p{Lower}
— a lower-case alphabetic character:[a-z]
\p{Upper}
— an upper-case alphabetic character:[A-Z]
\p{Blank}
— a space or a tab:[ \t]
\p{Cntrl}
— a control character:[\x00-\x1F\x7F]
\p{Space}
— a whitespace character:[ \t\n\x0B\f\r]
- predefined by
Character
methods\p{javaLowerCase}
— Equivalent toCharacter.isLowerCase()
\p{javaUpperCase}
— Equivalent toCharacter.isUpperCase()
\p{javaWhitespace}
— Equivalent toCharacter.isWhitespace()
\p{javaMirrored}
— Equivalent toCharacter.isMirrored()
- more
- predefined Unicode properties
\p{IsLatin}
— a Latin script character (prefixIs
withCharacter.UnicodeScript
enum value)\p{InGreek}
— a character in the Greek block (prefixIn
withCharacter.UnicodeBlock
static fields)\p{Lu}
,\p{gc=Lu}
— an uppercase letter (general category (opens new window), also partially documented in the javadoc of static fields inCharacter
)\P{InGreek}
— any character except one in the Greek block (negation)\p{IsAlphabetic}
— an alphabetic character (binary property,Is
prefix with below), conform with POSIX character classes whenUNICODE_CHARACTER_CLASS
Alphabetic
Ideographic
Letter
Lowercase
— granted to conform with\p{Lower}
whenUNICODE_CHARACTER_CLASS
Uppercase
Titlecase
Punctuation
Control
White_Space
Digit
Hex_Digit
Join_Control
Noncharacter_Code_Point
Assigned
# Matchers, Quantifiers and Others
line break matcher
\R
— any Unicode line break sequence, is equivalent to\r\n|[\n\u000B\f\r\u0085\u2028\u2029]
\u000B
— a vertical tab\u0085
— a next-line character\u2028
— a line-separator character\u2029
— a paragraph-separator character- line terminator
\n
,\r
,\r\n
\u0085
,\u2028
,\u2029
- line terminator when
UNIX_LINES
—\n
Boundary matchers
^
$
— ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence; respect line terminators whenMULTILINE
\b
— A word boundary\B
— A non-word boundary\A
— The beginning of the input\G
— The end of the previous match\Z
— The end of the input but for the final terminator, if any\z
— The end of the input
quantifiers
- Greedy quantifiers
- Reluctant quantifiers — non-greedy
- Possessive quantifiers — greedy quantifiers that do not backtrack (no turning back to accommodate other parts of the pattern once matched)
Pattern.matches(".*+foo", "xfooxxxxxxfoo") // $1 ==> false Pattern.matches(".*foo", "xfooxxxxxxfoo") // $2 ==> true
X?+
X*+
X++
X{n}+
X{n,}+
X{n,m}+
Logical operators
XY
— X followed by YX|Y
— Either X or Y(X)
— X, as a capturing group
Back references
\n
— Whatever the nth capturing group matched\k<name>
— Whatever the named-capturing group "name" matched
capturing group and flags
((A)(B(C)))
— numbered by counting their opening parentheses from left to right1 ((A)(B(C))) 2 (A) 3 (B(C)) 4 (C)
\0
— stands for the entire expression- if quantification — most recently matched
(?<name>X)
— X, as a named-capturing group, name matches\p{Alpha}\p{Alnum}*
(?idmsuxU-idmsuxU)
— Nothing, but turns match flagsi
d
m
s
u
x
U
on - off(?:X)
— X, as a non-capturing group(?idmsux-idmsux:X)
— X, as a non-capturing group with the given flags i d m s u x on - off
(?>X)
— X, as an independent, non-capturing group, similar to possessive quantifiersPattern.matches("a(?>bc|b)c", "abc") // $1 ==> false Pattern.matches("a(?>bc|b)c", "abcc") // $2 ==> true
- optimization — more performance for patterns like
(?>.*\/)(.*)
,\b(integer|insert|in)\b
- order — for "insert",
\b(?>integer|insert|in)\b
matches but\b(?>in|integer|insert)\b
does not match
- optimization — more performance for patterns like
Quotation
\
— Nothing, but quotes the following character\Q...\E
— Nothing, but quotes all characters until \E
assertion
(?=X)
— X, via zero-width positive lookahead(?!X)
— X, via zero-width negative lookahead(?<=X)
— X, via zero-width positive lookbehind(?<!X)
— X, via zero-width negative lookbehind
# Regex Classes
java.util.regex.Pattern
public final class Pattern extends Object implements Serializable
- flags — bit vector
- encoding and case
static int CANON_EQ
— canonical equivalence, e.g.a\u030A
andå
static int CASE_INSENSITIVE
—(?i)
, only for US ASCIIstatic int UNICODE_CASE
-(?u)
, also Unicode-aware case folding wheni
static int UNICODE_CHARACTER_CLASS
—(?U)
, impliesu
, select Unicode character classes instead of POSIX, see before
- line terminator
static int DOTALL
—(?s)
, make.
match line terminatorsstatic int MULTILINE
—(?m)
, make^
and$
match multiple linesstatic int UNIX_LINES
—(?d)
, only\n
as line terminator for.
,^
,$
- literal and comments
static int LITERAL
— literal parsing of meta characters or escape sequences, onlyu
andi
flag work in this modestatic int COMMENTS
—(?x)
, ignore white spaces and comments which start with#
- encoding and case
- creation
static Pattern compile(String regex)
static Pattern compile(String regex, int flags)
static String quote(String s)
— quote with\Q...\E
- use
static boolean matches(String regex, CharSequence input)
String::split
String[] split(CharSequence input)
String[] split(CharSequence input, int limit)
Stream<String> splitAsStream(CharSequence input)
Matcher matcher(CharSequence input)
Predicate<String> asPredicate()
- get info
int flags()
String pattern()
String toString()
- flags — bit vector
interface java.util.regex.MatchResult
— group related, defaults to using group 0, available if match succeeds- starting index and the past-the-end index
int end()
int end(int group)
int start()
int start(int group)
- group
String group()
String group(int group)
int groupCount()
- starting index and the past-the-end index
java.util.regex.Matcher
public final class Matcher extends Object implements MatchResult
- match operation —
MatchResult
available if succeedsboolean matches()
— match the entire regionboolean lookingAt()
—matches()
but does not require the entire region be matchedboolean find()
— find from the first character not matched by the previous matchboolean find(int start)
— reset and find fromstart
- replace methods
- reset — discards its explicit state information and sets the append position to zero
boolean find(int start)
— reset and find fromstart
Matcher reset()
Matcher reset(CharSequence input)
— reset with a new input sequence
MatchResult
- methods in
MatchResult
int end(String name)
String group(String name)
int start(String name)
MatchResult toMatchResult()
— result unaffected by subsequent operationsboolean requireEnd()
— whether more input could change a positive match into a negative one
- methods in
- region — the region of input to match against
Matcher region(int start, int end)
int regionEnd()
int regionStart()
Matcher useAnchoringBounds(boolean b)
— defaults to using anchoring bounds, the boundaries of the region match anchors such as^
and$
boolean hasAnchoringBounds()
Matcher useTransparentBounds(boolean b)
— defaults to using opaque bounds, the boundaries of the region are opaque, to lookahead, lookbehind, and boundary matching constructs that may try to see beyond themboolean hasTransparentBounds()
- replace
Matcher appendReplacement(StringBuffer sb, String replacement)
— used byreplaceAll
andreplaceFirst
,IllegalStateException
if no match available
StringBuffer appendTail(StringBuffer sb)
Pattern p = Pattern.compile("cat"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, "dog"); System.out.println(sb.toString()); } m.appendTail(sb); System.out.println(sb.toString()); // one dog // one dog two dog // one dog two dogs in the yard
String replaceAll(String replacement)
String replaceFirst(String replacement)
static String quoteReplacement(String s)
— quote\
and$
: otherwise\
for escape,${name}
for named groups,$0
to$9
for group number
- get, set info
Pattern pattern()
Matcher usePattern(Pattern newPattern)
— position in the input and last append position are unaffectedString toString()
boolean hitEnd()
- match operation —
# Format
java.util.Formatter
String::format
java.text.MessageFormat
— partially similar tostr.format
in Pythonpublic class MessageFormat extends Format
- creation
MessageFormat(String pattern)
static String format(String pattern, Object... arguments)
— uses the current localepublic final String format(Object obj)
- choice formatting —
{1,choice,0#no houses|1#one house|2#{1} houses}
, see javadoc for more
- creation
# Logging
- log4j2 with slf4j
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <!-- 使用log4j2要排除logBack依赖 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Spring已经写好了一个log4j2-starter但缺少桥接包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 引入缺少的桥接包 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency>
org.apache.logging.log4j.ThreadContext
— stores properties in the current thread
# Logger
use logger
- prevent gc — A logger that is not referenced by any variable can be garbage collected
private static final Logger myLogger = Logger.getLogger("com.mycompany.myapp");
- hierarchy — child loggers inherit certain properties from parent loggers
- levels —
Level
Level.INFO
by default
- stack info — default log record shows the name of the class and method that contain the logging call, as inferred from the call stack
- call info and optimization — the virtual machine optimizes execution, accurate call information may not be available
Logger::logp
to set calling point explicitly
- log configuration — processed by
LogManager
- manager object property —
java.util.logging.manager
, ifnull
the no-arg protected constructor is used - configuration class property —
java.util.logging.config.class
, take precedence over config files - configuration file —
java.util.logging.config.file
- default config file —
$JAVA_HOME/lib/logging.properties
, or$JAVA_HOME/conf/logging.properties
for JDK >= 9 .level=INFO
— default global logging leveljava.util.logging.ConsoleHandler.level=INFO
— log level forConsoleHandler
- hierarchy —
foo.level
defines a log level for the logger called "foo" and (recursively) for any of its children in the naming hierarchy - children after parents — level settings for child nodes in the tree should come after settings for their parents
- default config file —
- manager object property —
- localization — use resource bundle
Logger logger = Logger.getLogger(loggerName, "com.mycompany.logmessages"); logger.log(Level.INFO, "readingFile", fileName); // fill placeholder
- resource bundles
- such as
com/mycompany/logmessages_de.properties
- property with placeholder —
readingFile=Achtung! Datei {0} wird eingelesen
- such as
- log with explicit resource bundle —
Logger::logrb
- get and set resource bundle —
Logger::getResourceBundle
,Logger::setResourceBundle
- resource bundles
- prevent gc — A logger that is not referenced by any variable can be garbage collected
java.util.logging.Logger
- creation — uses
LogManager
behind the scenesstatic Logger getGlobal()
static Logger getAnonymousLogger()
static Logger getAnonymousLogger(String resourceBundleName)
static Logger getLogger(String name)
static Logger getLogger(String name, String resourceBundleName)
- settings
void setLevel(Level newLevel)
void setFilter(Filter newFilter)
- handler
void setUseParentHandlers(boolean useParentHandlers)
void addHandler(Handler handler)
- hierarchy
void setParent(Logger parent)
Logger getParent()
- log — uses
Handler::publish
behind the scenesvoid info(String msg)
void info(Supplier<String> msgSupplier)
- all levels have methods, except
Level.ALL
andLevel.OFF
- all levels have methods, except
void log(Level level, String msg)
void log(Level level, Supplier<String> msgSupplier)
void logp(Level level, String sourceClass, String sourceMethod, String msg)
— give the precise location of the calling class and method
- log errors
void throwing(String sourceClass, String sourceMethod, Throwable thrown)
FINER
level- message starts with
THROW
void log(Level level, String msg, Throwable thrown)
void logp(Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown)
- pass values into placeholders (in property files)
void log(Level level, String msg, Object param1)
void log(Level level, String msg, Object[] params)
- log execution flow — place at the start and the end of methods, level is
FINER
void entering(String sourceClass, String sourceMethod)
void entering(String sourceClass, String sourceMethod, Object param1)
void entering(String sourceClass, String sourceMethod, Object[] params)
- log records start with
ENTRY
- log records start with
void exiting(String sourceClass, String sourceMethod)
void exiting(String sourceClass, String sourceMethod, Object result)
- log records start with
RETURN
- log records start with
- creation — uses
java.util.logging.LogManager
— singleton to maintain a set of shared state about loggers and log services
# Log Levels and Records
java.util.logging.Level
public class Level extends Object implements Serializable
- levels
static Level OFF
—Integer.MAX_VALUE
static Level SEVERE
— 1000,error
forslf4j
static Level WARNING
— 900,warn
forslf4j
static Level INFO
— 800static Level CONFIG
— 700static Level FINE
— 500,debug
,trace
forslf4j
static Level FINER
— 400static Level FINEST
— 300static Level ALL
—Integer.MIN_VALUE
- levels
java.util.logging.LogRecord
— used to pass logging requests between the logging framework and individual log Handlerspublic class LogRecord extends Object implements Serializable
Level getLevel()
String getMessage()
Object[] getParameters()
Throwable getThrown()
- more
# Handlers
handler — all handlers extends
Handler
, used byLogger::log
- also has level —
INFO
by default, configurable via configuration filejava.util.logging.ConsoleHandler.level=INFO
- hierarchy — a logger sends records both to its own handlers and the handlers of the parent
- the primordial logger (with name
""
) handler —ConsoleHandler
Logger::setUseParentHandlers
- the primordial logger (with name
- custom handlers can be used —
Logger::addHandler
java.util.logging.StreamHandler
ConsoleHandler
— print records toSystem.err
FileHandler
— send records to a filejava<number>.log
in homedir, or some default dir in Windows,XMLFormatter
by default- supports
append
flag - supports file rotation
- use platform encoding by default — set via property
java.util.logging.FileHandler.encoding=UTF-8
- supports
SocketHandler
— sends records to a specified host and port,XMLFormatter
by default
java.util.logging.MemoryHandler
— Handler that buffers requests in a circular buffer in memory.
- also has level —
java.util.logging.Handler
public abstract class Handler
abstract void close()
abstract void flush()
abstract void publish(LogRecord record)
- settings
void setFilter(Filter newFilter)
void setFormatter(Formatter newFormatter)
void setLevel(Level newLevel)
- more
# Filters
filters — implements
Filter
, used by handlers- in addition to level filtering, logger and handler can have an optional filter
Logger::setFilter
,Handler::setFilter
- both at most one filter at a time
java.util.logging.Filter
@FunctionalInterface public interface Filter
boolean isLoggable(LogRecord record)
# Formatters
formatters — extends
Formatter
, used by handlersHandler::setFormatter
java.util.logging.Formatter
public abstract class Formatter
abstract String format(LogRecord record)
- more
# JDBC
java.sql.SQLException
public class SQLException extends Exception implements Iterable<Throwable>
java.sql.BatchUpdateException
java.sql.SQLClientInfoException
java.sql.SQLNonTransientException
java.sql.SQLDataException
java.sql.SQLFeatureNotSupportedException
java.sql.SQLIntegrityConstraintViolationException
java.sql.SQLInvalidAuthorizationSpecException
java.sql.SQLNonTransientConnectionException
java.sql.SQLSyntaxErrorException
java.sql.SQLRecoverableException
java.sql.SQLTransientException
java.sql.SQLTimeoutException
java.sql.SQLTransactionRollbackException
java.sql.SQLTransientConnectionException
java.sql.SQLWarning
java.sql.DataTruncation
# JDBC Properties
JDBC system properties
jdbc.drivers=org.postgresql.Driver jdbc.url=jdbc:postgresql:example jdbc.username=dbuser jdbc.password=secret
JDBC driver
- types
- type 1 driver, no in use anymore — translates JDBC to ODBC and relies on an ODBC driver to communicate with the database
- type 2 driver, deprecated — written partly in Java and partly in native code; it communicates with the client API of a database, platform-specific code required
- type 3 driver — a pure Java client library that uses a database-independent protocol to communicate database requests to a server component, which then translates the requests into a database-specific protocol
- type 4 driver — a pure Java library that translates JDBC requests directly to a database-specific protocol
- driver class register — the driver manager iterates through the registered drivers to find a driver that can use the subprotocol specified in the database URL
- automatic register as service provider — a jar file is automatically registered if it contains the file
META-INF/services/java.sql.Driver
- hard code — load a class and its static initializers executed
Class.forName("org.postgresql.Driver");
- system property —
jdbc.drivers
, separated by colons
- automatic register as service provider — a jar file is automatically registered if it contains the file
- debug
DriverManager::setLogWriter
- some drivers support trace file parameter in url
- types
JDBC url
jdbc:subprotocol:others jdbc:derby://localhost:1527/example;create=true jdbc:postgresql:example
JDBC escape syntax — translate to database-specific syntax variations, see JDBC Reference Information (opens new window)
- control —
Statement::setEscapeProcessing
- temporal
{d '2008-01-24'} {t '23:59:59'} {ts '2008-01-24 23:59:59.999'}
- scalar function
{fn left(?, 20)} {fn user()}
- calling stored procedures
- outer joins —
oj
- the escape character in
LIKE
clauses
- control —
# JDBC Classes
example: establish a connection and execute statements
public static void runTest() throws SQLException, IOException { try (Connection conn = getConnection(); Statement stat = conn.createStatement()) { stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))"); stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')"); try (ResultSet result = stat.executeQuery("SELECT * FROM Greetings")) { if (result.next()) System.out.println(result.getString(1)); } stat.executeUpdate("DROP TABLE Greetings"); } } public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); try (InputStream in = Files.newInputStream(Paths.get("database.properties"))) { props.load(in); } String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); }
java.sql.DriverManager
— for managing a set of JDBC driverspublic class DriverManager extends Object
static Connection getConnection(String url)
— the driver manager iterates through the registered drivers to find a driver that can use the subprotocol specified in the database URL
static Connection getConnection(String url, Properties info)
static Connection getConnection(String url, String user, String password)
static void setLogWriter(PrintWriter out)
- more
interface java.sql.Wrapper
— retrieve the delegate instance when the instance in question is in fact a proxy classboolean isWrapperFor(Class<?> iface)
<T> T unwrap(Class<T> iface)
java.sql.Connection
— A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connectionpublic interface Connection extends Wrapper, AutoCloseable
- metadata
DatabaseMetaData getMetaData()
— information about the database's tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on
- configuration — should not use a SQL statements to configure if a JDBC method available
void abort(Executor executor)
void close()
SQLWarning getWarnings()
,void clearWarnings()
String getSchema()
,void setSchema(String schema)
getClientInfo
,setClientInfo
- transaction — should not use a SQL statements to configure if a JDBC method available
void setAutoCommit(boolean autoCommit)
— defaults totrue
boolean getAutoCommit()
void setTransactionIsolation(int level)
- isolation levels as
static int
fields —TRANSACTION_NONE
,TRANSACTION_READ_COMMITTED
,TRANSACTION_READ_UNCOMMITTED
,TRANSACTION_REPEATABLE_READ
,TRANSACTION_SERIALIZABLE
- isolation levels as
int getTransactionIsolation()
void commit()
Savepoint setSavepoint()
Savepoint setSavepoint(String name)
void releaseSavepoint(Savepoint savepoint)
void rollback()
void rollback(Savepoint savepoint)
- statements — one or more, according to
DatabaseMetaData::getMaxStatements
Statement createStatement()
Statement createStatement(int resultSetType, int resultSetConcurrency)
Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
resultSetType
—ResultSet.TYPE_FORWARD_ONLY
,ResultSet.TYPE_SCROLL_INSENSITIVE
,ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency
—ResultSet.CONCUR_READ_ONLY
,ResultSet.CONCUR_UPDATABLE
resultSetHoldability
—ResultSet.HOLD_CURSORS_OVER_COMMIT
,ResultSet.CLOSE_CURSORS_AT_COMMIT
prepareCall
— forCALL
stored proceduresprepareStatement
—PREPARE
andEXECUTE
java.sql.DatabaseMetaData
— information about the DBMS, the driver, and the results of someSHOW
statementspublic interface DatabaseMetaData extends Wrapper
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
- metadata
java.sql.Statement
— executing a static SQL statement and returning the resultspublic interface Statement extends Wrapper, AutoCloseable
- one
ResultSet
a time — all execution methods in theStatement
interface implicitly close a currentResultSet
object of the statement if an open one exists ResultSet getResultSet()
getMoreResults
— for databases that also allow submission of multiple SELECT statements in a single queryvoid setEscapeProcessing(boolean enable)
— defaults totrue
- autogenerated keys — whether retrieve autogenerated keys, often the primary key
int executeUpdate(String sql, int autoGeneratedKeys)
and other methodsstatic int RETURN_GENERATED_KEYS
static int NO_GENERATED_KEYS
- batch — no
SELECT
void addBatch(String sql)
int[] executeBatch()
- execute
execute
int[] executeBatch()
default long[] executeLargeBatch()
executeLargeUpdate
ResultSet executeQuery(String sql)
executeUpdate
— returns count for rows affected or 0
- one
java.sql.PreparedStatement
public interface PreparedStatement extends Statement
void clearParameters()
- example
PreparedStatement pSt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pSt.setBigDecimal(1, 153833.00); pSt.setInt(2, 110592); pSt.executeUpdate();
java.sql.ResultSet
— a table of data representing a database result setpublic interface ResultSet extends Wrapper, AutoCloseable
- cursor — initially positioned before the first row; no
hasNext
, keep callingnext
untilfalse
boolean next()
— moves the cursor forward one row from its current position, initially positioned before the first row,false
when at last rowwhile (rs.next()) { }
- scrollable cursor
boolean absolute(int row)
boolean relative(int rows)
boolean previous()
first
,last
,beforeFirst
, andafterLast
isFirst
,isLast
,isBeforeFirst
, andisAfterLast
ResultSetMetaData getMetaData()
ResultSetMetaData::getColumnTypeName
get-
prefixed methods — get results as a certain type from the current row, two forms of parameters, for types likeArray
,Blob
,Clob
,int
,String
etc.__ get__(int columnIndex)
— index starts from 1__ get__(String columnLabel)
update-
prefixed methods — likeget-
methods, for interactive updatevoid updateRow()
— updates the underlying database with the new contents of the current row, changes will be discarded if not calledvoid cancelRowUpdates()
- insert — for interactive scenarios
void moveToInsertRow()
update-
methodsvoid insertRow()
void moveToCurrentRow()
void deleteRow()
— for interactive scenarios
- cursor — initially positioned before the first row; no
JDBC type interfaces
java.lang
type classes,String
and stream type classes,java.math
type classes, primitive typesjava.net.URL
type classjava.sql.Array
java.sql.Blob
java.sql.Clob
java.sql.Ref
java.sql.RowId
java.sql.SQLData
— SQL user-defined type (UDT)java.sql.SQLType
— a generic SQL type, called a JDBC type or a vendor specific data typejava.sql.Date
public class Date extends java.util.Date
java.sql.Time
public class Time extends java.util.Date
java.sql.Timestamp
public class Timestamp extends java.util.Date
java.sql.Types
—static int
constants that are used to identify generic SQL typesARRAY
,BIGINT
,BINARY
,BIT
,BLOB
,BOOLEAN
,TINYINT
,VARBINARY
,VARCHAR
- more
# Row Sets
javax.sql.RowSet
— a connected rowset or more commonly, a disconnected rowsetpublic interface RowSet extends ResultSet
- `javax.sql.rowset.JdbcRowSet — added functionality of connections, like transaction
javax.sql.rowset.CachedRowSet
— a disconnected rowsetpublic interface CachedRowSet extends RowSet, Joinable
javax.sql.rowset.WebRowSet
— a cached row set that can be saved to an XML filejavax.sql.rowset.FilteredRowSet
— for filteringjavax.sql.rowset.JoinRowSet
— forJOIN
interface javax.sql.rowset.RowSetFactory
__RowSet create__RowSet()
javax.sql.rowset.RowSetProvider
static RowSetFactory newFactory()
static RowSetFactory newFactory(String factoryClassName, ClassLoader cl)
# Time
current timestamps
System
static long nanoTime()
static long currentTimeMillis()
SQL and legacy classes
- timestamps (
Instant
) —java.util.Date
: represents a specific instant in time, with millisecond precisionpublic class Date extends Object implements Serializable, Cloneable, Comparable<Date>
java.sql.Date
— interoperable with SQLDATE
java.sql.Time
— interoperable with SQLTIME
without a timezonejava.sql.Timestamp
— interoperable with SQLTIMESTAMP
, adds the ability to hold the SQLTIMESTAMP
fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds
- calendars —
java.util.Calendar
: for converting between a specific instant in time and a set of calendar fields, and for manipulating the calendar fieldspublic abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
java.util.GregorianCalendar
— a concrete subclass ofCalendar
and provides the standard calendar system used by most of the world
- timezones —
java.util.TimeZone
public abstract class TimeZone extends Object implements Serializable, Cloneable
java.util.SimpleTimeZone
- formats
java.text.DateFormat
public abstract class DateFormat extends Format
java.text.SimpleDateFormat
— not thread safe, useThreadLocal
to alleviate, see Thread
- timestamps (
java.time
— dates, times, instants, and durationsjava.time.temporal
— lower level access to the fieldsjava.time.format
— printing and parsing all manner of dates and timesjava.time.chrono
— calendar neutral APIs, localized calendars, for interactions with users- non-null — non-null constructors and methods, except checking and validating methods
- API convention
of
— static factory methodparse
— static factory method focussed on parsingget
— gets the value of somethingis
— checks if something is truewith
— the immutable equivalent of a setterplus
— adds an amount to an objectminus
— subtracts an amount from an objectto
— converts this object to another typeat
— combines this object with another, such asdate.atTime(time)
from
- enums
java.time.Month
public enum Month extends Enum<Month> implements TemporalAccessor, TemporalAdjuster
java.time.DayOfWeek
public enum DayOfWeek extends Enum<DayOfWeek> implements TemporalAccessor, TemporalAdjuster
java.time.Instant
public final class Instant extends Object implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable
- SQL type —
TIMESTAMP
with a timezone - epoch
static Instant EPOCH
— Constant for the 1970-01-01T00:00:00Z epoch instant.static Instant MAX
— The maximum supported Instant, '1000000000-12-31T23:59:59.999999999Z'.static Instant MIN
— The minimum supported Instant, '-1000000000-01-01T00:00Z'.
- creation
now
of-
prefixed methodsstatic Instant parse(CharSequence text)
- conversion
long toEpochMilli()
String toString()
int getNano()
getLong
- SQL type —
java.time.Duration
— amount of time, kept in severallong
for seconds, anint
for nanosecondspublic final class Duration extends Object implements TemporalAmount, Comparable<Duration>, Serializable
- creation
static Duration between(Temporal startInclusive, Temporal endExclusive)
- more
- creation
java.time.OffsetDateTime
— A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as2007-12-03T10:15:30+01:00
- SQL type —
TIMESTAMP
with a timezone
- SQL type —
java.time.ZonedDateTime
—OffsetDateTime
withZoneId
, such as2007-12-03T10:15:30+01:00 Europe/Paris
- SQL type —
TIMESTAMP
with a timezone - The Internet Assigned Numbers Authority (IANA) database
ZoneId getZone()
OffsetDateTime toOffsetDateTime()
- SQL type —
java.time.LocalDateTime
- SQL type —
DATETIME
- SQL type —
java.time.LocalDate
— A date without a time-zone in the ISO-8601 calendar systempublic final class LocalDate extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
- SQL type —
DATE
- overflow when
plusMonth
orminusMonth
— return the last valid day of the month
- SQL type —
java.time.Period
— A date-based amount of time in the ISO-8601 calendar system, counterpart ofDuration
public final class Period extends Object implements ChronoPeriod, Serializable
java.time.OffsetTime
- SQL type —
TIME
with a timezone
- SQL type —
java.time.LocalTime
- SQL type —
TIME
without a timezone
- SQL type —
java.time.format.DateTimeFormatter
— Formatter for printing and parsing date-time objectspublic final class DateTimeFormatter extends Object
- creation
- predefined — see javadoc
ofLocalized-
prefixed methods to create fromFormatStyle
- patterns — see javadoc for patterns like
"E yyyy-MM-dd HH:mm"
static DateTimeFormatter ofPattern(String pattern)
static DateTimeFormatter ofPattern(String pattern, Locale locale)
- localized
DateTimeFormatter withLocale(Locale locale)
- conversion
toFormat
- format
String format(TemporalAccessor temporal)
void formatTo(TemporalAccessor temporal, Appendable appendable)
- parse methods
LocalDate::parse
,LocalDateTime::parse
,LocalTime::parse
, orZonedDateTime::parse
, not suitable for parsing human input- other parse methods
enum java.time.format.FormatStyle
—FULL
,LONG
,MEDIUM
,SHORT
java.time.format.TextStyle
public enum TextStyle extends Enum<TextStyle>
FULL
/FULL_STANDALONE
SHORT
/SHORT_STANDALONE
NARROW
/NARROW_STANDALONE
Month::getDisplayName
—String getDisplayName(TextStyle style, Locale locale)
- creation
# i18n
locale — language code and optionally other codes; language tag like
en-US
lang-script-region-extension
- language code — two or three lowercase letters, such as
en
- script code — four letters with an initial uppercase, such as
Latn
(Latin),Cyrl
(Cyrillic), orHant
(traditional Chinese characters) - country (region) code — two uppercase letters or three digits such as US (United States) or CH (Switzerland)
- variant: miscellaneous (such as dialects or spelling rules) codes — rarely used or made into extension
- extension — start with
u-
and a two-letter code specifying whether the extension deals with the calendar (ca
), numbers (nu
), and so on; Other extensions are entirely arbitrary and start withx-
, such asx-java
- describe local preferences — calendars (such as the Japanese calendar), numbers (
u-nu-thai
Thai instead of Western digits), and so on
- describe local preferences — calendars (such as the Japanese calendar), numbers (
- example
"zh-Hans-CN"
— Chinese written in simplified characters as used in China (primary language with script and country codes)"ja-JP-u-ca-japanese"
— Use the Japanese calendar in date and time formatting, so that 2013 is expressed as the year 25 of the Heisei period, or 平成25
- reference
- language code — two or three lowercase letters, such as
java.util.Locale
- creation
- constructors
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
static Locale forLanguageTag(String languageTag)
static Locale[] getAvailableLocales()
- predefined
static char PRIVATE_USE_EXTENSION
— The key for the private use extension ('x').static char UNICODE_LOCALE_EXTENSION
—'u'
static Locale ROOT
static Locale SIMPLIFIED_CHINESE
static Locale US
static Locale UK
- more
- from locale-dependent utility classes —
NumberFormat.getAvailableLocales()
Locale.Builder
public static final class Locale.Builder extends Object
Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
- constructors
- default —
System.getProperty("user.language")
,System.getProperty("user.region")
static Locale getDefault()
static Locale getDefault(Locale.Category category)
static void setDefault(Locale.Category category, Locale newLocale)
static void setDefault(Locale newLocale)
- get methods — show info about this locale
- transformation
Locale stripExtensions()
String toLanguageTag()
- creation
java.text.Format
— abstract base class for formatting locale-sensitive information such as dates, messages, and numberspublic abstract class Format extends Object implements Serializable, Cloneable
- format
String format(Object obj)
abstract StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
AttributedCharacterIterator formatToCharacterIterator(Object obj)
- parse
Object parseObject(String source)
abstract Object parseObject(String source, ParsePosition pos)
- use a Scanner to read localized integers and floating-point numbers —
Scanner::useLocale
- format
MessageFormat
- creation
MessageFormat(String pattern, Locale locale)
- public final String format(Object obj)
- creation
# Number Format
java.text.NumberFormat
public abstract class NumberFormat extends Format
- creation
static Locale[] getAvailableLocales()
static NumberFormat getCurrencyInstance()
static NumberFormat getCurrencyInstance(Locale inLocale)
static NumberFormat getInstance()
static NumberFormat getInstance(Locale inLocale)
static NumberFormat getIntegerInstance()
static NumberFormat getIntegerInstance(Locale inLocale)
static NumberFormat getNumberInstance()
static NumberFormat getNumberInstance(Locale inLocale)
static NumberFormat getPercentInstance()
static NumberFormat getPercentInstance(Locale inLocale)
- settings — get, set methods
- creation
java.util.Currency
— control the currency used by the formatterspublic final class Currency extends Object implements Serializable
NumberFormat::serCurrency
—void setCurrency(Currency currency)
- creation
static Set<Currency> getAvailableCurrencies()
static Currency getInstance(Locale locale)
static Currency getInstance(String currencyCode)
- display
String getCurrencyCode()
int getDefaultFractionDigits()
String getDisplayName()
String getDisplayName(Locale locale)
int getNumericCode()
String getSymbol()
String getSymbol(Locale locale)
String toString()
java.time.format.DateTimeFormatter
— see Time
# Collator and Normalizer
java.text.Collator
— locale-sensitive String comparisonpublic abstract class Collator extends Object implements Comparator<Object>, Cloneable
- decomposition — four normalization forms (D, KD, C, and KC)
- normalization forms
- In the normalization form C, accented characters are always composed. For example, a sequence of A and a combining ring above ° is combined into a single character Å (recommended by W3C)
- In form D, accented characters are always decomposed into their base letters and combining accents: Å is turned into A followed by °
- Forms KC and KD also decompose characters such as ligatures or the trademark symbol
static int NO_DECOMPOSITION
static int CANONICAL_DECOMPOSITION
static int FULL_DECOMPOSITION
- normalization forms
- strength — in Czech, "e" and "f" are considered primary differences, while "e" and "ě" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical
PRIMARY
,SECONDARY
,TERTIARY
, andIDENTICAL
int getStrength()
void setStrength(int newStrength)
- creation
static Locale[] getAvailableLocales()
static Collator getInstance()
static Collator getInstance(Locale desiredLocale)
- compare
int compare(Object o1, Object o2)
abstract int compare(String source, String target)
- sort —
abstract CollationKey getCollationKey(String source)
for better performance java.text.CollationKey
— a series of bits that can be compared bitwise against otherCollationKeys
of the sameCollator
objectpublic abstract class CollationKey extends Object implements Comparable<CollationKey>
abstract int compareTo(CollationKey target)
String getSourceString()
abstract byte[] toByteArray()
- example
var ss = new String[] {"Peter Öhlund", "Peter Ohlin", "Peter Olsdal", "Peter Zorn"}; Arrays.sort(ss); Arrays.sort(ss, Collator.getInstance(Locale.GERMANY)); Arrays.sort(ss, Collator.getInstance(Locale.forLanguageTag("sv"))); Arrays.stream(ss) .map((s) -> collator.getCollationKey(s)) .sorted() .map((k) -> k.getSourceString()) .toArray((i) -> new String[i]);
- decomposition — four normalization forms (D, KD, C, and KC)
java.text.Normalizer
— decomposition inCollator
, see Unicode Standard Annex #15 — Unicode Normalization Forms (opens new window)public final class Normalizer extends Object
static boolean isNormalized(CharSequence src, Normalizer.Form form)
static String normalize(CharSequence src, Normalizer.Form form)
String name = "Ångström"; String normalized = Normalizer.normalize(name, Normalizer.Form.NFD);
Normalizer.Form
public static enum Normalizer.Form extends Enum<Normalizer.Form>
NFC
NFD
NFKC
NFKD
# Resource Bundles
resource bundles — locale-specific items
- naming convention
- country specific —
bundleName_language_country
- language specific —
bundleName_language
- country specific —
- load order
bundleName_currentLocaleLanguage_currentLocaleCountry
bundleName_currentLocaleLanguage
bundleName_currentLocaleLanguage_defaultLocaleCountry
bundleName_defaultLocaleLanguage
bundleName
- resource hierarchy — the parents are searched if a lookup was not successful in the current bundle
- load — parents are looked up when a bundle is already located
- order —
bundleName_de_DE
, then thebundleName_de
andbundleName
- property files
- supported by
ResourceBundle::getBundle
// MyProgramStrings.properties // MyProgramStrings_en.properties // MyProgramStrings_de_DE.properties ResourceBundle bundle = ResourceBundle.getBundle("MyProgramStrings", locale);
- only ASCII in property files — use
\uxxxx
for unicode - property with placeholder —
readingFile=Achtung! Datei {0} wird eingelesen
- supported by
- bundle classes — to provide resources that are not strings
- priority — more prior than property files
- supported by
ResourceBundle::getBundle
// MyProgramResources.java // MyProgramResources_en.java // MyProgramResources_de_DE.java ResourceBundle bundle = ResourceBundle.getBundle("MyProgramResources", locale);
- define bundle classes
- extend
ListResourceBundle
public class MyProgramResources_de_DE extends ListResourceBundle { private static final Object[][] contents = { { "backgroundColor", Color.black }, { "defaultPaperSize", new double[] { 210, 297 } } } @Override public Object[][] getContents() { return contents; } }
- extend
ResourceBundle
and override below methodsabstract Enumeration<String> getKeys() protected abstract Object handleGetObject(String key)
- extend
- naming convention
java.util.ResourceBundle
public abstract class ResourceBundle extends Object
- load
static ResourceBundle getBundle(String baseName)
static ResourceBundle getBundle(String baseName, Locale locale)
static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)
static ResourceBundle getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)
static ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)
static ResourceBundle getBundle(String baseName, ResourceBundle.Control control)
- get
Object getObject(String key)
String getString(String key)
String[] getStringArray(String key)
- load
# Security
- security manager —
SecurityManager
- usage — no security manager installed by default, use with
System::setSecurityManager
, or CLI option-Djava.security.manager
- permission checking —
SecurityException
- security policy,
java.security.Policy
— code sources to permission sets,java.security.Permission
- protection domain — tbd
- policy files — tbd
- system property —
java.security.policy
, double equals sign (==
) to exclude other standard policy files
- system property —
- Java Authentication and Authorization Service (JAAS) —
javax.security.auth.login.LoginContext
, tbd- login policies
- tbd
- usage — no security manager installed by default, use with
# Other Java APIs
agents — bytecode engineering at load time
- bytecode engineering compiled classes — ASM (opens new window)
- CLI
-agentlib:<libname>[=<options>]
-agentpath:<pathname>[=<options>]
-javaagent:<jarpath>[=<options>]
java.lang.instrument
— allow agents to instrument programs running on the JVM. The mechanism for instrumentation is modification of the byte-codes of methods.- more in package javadoc
- build an agent when launching JVM
- write an agent class with
premain
, called when the agent is loaded, beforemain
of programspublic static void premain(String arg, Instrumentation instr)
- CLI options (args) — can get a single one
agentmain
method for agents starting sometime after JVM launched
- write a manifest setting
Premain-Class
Premain-Class: bytecodeAnnotations.EntryLoggingAgent
- package the agent class and the manifest
jar cvfm EntryLoggingAgent.jar bytecodeAnnotations/EntryLoggingAgent.mf \ bytecodeAnnotations/Entry*.class
- write an agent class with
scripting
var $1 = new javax.script.ScriptEngineManager().getEngineByName("nashorn"); String json = "{\"a\": 5}"; var $7 = (jdk.nashorn.api.scripting.ScriptObjectMirror) $1.eval("JSON.parse('" + json + "')"); $7.getMember("a"); // 5, Integer
- engines
- nashorn — JavaScript engine, included from JDK 8, deprecated from JDK 11
- groovy
- Renjin — R language
- sisc
- bindings — variable contexts
- new polyglot solution: GraalVM — a Java VM and JDK based on HotSpot/OpenJDK, for running applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin, Clojure, and LLVM-based languages such as C and C++
- engines
compiling —
javax.tools.JavaCompiler
and moreJavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); OutputStream outStream = ; // null for System.out OutputStream errStream = ; // null for System.err int result = compiler.run(null, outStream, errStream, "-sourcepath", "src", "Test.java");