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 Details

    • serializer

      public static JavaObjectSerializer serializer
      The serializer to use.
    • DRIVERS

      private static final String[] DRIVERS
    • UUID_PREFIX

      private static final byte[] UUID_PREFIX
    • allowAllClasses

      private static boolean allowAllClasses
    • allowedClassNames

      private static HashSet<String> allowedClassNames
    • userClassFactories

      private static final ArrayList<Utils.ClassFactory> userClassFactories
      In order to manage more than one class loader
    • allowedClassNamePrefixes

      private static String[] allowedClassNamePrefixes
  • Constructor Details

    • JdbcUtils

      private JdbcUtils()
  • 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
    • 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

      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
    • addDatabaseMetaData

      private static void addDatabaseMetaData(SimpleResultSet rs, DatabaseMetaData meta)
    • 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