Package org.h2.util
Class JdbcUtils
- java.lang.Object
-
- org.h2.util.JdbcUtils
-
public class JdbcUtils extends java.lang.Object
This is a utility class with JDBC helper functions.
-
-
Field Summary
Fields Modifier and Type Field Description private static boolean
allowAllClasses
private static java.lang.String[]
allowedClassNamePrefixes
private static java.util.HashSet<java.lang.String>
allowedClassNames
private static java.lang.String[]
DRIVERS
static JavaObjectSerializer
serializer
The serializer to use.private static java.util.ArrayList<Utils.ClassFactory>
userClassFactories
In order to manage more than one class loaderprivate static byte[]
UUID_PREFIX
-
Constructor Summary
Constructors Modifier Constructor Description private
JdbcUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addClassFactory(Utils.ClassFactory classFactory)
Add a class factory in order to manage more than one class loader.private static void
addDatabaseMetaData(SimpleResultSet rs, java.sql.DatabaseMetaData meta)
static void
closeSilently(java.sql.Connection conn)
Close a connection without throwing an exception.static void
closeSilently(java.sql.ResultSet rs)
Close a result set without throwing an exception.static void
closeSilently(java.sql.Statement stat)
Close a statement without throwing an exception.static java.lang.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 java.sql.Connection
getConnection(java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password)
Open a new database connection with the given settings.static java.sql.Connection
getConnection(java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password, NetworkConnectionInfo networkConnectionInfo, boolean forbidCreation)
Open a new database connection with the given settings.static java.lang.String
getDriver(java.lang.String url)
Get the driver class name for the given URL, or null if the URL is unknown.static java.sql.ResultSet
getMetaResultSet(java.sql.Connection conn, java.lang.String sql)
Get metadata from the database.static boolean
isBuiltIn(java.lang.String sql, java.lang.String builtIn)
Check is the SQL string starts with a prefix (case insensitive).static void
load(java.lang.String url)
Load the driver class for the given URL, if the database URL is known.static <Z> java.lang.Class<Z>
loadUserClass(java.lang.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(java.lang.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(java.sql.PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn)
Set a value as a parameter in a prepared statement.private static void
setLob(java.sql.PreparedStatement prep, int parameterIndex, ValueLob value)
private static void
setOther(java.sql.PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn)
static java.lang.String[]
split(java.lang.String s)
Split the string using the space separator into at least 10 entries.
-
-
-
Field Detail
-
serializer
public static JavaObjectSerializer serializer
The serializer to use.
-
DRIVERS
private static final java.lang.String[] DRIVERS
-
UUID_PREFIX
private static final byte[] UUID_PREFIX
-
allowAllClasses
private static boolean allowAllClasses
-
allowedClassNames
private static java.util.HashSet<java.lang.String> allowedClassNames
-
userClassFactories
private static final java.util.ArrayList<Utils.ClassFactory> userClassFactories
In order to manage more than one class loader
-
allowedClassNamePrefixes
private static java.lang.String[] allowedClassNamePrefixes
-
-
Method Detail
-
addClassFactory
public static void addClassFactory(Utils.ClassFactory classFactory)
Add a class factory in order to manage more than one class loader.- Parameters:
classFactory
- An object that implements ClassFactory
-
removeClassFactory
public static void removeClassFactory(Utils.ClassFactory classFactory)
Remove a class factory- Parameters:
classFactory
- Already inserted class factory instance
-
loadUserClass
public static <Z> java.lang.Class<Z> loadUserClass(java.lang.String className)
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
public static void closeSilently(java.sql.Statement stat)
Close a statement without throwing an exception.- Parameters:
stat
- the statement or null
-
closeSilently
public static void closeSilently(java.sql.Connection conn)
Close a connection without throwing an exception.- Parameters:
conn
- the connection or null
-
closeSilently
public static void closeSilently(java.sql.ResultSet rs)
Close a result set without throwing an exception.- Parameters:
rs
- the result set or null
-
getConnection
public static java.sql.Connection getConnection(java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password) throws java.sql.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:
java.sql.SQLException
- on failure
-
getConnection
public static java.sql.Connection getConnection(java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password, NetworkConnectionInfo networkConnectionInfo, boolean forbidCreation) throws java.sql.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:
java.sql.SQLException
- on failure
-
getDriver
public static java.lang.String getDriver(java.lang.String url)
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
public static void load(java.lang.String url)
Load the driver class for the given URL, if the database URL is known.- Parameters:
url
- the database URL
-
serialize
public static byte[] serialize(java.lang.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.- Parameters:
obj
- the object to serializejavaObjectSerializer
- the object serializer (may be null)- Returns:
- the byte array
-
deserialize
public static java.lang.Object deserialize(byte[] data, JavaObjectSerializer javaObjectSerializer)
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
public static ValueUuid deserializeUuid(byte[] data)
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(java.sql.PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) throws java.sql.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:
java.sql.SQLException
- on failure
-
setOther
private static void setOther(java.sql.PreparedStatement prep, int parameterIndex, Value value, JdbcConnection conn) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
setLob
private static void setLob(java.sql.PreparedStatement prep, int parameterIndex, ValueLob value) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getMetaResultSet
public static java.sql.ResultSet getMetaResultSet(java.sql.Connection conn, java.lang.String sql) throws java.sql.SQLException
Get metadata from the database.- Parameters:
conn
- the connectionsql
- the SQL statement- Returns:
- the metadata
- Throws:
java.sql.SQLException
- on failure
-
addDatabaseMetaData
private static void addDatabaseMetaData(SimpleResultSet rs, java.sql.DatabaseMetaData meta)
-
isBuiltIn
public static boolean isBuiltIn(java.lang.String sql, java.lang.String builtIn)
Check is the SQL string starts with a prefix (case insensitive).- Parameters:
sql
- the SQL statementbuiltIn
- the prefix- Returns:
- true if yes
-
split
public static java.lang.String[] split(java.lang.String s)
Split the string using the space separator into at least 10 entries.- Parameters:
s
- the string- Returns:
- the array
-
-