ColumnSizes Class
DEPRECATED
- public class ColumnSizes
extends Object
Utility class to help determine JDBC table column sizes and to truncate
data for inserting.
The general usage of this class should be truncate data fields before
sending them to the database to avoid extraneous errors when data
truncation would be a valid solution. For example,
String id = request.getParameter("id");
String name = request.getParameter("name");
String value = request.getParameter("value");
Connection con =
DriverManager.getConnection("jdbc:weblogic:jts:commercePool");
String sql = "INSERT INTO MYTABLE (ID, NAME, VALUE) VALUES (?, ?, ?)";
PreparedStatement stmt = con.prepareStatment(sql);
id = ColumnSizes.truncate
(id, con, "MYTABLE", "ID");
stmt.setString(1, id);
name = ColumnSizes.truncate
(name, con, "MYTABLE", "NAME");
stmt.setString(2, name);
value = ColumnSizes.truncate
(value, con, "MYTABLE", "VALUE");
stmt.setString(3, value);
stmt.executeUpdate();
stmt.close();
con.close();
Additionally, you can use the getColumnInfo()
method to determine the size of a table column. In this manner, you could
warn the user about the max size or about data truncation.
When determining the size of columns, the following logic is used:
- If the property "p13n.jdbc.maxsize.<table>.<column>" in
the loader.properties file is set to a numeric value, then it is
assumed the column is a TEXT column with that maximum length.
- If the property "p13n.jdbc.maxsize.checkMetaData" in the
loader.properties file is set to "true" and the specified
Connection is not null, then the Connection's DatabaseMetaData will be used
to determine the column information.
- If the column is not found or "p13n.jdbc.maxsize.checkMetaData" is
not "true", then null is returned.
Additionally, the column information is cached to avoid extraneous database
activity. Therefore, if the tables in the database are altered, the server
will need to be restarted.
Note: Setting p13n.jdbc.maxsize.checkMetaData to true will cause the
java.sql.DatabaseMetaData.getColumns() method to be invoked. Some JDBC
drivers do not support this method correctly. Some Type 2 drivers might
core dump on this method call.
-
Hierarchy
-
Object
ColumnSizes
public static final int |
-
BOOLEAN
- The constant for a boolean type (0).
|
public static boolean |
-
CHECK_META_DATA
- Should we check with the DatabaseMetaData for getting column sizes.
|
public static final int |
-
DATETIME
- The constant for a datetime type (4).
|
public static final int |
-
FLOAT
- The constant for the floating-point numeric type (2).
|
public static final int |
-
INTEGER
- The constant for a numeric type (1).
|
public static final int |
-
MULTI_VALUED
- The constant for a multivalued type (6).
|
public static final int |
-
TEXT
- The constant for a text type (3).
|
public static final int |
-
USER_DEFINED
- The constant for a user defined type (5).
|
Methods from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
BOOLEAN
public static final int BOOLEAN
- The constant for a boolean type (0).
CHECK_META_DATA
public static boolean CHECK_META_DATA
- Should we check with the DatabaseMetaData for getting column sizes.
This is initialized from the "p13n.jdbc.maxsize.checkMetaData"
property in the weblogiccommerce.properties.
DATETIME
public static final int DATETIME
- The constant for a datetime type (4).
FLOAT
public static final int FLOAT
- The constant for the floating-point numeric type (2).
INTEGER
public static final int INTEGER
- The constant for a numeric type (1).
MULTI_VALUED
public static final int MULTI_VALUED
- The constant for a multivalued type (6).
TEXT
public static final int TEXT
- The constant for a text type (3).
USER_DEFINED
public static final int USER_DEFINED
- The constant for a user defined type (5).
ColumnSizes
public ColumnSizes()
fromSQLType(int) Method
public static int fromSQLType(int sqlType)
Convert a java.sql.Types constant into one of ours.
Parameters
-
sqlType
- value from java.sql.Types.
Returns
- the corresponding attribute type (BOOLEAN, NUMERIC, TEXT,
DATETIME, MULTI_VALUED, or USER_DEFINED).
getColumnInfo(Connection, String, String) Method
public static ColumnSizes.ColumnInfo
getColumnInfo(Connection
con,
String
table,
String
column)
throws SQLException
Get the ColumnInfo for the specified column.
If the column information is not found in the cache, it will be
loaded (see class comments for details on how it is loaded).
Parameters
-
con
- the connection to use, null to not.
-
table
- the table name.
-
column
- the column name.
Returns
- the ColumnInfo or null if the column doesn't exist.
Exceptions
-
SQLException
- if the information cannot be loaded.
getTypeName(int) Method
public static String
getTypeName(int type)
Get the default descriptive type name.
truncate(Object, Connection, String, String) Method
public static Object
truncate(Object
in,
Connection
con,
String
table,
String
column)
Truncate the specified object to the size of the specified
table/column.
truncate(String, Connection, String, String) Method
public static String
truncate(String
in,
Connection
con,
String
table,
String
column)
Truncate the specified String to the size of the specified
table/column.
Related Topics
ColumnSizes.truncate(Object, Connection, String, String)