Package org.h2.util

# Class JdbcUtils

java.lang.Object
org.h2.util.JdbcUtils

public class JdbcUtils extends Object
This is a utility class with JDBC helper functions.

## Field Summary

Fields
Modifier and Type
Field
Description
`static JavaObjectSerializer`
`serializer`
The serializer to use.

## Method Summary

Modifier and Type
Method
Description
`static void`
`addClassFactory(Utils.ClassFactory classFactory)`
Add a class factory in order to manage more than one class loader.
`static void`
`closeSilently(Connection conn)`
Close a connection without throwing an exception.
`static void`
`closeSilently(ResultSet rs)`
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`
`getDriver(String url)`
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`
`isBuiltIn(String sql, String builtIn)`
Check is the SQL string starts with a prefix (case insensitive).
`static void`
`load(String url)`
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 factory
`static 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.
`static String[]`
`split(String s)`
Split the string using the space separator into at least 10 entries.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### serializer

public static JavaObjectSerializer serializer
The serializer to use.

## Method Details

### 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> Class<Z> loadUserClass(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(Statement stat)
Close a statement without throwing an exception.

Parameters:
  `stat` - the statement or null

### closeSilently

public static void closeSilently(Connection conn)
Close a connection without throwing an exception.

Parameters:
  `conn` - the connection or null

### closeSilently

public static void closeSilently(ResultSet rs)
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 name
  `url` - the database URL
  `user` - the user name
  `password` - 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 name
  `url` - the database URL
  `user` - the user name or `null`
  `password` - the password or `null`
  `networkConnectionInfo` - the network connection information, or `null`
  `forbidCreation` - whether database creation is forbidden
Returns:
  the database connection
Throws:
  `SQLException` - on failure

### getDriver

public static String getDriver(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(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(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 serialize
  `javaObjectSerializer` - the object serializer (may be null)
Returns:
  the byte array

### deserialize

public static 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 array
  `javaObjectSerializer` - 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. Unlike `deserialize(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 statement
  `parameterIndex` - the parameter index
  `value` - the value
  `conn` - the own connection
Throws:
  `SQLException` - on failure

### getMetaResultSet

public static ResultSet getMetaResultSet(Connection conn, String sql) throws SQLException
Get metadata from the database.

Parameters:
  `conn` - the connection
  `sql` - the SQL statement
Returns:
  the metadata
Throws:
  `SQLException` - on failure

### isBuiltIn

public static boolean isBuiltIn(String sql, String builtIn)
Check is the SQL string starts with a prefix (case insensitive).

Parameters:
  `sql` - the SQL statement
  `builtIn` - the prefix
Returns:
  true if yes

### split

public static String[] split(String s)
Split the string using the space separator into at least 10 entries.

Parameters:
  `s` - the string
Returns:
  the array