private void psSetValue(int index, PreparedStatement ps, Integer type, String typeName, Object value) throws SQLException, UnsupportedEncodingException {
if(value==null){
ps.setObject(index,null);
return;
}
if(ignoreTypeSet.contains(typeName)){
ps.setObject(index,null);
return;
}
switch (type){
case Types.VARBINARY:
ps.setBlob(index, new ByteArrayInputStream(ByteUtil.parseObjectToByteArray(value)));
break;
case Types.LONGVARCHAR:
ps.setString(index,value.toString());
break;
case Types.BINARY:
ps.setBlob(index, new ByteArrayInputStream(ByteUtil.parseObjectToByteArray(value)));
break;
case Types.DECIMAL:
ps.setBigDecimal(index, BigDecimal.valueOf(Long.parseLong(value.toString())));
break;
case Types.TIMESTAMP://93
String s = value.toString();
Long timestampLong = Long.parseLong(s);
Timestamp timestamp = new Timestamp(timestampLong);
ps.setTimestamp(index,timestamp);
break;
case Types.DATE:
if(typeName.equals("YEAR")){
Date date = new Date(Long.parseLong(value.toString()));
Calendar now = Calendar.getInstance();
now.setTime(date);
ps.setInt(index,now.get(Calendar.YEAR));
break;
}
Date date = new Date(Long.parseLong(value.toString()));
ps.setDate(index,date);
break;
case Types.TIME:
Time time = new Time(Long.parseLong(value.toString()));
ps.setTime(index,time);
break;
case Types.LONGVARBINARY:
ps.setBlob(index, new ByteArrayInputStream(ByteUtil.parseObjectToByteArray(value)));
break;
case Types.VARCHAR:
ps.setString(index,value.toString());
break;
case Types.CHAR:
ps.setString(index,value.toString());
break;
case Types.BIT:
ps.setBoolean(index,Boolean.parseBoolean(value.toString()));
break;
case Types.INTEGER:
ps.setInt(index,Integer.parseInt(value.toString()));
default:
ps.setObject(index,value);
break;
}
}
public static byte[] parseObjectToByteArray(Object value) {
Object[] objects = (Object[]) value;
byte[] byteArray = new byte[objects.length];
for (int kkk = 0; kkk < objects.length; kkk++) {
Object o = objects[kkk];
if (o instanceof Number) {
byteArray[kkk] = ((Number) o).byteValue();
}
}
return byteArray;
}
private static Set<String> ignoreTypeSet = new HashSet<>();//空间位置相关的数据类型
static {
ignoreTypeSet.add("GEOMETRY");
ignoreTypeSet.add("GEOMETRYCOLLECTION");
ignoreTypeSet.add("POINT");
ignoreTypeSet.add("POLYGON");
ignoreTypeSet.add("LINESTRING");
}