Package org.h2.util
Class JdbcUtils
java.lang.Object
org.h2.util.JdbcUtils
This is a utility class with JDBC helper functions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static boolean
private static String[]
private static final String[]
static JavaObjectSerializer
The serializer to use.private static final ArrayList
<Utils.ClassFactory> In order to manage more than one class loaderprivate static final byte[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addClassFactory
(Utils.ClassFactory classFactory) Add a class factory in order to manage more than one class loader.private static void
static void
closeSilently
(Connection conn) Close a connection without throwing an exception.static void
Close a result set without throwing an exception.static void
closeSilently
(Statement stat) Close a statement without throwing an exception.static Object
deserialize
(byte[] data, JavaObjectSerializer javaObjectSerializer) De-serialize the byte array to an object, eventually using the serializer specified by the connection info.static ValueUuid
deserializeUuid
(byte[] data) De-serialize the byte array to a UUID object.static Connection
getConnection
(String driver, String url, String user, String password) Open a new database connection with the given settings.static Connection
getConnection
(String driver, String url, String user, String password, NetworkConnectionInfo networkConnectionInfo, boolean forbidCreation) Open a new database connection with the given settings.static String
Get the driver class name for the given URL, or null if the URL is unknown.static ResultSet
getMetaResultSet
(Connection conn, String sql) Get metadata from the database.static boolean
Check is the SQL string starts with a prefix (case insensitive).static void
Load the driver class for the given URL, if the database URL is known.static <Z> Class
<Z> loadUserClass
(String className) Load a class, but check if it is allowed to load this class first.static void
removeClassFactory
(Utils.ClassFactory classFactory) Remove a class factorystatic byte[]
serialize
(Object obj, JavaObjectSerializer javaObjectSerializer) Serialize the object to a byte array, using the serializer specified by the connection info if set, or the default serializer.static void
set
(PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) Set a value as a parameter in a prepared statement.private static void
setLob
(PreparedStatement prep, int parameterIndex, ValueLob value) private static void
setOther
(PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) static String[]
Split the string using the space separator into at least 10 entries.
-
Field Details
-
serializer
The serializer to use. -
DRIVERS
-
UUID_PREFIX
private static final byte[] UUID_PREFIX -
allowAllClasses
private static boolean allowAllClasses -
allowedClassNames
-
userClassFactories
In order to manage more than one class loader -
allowedClassNamePrefixes
-
-
Constructor Details
-
JdbcUtils
private JdbcUtils()
-
-
Method Details
-
addClassFactory
Add a class factory in order to manage more than one class loader.- Parameters:
classFactory
- An object that implements ClassFactory
-
removeClassFactory
Remove a class factory- Parameters:
classFactory
- Already inserted class factory instance
-
loadUserClass
Load a class, but check if it is allowed to load this class first. To perform access rights checking, the system property h2.allowedClasses needs to be set to a list of class file name prefixes.- Type Parameters:
Z
- generic return type- Parameters:
className
- the name of the class- Returns:
- the class object
-
closeSilently
Close a statement without throwing an exception.- Parameters:
stat
- the statement or null
-
closeSilently
Close a connection without throwing an exception.- Parameters:
conn
- the connection or null
-
closeSilently
Close a result set without throwing an exception.- Parameters:
rs
- the result set or null
-
getConnection
public static Connection getConnection(String driver, String url, String user, String password) throws SQLException Open a new database connection with the given settings.- Parameters:
driver
- the driver class nameurl
- the database URLuser
- the user namepassword
- the password- Returns:
- the database connection
- Throws:
SQLException
- on failure
-
getConnection
public static Connection getConnection(String driver, String url, String user, String password, NetworkConnectionInfo networkConnectionInfo, boolean forbidCreation) throws SQLException Open a new database connection with the given settings.- Parameters:
driver
- the driver class nameurl
- the database URLuser
- the user name ornull
password
- the password ornull
networkConnectionInfo
- the network connection information, ornull
forbidCreation
- whether database creation is forbidden- Returns:
- the database connection
- Throws:
SQLException
- on failure
-
getDriver
Get the driver class name for the given URL, or null if the URL is unknown.- Parameters:
url
- the database URL- Returns:
- the driver class name
-
load
Load the driver class for the given URL, if the database URL is known.- Parameters:
url
- the database URL
-
serialize
Serialize the object to a byte array, using the serializer specified by the connection info if set, or the default serializer.- Parameters:
obj
- the object to serializejavaObjectSerializer
- the object serializer (may be null)- Returns:
- the byte array
-
deserialize
De-serialize the byte array to an object, eventually using the serializer specified by the connection info.- Parameters:
data
- the byte arrayjavaObjectSerializer
- the object serializer (may be null)- Returns:
- the object
- Throws:
DbException
- if serialization fails
-
deserializeUuid
De-serialize the byte array to a UUID object. This method is called on the server side where regular de-serialization of user-supplied Java objects may create a security hole if object was maliciously crafted. Unlikedeserialize(byte[], JavaObjectSerializer)
, this method does not try to de-serialize instances of other classes.- Parameters:
data
- the byte array- Returns:
- the UUID object
- Throws:
DbException
- if serialization fails
-
set
public static void set(PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) throws SQLException Set a value as a parameter in a prepared statement.- Parameters:
prep
- the prepared statementparameterIndex
- the parameter indexvalue
- the valueconn
- the own connection- Throws:
SQLException
- on failure
-
setOther
private static void setOther(PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) throws SQLException - Throws:
SQLException
-
setLob
private static void setLob(PreparedStatement prep, int parameterIndex, ValueLob value) throws SQLException - Throws:
SQLException
-
getMetaResultSet
Get metadata from the database.- Parameters:
conn
- the connectionsql
- the SQL statement- Returns:
- the metadata
- Throws:
SQLException
- on failure
-
addDatabaseMetaData
-
isBuiltIn
Check is the SQL string starts with a prefix (case insensitive).- Parameters:
sql
- the SQL statementbuiltIn
- the prefix- Returns:
- true if yes
-
split
Split the string using the space separator into at least 10 entries.- Parameters:
s
- the string- Returns:
- the array
-