All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.frameworkset.common.poolman.handle.ValueExchange Maven / Gradle / Ivy

Go to download

bboss is a j2ee framework include aop/ioc,mvc,persistent,taglib,rpc,event ,bean-xml serializable and so on.http://www.bbossgroups.com

The newest version!
package com.frameworkset.common.poolman.handle;

import com.frameworkset.common.poolman.NestedSQLException;
import com.frameworkset.common.poolman.util.SQLUtil;
import com.frameworkset.orm.adapter.DB;
import com.frameworkset.util.*;
import oracle.jdbc.OracleTypes;
import org.frameworkset.util.TimeUtil;
import org.frameworkset.util.annotations.wraper.ColumnWraper;
import org.frameworkset.util.encoder.BASE64Encoder;

import java.io.*;
import java.math.BigDecimal;
import java.sql.*;
import java.text.DecimalFormat;
import java.util.Locale;

/**
 * 
 * 
 * 

* Title: TypeHandler.java *

* *

* Description: *

* 数据交换,实现数据库记录字段的值和java中数据类型值的转换 *

* Copyright: Copyright (c) 2007 *

* * * @Date Oct 29, 2008 10:36:23 AM * @author biaoping.yin * @version 1.0 */ public class ValueExchange { // public static final TypeHandlerFactory typeHandlerFactory = new TypeHandlerFactory(); // public Object valueChange(CallableStatement cstmt, Class toType, // int sqlType, String dbname) { // switch (sqlType) { // // } // return null; // } // // public Object valueChange(ResultSet rs, Class toType, int sqlType, // String dbname) { // return null; // } // public static TypeHandler getTypeHandler(int sqlType) // { // TypeHandler handler = null; // switch (sqlType) { // // // case Types.LONGVARBINARY: // /********************************************************************** // * Types.BIT Types.BOOLEAN java.lang.Boolean // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * BIT. // */ // case Types.BIT : // /** // * The constant in the Java programming language, somtimes referred to // * as a type code, that identifies the generic SQL type BOOLEAN. // * // * @since 1.4 // */ // case Types.BOOLEAN : // //// BooleanTypeHandler b = new BooleanTypeHandler(); // handler = typeHandlerFactory.getTypeHandler(boolean.class); //// return b.getResult(cs, columnIndex, javaType); // return handler; // // /********************************************************************** // * Types.TINYINT byte // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * TINYINT. // */ // case Types.TINYINT : //// ByteTypeHandler byteHander = new ByteTypeHandler(); //// return byteHander.getResult(cs, columnIndex, javaType); // handler = typeHandlerFactory.getTypeHandler(byte.class); // return handler; // /********************************************************************** // * Types.SMALLINT short // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * SMALLINT. // */ // case Types.SMALLINT : // handler = typeHandlerFactory.getTypeHandler(short.class); // return handler; //// return shortHandler.getResult(cs, columnIndex, javaType); // // /********************************************************************** // * Types.SMALLINT int // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * INTEGER. // */ // case Types.INTEGER : // handler = typeHandlerFactory.getTypeHandler(int.class); // return handler; // /********************************************************************** // * Types.BIGINT long // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * BIGINT. // */ // case Types.BIGINT: // /** // * 处理long类型的字段时需要,当应用程序向long形字段中存放大文本时需要,存放Reader // * res.getCharacterStream(); // */ //// Object reader = null; //// // reader = cstmt.getCharacterStream(i); //// Object temp = cstmt.getObject(i); //// // value = new Object[] { temp, reader }; //// value = temp; //// break; // handler = typeHandlerFactory.getTypeHandler(long.class); // return handler; // // // // // /********************************************************************** // * Types.REAL float // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * REAL. // */ // case Types.REAL : // handler = typeHandlerFactory.getTypeHandler(float.class); // return handler; // // /********************************************************************** // * Types.DOUBLE Types.FLOAT double ???? Types.FLOAT 为什么要归结为double而不是float // **********************************************************************/ // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * FLOAT. // */ // case Types.FLOAT : // handler = typeHandlerFactory.getTypeHandler(float.class); // return handler; // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * DOUBLE. // */ // case Types.DOUBLE : // handler = typeHandlerFactory.getTypeHandler(double.class); // return handler; // /********************************************************************** // * Types.NUMERIC Types.DECIMAL java.lang.BigDecimal // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * NUMERIC. // */ // case Types.NUMERIC : // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * DECIMAL. // */ // case Types.DECIMAL : //// value = cstmt.getBigDecimal(i); //// BigDecimalTypeHandler big = new BigDecimalTypeHandler(); //// return big.getResult(cs, columnIndex, javaType); // handler = typeHandlerFactory.getTypeHandler(BigDecimal.class); // return handler; //// break; // // /********************************************************************** // * Types.CHAR Types.VARCHAR Types.LONGVARCHAR Types.CLOB java.lang.String // **********************************************************************/ // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * CHAR. // */ // case Types.CHAR: // handler = typeHandlerFactory.getTypeHandler(String.class); // return handler; // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * VARCHAR. // */ // case Types.VARCHAR : // handler = typeHandlerFactory.getTypeHandler(String.class); // return handler; // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * LONGVARCHAR. // */ // case Types.LONGVARCHAR : // handler = typeHandlerFactory.getTypeHandler(String.class,"LONGVARCHAR"); // return handler; // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * CLOB. // * @since 1.2 // */ // case Types.CLOB: //// Clob clob = (Clob) cstmt.getObject(i); //// value = clob; //// // value = this.clobToString(clob); //// break; //// try { // // not sure about this fix, so be overly cautious // // 判断是否有中文如果有中文则,有些数据库需要处理调后面多余的空格,比如oracle // handler = typeHandlerFactory.getTypeHandler(String.class,"CLOB"); // return handler; //// value = cstmt.getString(i); //// ////// value = SQLUtil.getPool(dbname).getDbAdapter() ////// .getCharValue(cstmt, i, (String) value); //// //// } catch (Exception _e) { //// // _e.printStackTrace(); //// // System.out.println("column :" + i + " " + //// // meta.getColumnLabel(i)); //// // 捕获异常:有些数据库中(例如mssql //// // server2000)表字段的值为空的情况下调用res.getObject(i)会报 //// // 读取零字节异常,造成数据读取失败 //// try { //// value = cstmt.getObject(i); //// } catch (Exception e) { //// value = null; //// } //// } //// break; // /********************************************************************** // * Types.DATE Types.TIME Types.TIMESTAMP OracleTypes.TIMESTAMPLTZ // * OracleTypes.TIMESTAMPNS OracleTypes.TIMESTAMPTZ java.util.Date // **********************************************************************/ // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * DATE. // */ // case Types.DATE: // // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; //// try { //// value = cstmt.getTimestamp(i); //// break; //// } catch (Exception e) { //// value = cstmt.getDate(i); //// break; //// } // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * TIME. // */ // case Types.TIME: // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; //// value = cstmt.getTime(i); // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * TIMESTAMP. // */ // case Types.TIMESTAMP: // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; // case OracleTypes.TIMESTAMPLTZ: // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; // case OracleTypes.TIMESTAMPNS: // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; // case OracleTypes.TIMESTAMPTZ: // handler = typeHandlerFactory.getTypeHandler(java.util.Date.class); // return handler; // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * BINARY. // */ // case Types.BINARY : // handler = typeHandlerFactory.getTypeHandler(byte[].class,"BINARY"); // return handler; // // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * VARBINARY. // */ // case Types. VARBINARY : // handler = typeHandlerFactory.getTypeHandler(byte[].class,"VARBINARY"); // return handler; // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * LONGVARBINARY. // */ // case Types.LONGVARBINARY : // handler = typeHandlerFactory.getTypeHandler(byte[].class,"LONGVARBINARY"); // return handler; // case Types.BLOB: //// Blob blob = (Blob) cstmt.getObject(i); //// value = blob; //// // value = this.blobToByteArray(blob); //// break; // handler = typeHandlerFactory.getTypeHandler(byte[].class,"BLOB"); // return handler; // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * JAVA_OBJECT. // * @since 1.2 // */ // case Types.JAVA_OBJECT : // handler = typeHandlerFactory.getTypeHandler(Object.class, "OBJECT"); // return handler; // /** // *

The constant in the Java programming language, sometimes referred // * to as a type code, that identifies the generic SQL type // * NULL. // */ // case Types.NULL : // // /** // * The constant in the Java programming language that indicates // * that the SQL type is database-specific and // * gets mapped to a Java object that can be accessed via // * the methods getObject and setObject. // */ // case Types.OTHER : // // // // // // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * DISTINCT. // * @since 1.2 // */ // case Types.DISTINCT : // // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * STRUCT. // * @since 1.2 // */ // case Types.STRUCT : // // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * ARRAY. // * @since 1.2 // */ // case Types.ARRAY : // // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * BLOB. // * @since 1.2 // */ // // // // // /** // * The constant in the Java programming language, sometimes referred to // * as a type code, that identifies the generic SQL type // * REF. // * @since 1.2 // */ // case Types.REF : // // /** // * The constant in the Java programming language, somtimes referred to // * as a type code, that identifies the generic SQL type DATALINK. // * // * @since 1.4 // */ // case Types.DATALINK : // // // // // // default: // // System.out.println("column :" + i + " " + // // meta.getColumnLabel(i)); // // 捕获异常:有些数据库中(例如mssql // // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // // 读取零字节异常,造成数据读取失败 // handler = typeHandlerFactory.getTypeHandler(Object.class, "OBJECT"); // return handler; // } // } public static byte[] getResultBytes(Blob blob) throws SQLException { byte[] returnValue; if (blob != null) { // returnValue = blob.getBytes(1, (int) blob.length()); returnValue = ValueExchange.getByteArrayFromBlob(blob); return returnValue; } else { returnValue = null; } return returnValue; } public static byte[] convertObjectToBytes(Object bytes) throws SQLException{ if(bytes == null) return null; if(bytes instanceof String) { return ((String)bytes).getBytes(); } else if(bytes instanceof java.sql.Blob) { try { return getResultBytes((Blob)bytes); } catch (SQLException e) { throw e; }catch (Exception e) { throw new NestedSQLException(e); } } else if(bytes instanceof java.sql.Clob) { try { return getResultBytes((Clob)bytes); } catch (SQLException e) { throw e; }catch (Exception e) { throw new NestedSQLException(e); } } return (byte[])bytes; } public static byte[] getResultBytes(Clob clob) throws SQLException{ String value; // Clob clob = rs.getClob(columnIndex); if (clob != null) { // int size = (int) clob.length(); return ValueExchange.getByteArrayFromClob(clob); // value = clob.getSubString(1, size); // return value.getBytes(); } else { return null; } } public static Object getValueFromCallableStatement(CallableStatement cs, int columnIndex, int sqltype, Class javaType,String dbname,EditorInf editor) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(cs, columnIndex, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; Object value = getValueFromCallableStatement(cs, columnIndex, sqltype, dbname); if(editor == null) { if(value == null) return ValueObjectUtil.getDefaultValue(javaType); return convert(value, value.getClass(), javaType); } else { return editor.getValueFromObject(value); } } public static Object getValueFromCallableStatement(CallableStatement cs, String parameterName, int sqltype, Class javaType,String dbname,ColumnEditorInf editor, ColumnWraper cl ) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(cs, parameterName, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; Object value = getValueFromCallableStatement(cs, parameterName, sqltype, dbname); if(editor == null || editor instanceof FieldToColumnEditor) { if(value == null) return value; return convert(value, value.getClass(), javaType); } else { return editor.getValueFromObject(cl,value); } } // public static Object getValueFromCallableStatement(CallableStatement cs, // int columnIndex, int sqltype, Class javaType,String dbname) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(cs, columnIndex, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; // } // // public static Object getValueFromCallableStatement(CallableStatement cs, // String parameterName, int sqltype, Class javaType,String dbname) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(cs, parameterName, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; // } // public static Object getValueFromResultSet(ResultSet rs, // String parameterName, int sqltype, Class javaType,String dbname) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(rs, parameterName, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; // } // // public static Object getValueFromResultSet(ResultSet rs, // int columnIndex, int sqltype, Class javaType,String dbname) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(rs, columnIndex, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; // } public static Object getValueFromCallableStatement(CallableStatement cstmt, int i, int type, String dbname) throws SQLException { Object value = null; try { DB db = SQLUtil.getPool(dbname).getDbAdapter(); switch (type) { // case Types.LONGVARBINARY: case Types.CHAR: try { // not sure about this fix, so be overly cautious // 判断是否有中文如果有中文则,有些数据库需要处理调后面多余的空格,比如oracle value = cstmt.getString(i); value = db.getCharValue(cstmt, i, (String) value); } catch (Exception _e) { // _e.printStackTrace(); // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 try { value = cstmt.getObject(i); } catch (Exception e) { value = null; } } break; case Types.BIGINT: /** * 处理long类型的字段时需要,当应用程序向long形字段中存放大文本时需要,存放Reader * res.getCharacterStream(); */ Object reader = null; // reader = cstmt.getCharacterStream(i); Object temp = cstmt.getObject(i); // value = new Object[] { temp, reader }; value = temp; break; case Types.BLOB: Blob blob = cstmt.getBlob(i); value = blob; //value = this.blobToByteArray(blob); break; case Types.LONGVARBINARY: value = db.getLONGVARBINARY(cstmt, i); //value = this.blobToByteArray(blob); break; case Types.CLOB: Clob clob = cstmt.getClob(i); value = clob; //value = this.clobToString(clob); break; case Types.LONGVARCHAR: value = db.getLONGVARCHAR(cstmt, i); //value = this.clobToString(clob); break; case OracleTypes.TIMESTAMPLTZ: value = cstmt.getTimestamp(i); break; case OracleTypes.TIMESTAMPNS: value = cstmt.getTimestamp(i); break; case OracleTypes.TIMESTAMPTZ: value = cstmt.getTimestamp(i); break; case Types.TIMESTAMP: value = cstmt.getTimestamp(i); break; case Types.TIME: value = cstmt.getTime(i); break; case Types.DATE: try { value = cstmt.getTimestamp(i);break; } catch(Exception e) { value = cstmt.getDate(i); if(value != null) value = new java.sql.Timestamp(((java.sql.Date)value).getTime()); break; } default: // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 try { value = cstmt.getObject(i); value = TimeUtil.convertLocalDate(value); } catch (Exception e) { value = null; } // value = res.getObject(i); break; } } catch (SQLException ee) { throw ee; } catch (Exception ee) { throw new NestedSQLException(ee); } return value; } public static Object getValueFromCallableStatement(CallableStatement cstmt, String paramName, int sqltype, String dbname) throws SQLException { Object value = null; // SchemaType schemaType = SQLUtil.getSchemaType(dbname, sqltype); // String javaType = schemaType.getJavaType(); try { switch (sqltype) { // case Types.LONGVARBINARY: case Types.CHAR: try { // not sure about this fix, so be overly cautious // 判断是否有中文如果有中文则,有些数据库需要处理调后面多余的空格,比如oracle value = cstmt.getString(paramName); value = SQLUtil.getPool(dbname).getDbAdapter().getCharValue(cstmt, paramName, (String) value); } catch (Exception _e) { // _e.printStackTrace(); // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 try { value = cstmt.getObject(paramName); } catch (Exception e) { value = null; } } break; case Types.BIGINT: /** * 处理long类型的字段时需要,当应用程序向long形字段中存放大文本时需要,存放Reader * res.getCharacterStream(); */ Object reader = null; // reader = cstmt.getCharacterStream(i); Object temp = cstmt.getObject(paramName); value = temp; break; case Types.BLOB: case Types.LONGVARBINARY: Blob blob = cstmt.getBlob(paramName); value = blob; //value = this.blobToByteArray(blob); break; case Types.CLOB: case Types.LONGVARCHAR: Clob clob = (Clob) cstmt.getClob(paramName); value = clob; //value = this.clobToString(clob); break; case OracleTypes.TIMESTAMPLTZ: value = cstmt.getTimestamp(paramName); break; case OracleTypes.TIMESTAMPNS: value = cstmt.getTimestamp(paramName); break; case OracleTypes.TIMESTAMPTZ: value = cstmt.getTimestamp(paramName); break; case Types.TIMESTAMP: value = cstmt.getTimestamp(paramName); break; case Types.TIME: value = cstmt.getTime(paramName); break; case Types.DATE: try { value = cstmt.getTimestamp(paramName);break; } catch(Exception e) { value = cstmt.getDate(paramName); if(value != null) value = new java.sql.Timestamp(((java.sql.Date)value).getTime()); break; } default: // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 try { value = cstmt.getObject(paramName); value = TimeUtil.convertLocalDate(value); } catch (Exception e) { value = null; } // value = res.getObject(i); break; } } catch (SQLException ee) { throw ee; } catch (Exception ee) { throw new NestedSQLException(ee); } return value; } public static byte[] getByteArrayFromBlob(Blob blob) throws SQLException { return ValueObjectUtil.getByteArrayFromBlob(blob); } public static String getByteStringFromBlob(Blob blob) throws SQLException { return ValueObjectUtil. getByteStringFromBlob( blob); } public static String getStringFromBlob(Blob blob) throws SQLException { return ValueObjectUtil.getStringFromBlob(blob); } public static byte[] getByteArrayFromClob(Clob clob) throws SQLException { return ValueObjectUtil.getByteArrayFromClob( clob); } public static String getStringFromClob(Clob clob) throws SQLException { return ValueObjectUtil.getStringFromClob(clob); } public static String getStringFromReader(Reader in) throws SQLException { StringWriter w = null; if(in == null) return null; // Reader out = null; try { w = new StringWriter(); // out = in; char[] buf = new char[1024]; int i =0; while((i = in.read(buf)) > 0) { w.write(buf,0,i); } return w.toString(); } // catch(SQLException e) // { // throw e; // } catch(Exception e) { throw new NestedSQLException(e); } finally { try { if(in != null) { in.close(); in = null; } } catch(Exception e) { } try { if(w != null) { w.close(); w = null; } } catch(Exception e) { } } } public static String getStringFromStream(InputStream in) throws SQLException { if(in == null) return null; OutputStream out = null; // InputStream in = null; try { out = new ByteArrayOutputStream(); // in = blob.getBinaryStream(); byte[] buf = new byte[1024]; int i =0; while((i = in.read(buf)) > 0) { // System.out.println("i=" + i); out.write(buf,0,i); } return out.toString(); } // catch(SQLException e) // { // throw e; // } catch(Exception e) { throw new NestedSQLException(e); } finally { try { if(out != null) { out.close(); out = null; } } catch(Exception e) { } try { if(in != null) { in.close(); in = null; } } catch(Exception e) { } } } public static String getStringFromObject(Object value) throws SQLException { String ret = null; if(value == null) { return null; } else { try { if(value instanceof String) return (String)value; else if(value instanceof Clob) { ret = getStringFromClob((Clob)value); } else if(value instanceof Blob) { ret = getStringFromBlob((Blob)value); } else if(value instanceof byte[]) { return new String(((byte[]) value)); } else if(value instanceof InputStream) { ret = ValueExchange.getStringFromStream((InputStream)value); } else if(value instanceof Reader) { ret = ValueExchange.getStringFromReader((Reader)value); } else { return value.toString(); } } catch(SQLException ioe) { throw ioe; } catch(Exception e) { throw new NestedSQLException(e); } } return ret; } // public static Object getValueFromResultSet(ResultSet rs, // String parameterName, int sqltype, Class javaType,String dbname) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(rs, parameterName, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } //// return value; // // } public static Object getValueFromResultSet(ResultSet rs, int columnIndex, int sqltype, Class javaType,DB db,ColumnEditorInf editor,ColumnWraper columnWraper) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(rs, columnIndex, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; Object value = getValueFromRS(rs, columnIndex, sqltype, db); if(editor == null || editor instanceof FieldToColumnEditor) { if(value == null) return ValueObjectUtil.getDefaultValue(javaType); return convert(value, value.getClass(), javaType); } else { return editor.getValueFromObject(columnWraper,value); } } public static Object getValueFromResultSet(ResultSet rs, String column, int sqltype, Class javaType,String dbname,ColumnEditorInf editor,ColumnWraper cl) throws SQLException{ // Object value = null; // try { // TypeHandler handler = getTypeHandler(sqltype); // if(handler != null) // return handler.getResult(rs, columnIndex, javaType, null); // } catch (SQLException ee) { // throw ee; // } catch (Exception ee) { // throw new NestedSQLException(ee); // } // return value; Object value = getValueFromRS(rs, column, sqltype, dbname); if(editor == null || editor instanceof FieldToColumnEditor) { if(value == null) return ValueObjectUtil.getDefaultValue(javaType); return convert(value, value.getClass(), javaType); } else { return editor.getValueFromObject(cl,value); } } public static Object convert(Object value, Class type, Class javaType) throws SQLException{ try { if(javaType == null || value == null) return ValueObjectUtil.getDefaultValue(javaType); if(java.sql.Clob.class.isAssignableFrom(type)) { if(javaType == Clob.class) return value; // value = ValueExchange.getStringFromClob((Clob)value); } else if(java.sql.Blob.class.isAssignableFrom(type)) { if(javaType == Blob.class) return value; // value = ValueExchange.getStringFromBlob((Blob)value); } return ValueObjectUtil.typeCast(value, value.getClass(), javaType); } catch (NumberFormatException e) { throw new NestedSQLException(e); } catch (IllegalArgumentException e) { throw new NestedSQLException(e); } catch (NoSupportTypeCastException e) { throw new NestedSQLException(e); } catch (Exception e) { throw new NestedSQLException(e); } // return null; } public static Object changeLob2String(Object value) throws SQLException { if(value != null ){ if(value instanceof Clob) value = ValueExchange.getStringFromClob((Clob) value); else if(value instanceof Blob){ value = ValueExchange.getStringFromBlob((Blob) value); } } return value; } public static Object getValueFromRS(ResultSet res, int i, int type, DB db) throws SQLException { Object value = null; // DB db = SQLUtil.getPool(dbname).getDbAdapter(); try { switch (type) { case Types.VARCHAR: value = res.getString(i);break; // case Types.LONGVARBINARY: case Types.BIGINT: /** * 处理long类型的字段时需要,当应用程序向long形字段中存放大文本时需要,存放Reader * res.getCharacterStream(); */ // Object reader = null; // reader = res.getCharacterStream(i); // Object temp = res.getObject(i); // value = new Object[] { temp, reader }; long temp = res.getLong(i); value = new Long(temp); break; case Types.TIMESTAMP: value = res.getTimestamp(i); break; case Types.DATE: try { value = res.getTimestamp(i);break; } catch(Exception e) { value = res.getDate(i); if(value != null) value = new java.sql.Timestamp(((java.sql.Date)value).getTime()); break; } case Types.DECIMAL: value =res.getBigDecimal(i); break; case Types.CHAR: try { // not sure about this fix, so be overly cautious // 判断是否有中文如果有中文则,有些数据库需要处理调后面多余的空格,比如oracle value = res.getString(i); // value = db.getCharValue(res, i, // (String) value); } catch (SQLException _e) { // // _e.printStackTrace(); // // System.out.println("column :" + i + " " + // // meta.getColumnLabel(i)); // // 捕获异常:有些数据库中(例如mssql // // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // // 读取零字节异常,造成数据读取失败 // value = res.getObject(i); throw _e; } catch (Exception _e) { // // _e.printStackTrace(); // // System.out.println("column :" + i + " " + // // meta.getColumnLabel(i)); // // 捕获异常:有些数据库中(例如mssql // // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // // 读取零字节异常,造成数据读取失败 // value = res.getObject(i); throw new NestedSQLException(_e); } break; case Types.BLOB: Blob blob = res.getBlob(i); value = blob; //value = this.blobToByteArray(blob); break; case Types.LONGVARBINARY: value = db.getLONGVARBINARY(res, i); //value = this.blobToByteArray(blob); break; case Types.CLOB: Clob clob = res.getClob(i); value = clob; //value = this.clobToString(clob); break; case Types.LONGVARCHAR: value = db.getLONGVARCHAR(res, i); //value = this.clobToString(clob); break; case OracleTypes.TIMESTAMPLTZ: value = res.getTimestamp(i); break; case OracleTypes.TIMESTAMPNS: value = res.getTimestamp(i); break; case OracleTypes.TIMESTAMPTZ: value = res.getTimestamp(i); break; case Types.TIME: value = res.getTime(i); break; default: // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 value = res.getObject(i); value = TimeUtil.convertLocalDate(value); // value = res.getObject(i); break; } } catch(SQLException ioe) { throw ioe; } catch(Exception e) { throw new NestedSQLException(e); } return value; } public static Object getValueFromRS(ResultSet res, String i, int type, String dbname) throws SQLException { Object value = null; DB db = SQLUtil.getPool(dbname).getDbAdapter(); try { switch (type) { case Types.VARCHAR: value = res.getString(i);break; // case Types.LONGVARBINARY: case Types.BIGINT: /** * 处理long类型的字段时需要,当应用程序向long形字段中存放大文本时需要,存放Reader * res.getCharacterStream(); */ // Object reader = null; // reader = res.getCharacterStream(i); // Object temp = res.getObject(i); // value = new Object[] { temp, reader }; long temp = res.getLong(i); value = new Long(temp); break; case Types.TIMESTAMP: value = res.getTimestamp(i); break; case Types.DATE: try { value = res.getTimestamp(i);break; } catch(Exception e) { value = res.getDate(i); if(value != null) value = new java.sql.Timestamp(((java.sql.Date)value).getTime()); break; } case Types.DECIMAL: value =res.getBigDecimal(i); break; case Types.CHAR: try { // not sure about this fix, so be overly cautious // 判断是否有中文如果有中文则,有些数据库需要处理调后面多余的空格,比如oracle value = res.getString(i); // value = db.getCharValue(res, i, // (String) value); } catch (SQLException _e) { // // _e.printStackTrace(); // // System.out.println("column :" + i + " " + // // meta.getColumnLabel(i)); // // 捕获异常:有些数据库中(例如mssql // // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // // 读取零字节异常,造成数据读取失败 // value = res.getObject(i); throw _e; } catch (Exception _e) { // // _e.printStackTrace(); // // System.out.println("column :" + i + " " + // // meta.getColumnLabel(i)); // // 捕获异常:有些数据库中(例如mssql // // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // // 读取零字节异常,造成数据读取失败 // value = res.getObject(i); throw new NestedSQLException(_e); } break; case Types.BLOB: Blob blob = res.getBlob(i); value = blob; //value = this.blobToByteArray(blob); break; case Types.LONGVARBINARY: value = db.getLONGVARBINARY(res, i); //value = this.blobToByteArray(blob); break; case Types.CLOB: Clob clob = res.getClob(i); value = clob; //value = this.clobToString(clob); break; case Types.LONGVARCHAR: value = db.getLONGVARCHAR(res, i); //value = this.clobToString(clob); break; case OracleTypes.TIMESTAMPLTZ: value = res.getTimestamp(i); break; case OracleTypes.TIMESTAMPNS: value = res.getTimestamp(i); break; case OracleTypes.TIMESTAMPTZ: value = res.getTimestamp(i); break; case Types.TIME: value = res.getTime(i); break; default: // System.out.println("column :" + i + " " + // meta.getColumnLabel(i)); // 捕获异常:有些数据库中(例如mssql // server2000)表字段的值为空的情况下调用res.getObject(i)会报 // 读取零字节异常,造成数据读取失败 value = res.getObject(i); value = TimeUtil.convertLocalDate(value); // value = res.getObject(i); break; } } catch(SQLException ioe) { throw ioe; } catch(Exception e) { throw new NestedSQLException(e); } return value; } /** * 数字转化为汉字 * @param column * @return */ public static String numberToCN(String column){ char[] cnNumbers={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'}; if(!"".equals(column)){ StringBuilder stringbuffer = new StringBuilder(); for(int i=0;i= 0) // fmt.append(' '); //to compensate for minus sign. if(precision != null && !"".equals(precision)){ precision_ = Integer.parseInt(precision); } // if(length != null && !"".equals(length)){ // length_ = Integer.parseInt(length); // } if (precision_<0) // Default: two decimals { // for (i=0;i=0 && pos = fmt.length()){ // for(int j=0;j< String.valueOf(value).length()+1;j++) // fmt.append('0'); // fmt.setCharAt(pos+1-(value<0?1:0), '.'); // }if(pos<=0){ // for(int j=0;j< precision_;j++) // fmt.append('0'); // fmt.setCharAt(0, '.'); // } int idex = ret.indexOf("."); if(idex > 0) { String sn = ret.substring(idex + 1); if(sn.length() > precision_) { sn = sn.substring(0,precision_); } else if(sn.length() < precision_) { StringBuilder ret_ = new StringBuilder(); int rap = precision_ - sn.length(); for(int i = 0; i < rap; i ++) ret_.append("0"); sn = sn + ret_; } String integers = ret.substring(0,idex); ret = integers + "." + sn; } } // form= new DecimalFormat(fmt.toString()); // ret=form.format(value); return ret; } public static String toStringDate(Date date, String dataformat,Locale locale) { //进行转换函数处理 if(date == null) return null; if(dataformat != null && !"".equals(dataformat)){ return ValueObjectUtil.getDateFormat(dataformat,locale).format(date); }else { return ValueObjectUtil.getDateFormat(null,locale).format(date); } } public static String toStringInteger(int value, String length, int numbertocn, String dataformat) { //先判断值是否为空 String ret = ""; // 先完成对整数的处理 if(numbertocn == 1){ return numberToCN(String.valueOf(value)); }else if(dataformat != null && !"".equals(dataformat)){ DecimalFormat form= new DecimalFormat(dataformat); ret = form.format(value); } else ret = value + ""; // //接着做长度处理 // StringBuilder fmt=new StringBuilder(); // int i; // DecimalFormat form; // // if (value>=0) fmt.append(' '); //to compensate for minus sign. // int len = 0; // if(length != null && !"".equals(length)){ // len = Integer.parseInt(length); // for (i=0;i limit) { ret.setLength(limit); } else { for (l = len; l < limit; l++) ret.append(' '); } return ret.toString(); } public static String toStringString(String origin,int swtich_case, String length) { if(origin == null || "".equals(origin)) return null; if(swtich_case == 1){ origin = origin.toUpperCase(); } String retval=null; StringBuilder ret = new StringBuilder(origin); int length_ = 0; if( length != null && !"".equals(length)){ length_ = Integer.parseInt(length); if(length_>16384){ length_ = 16384; } rightPad(ret, length_); } retval = ret.toString(); return retval; } public static String toStringTimeStamp(Timestamp timestamp, String dataformat,Locale locale) { //进行转换函数处理 if(timestamp == null) return null; if(dataformat != null && !"".equals(dataformat)){ return ValueObjectUtil.getDateFormat(dataformat, locale).format(timestamp); }else { return ValueObjectUtil.getDateFormat(null, locale).format(timestamp); } } public static String toStringTime(Time time){ if(time != null) return time.toString(); return null; } public static String getByteStringFromBytes(byte[] bytes) { if(bytes == null) return null; BASE64Encoder en = new BASE64Encoder(); return en.encode(bytes); } public static String getStringFromBytes(byte[] bytes) { if(bytes == null) return null; return new String(bytes); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy